- The command line (or terminal) is a text-based interface used to interact with the computer’s operating system.
- It allows users to execute commands by typing them in and pressing Enter.
- Commonly used for navigating the file system, running scripts, and managing system processes.
- Efficiency: Many tasks can be performed faster using the command line compared to graphical interfaces.
- Automation: Scripts can automate repetitive tasks.
- Remote Access: Essential for managing servers or working on remote machines.
- Learning Curve: Fundamental for understanding the underlying workings of operating systems and development environments.
- Windows: Open the Command Prompt (cmd) or PowerShell. Search for "cmd" or "PowerShell" in the Start menu.
- Mac: Open the Terminal. Find it in Applications > Utilities > Terminal.
- Linux: Open the Terminal. Depending on the distribution, find it in the application menu or use the keyboard shortcut (often
Ctrl + Alt + T
).
-
Current Directory: Display the current working directory.
pwd
(Print Working Directory)
-
Listing Files: List files and directories in the current directory.
ls
- Options:
ls -l
(detailed list)ls -a
(include hidden files)
- Options:
-
Changing Directory: Move to a different directory.
cd /path/to/directory
- Example:
cd Documents
- Go up one level:
cd ..
- Example:
-
Creating a Directory: Make a new directory.
mkdir directory-name
- Example:
mkdir new-folder
- Example:
-
Creating a File: Create an empty file or edit a file.
touch filename
- Example:
touch newfile.txt
- Example:
-
Copying Files: Copy a file or directory.
cp source destination
- Example:
cp file.txt copyfile.txt
- For directories:
cp -r source-directory destination-directory
- Example:
-
Moving/Renaming Files: Move or rename a file or directory.
mv source destination
- Example:
mv oldname.txt newname.txt
- Example:
-
Deleting Files: Remove a file.
rm filename
- Example:
rm unwantedfile.txt
- For directories:
rm -r directory-name
- Example:
-
Editing File Contents: Replace the contents of a file.
echo "file content" > filename
- Example:
echo "First line" > example.txt
- Example:
-
Editing File Contents: Append to the contents of a file.
echo "file content" >> filename
- Example:
echo "First line" >> example.txt
- Example:
-
Viewing File Contents: Display the contents of a file.
cat filename
- Example:
cat example.txt
- Example:
-
Editing Files: Use text editors like
nano
,vim
, orcode
(VS Code).code filename
- Example:
code notes.txt
- Example:
-
Clear Screen: Clear the terminal screen.
clear
-
Exiting the Terminal: Close the terminal session.
exit
-
Tab Completion: Press
Tab
to auto-complete commands or file names. -
Command History: Use the up and down arrow keys to navigate through previously entered commands.
-
Manual Pages: Use
man
to access the manual pages for commands.man command
- Example:
man ls
- Example:
-
Shortcuts: Learn keyboard shortcuts to improve efficiency (e.g.,
Ctrl + C
to stop a running command).
- Navigate to your home directory:
cd ~
- Create a directory called
projects
:mkdir projects
- Move into the
projects
directory:cd projects
- Create a new file called
readme.txt
:touch readme.txt
- List the contents of the directory:
ls
- Open
readme.txt
in a text editor (e.g., nano):code readme.txt
- Write a few lines in
readme.txt
, save, and exit:- In
code
, type your text, then pressCtrl + X
,Y
, andEnter
to save and exit.
- In
Understanding and using the command line is a foundational skill for developers, enhancing their ability to manage files, run programs, and automate tasks effectively.