Command Line Basics
Basic Linux command line notes.
This page explains common commands and how to move around the terminal.
What is the command line?
The command line is where you type commands to interact with the system.
Example:
ls
The shell reads the command and runs it.
Show current directory
pwd
pwd means:
print working directory
Example output:
/home/user
List files
ls
List with details:
ls -l
List hidden files too:
ls -la
Human-readable sizes:
ls -lah
Change directory
cd /path/to/directory
Go to home directory:
cd
Go one directory up:
cd ..
Go to previous directory:
cd -
Create directory
mkdir DIRECTORY_NAME
Create parent directories if needed:
mkdir -p /path/to/directory
Create empty file
touch file.txt
Copy files
cp source.txt destination.txt
Copy directory recursively:
cp -r source_directory destination_directory
Move or rename files
Move file:
mv file.txt /tmp/file.txt
Rename file:
mv oldname.txt newname.txt
Remove files
rm file.txt
Remove directory and contents:
rm -r directory
Force remove:
rm -rf directory
Be very careful with rm -rf.
View file content
Show full file:
cat file.txt
Show one page at a time:
less file.txt
Show first lines:
head file.txt
Show last lines:
tail file.txt
Show last 100 lines:
tail -n 100 file.txt
Follow file live:
tail -f file.txt
Search text in files
Search in one file:
grep "text" file.txt
Case-insensitive:
grep -i "text" file.txt
Search recursively:
grep -R "text" /path
Show line numbers:
grep -n "text" file.txt
Find files
Find by name:
find /path -name "filename"
Example:
find /etc -name "*.conf"
Find directories:
find /path -type d -name "directory_name"
Find files:
find /path -type f -name "*.log"
Command history
Show command history:
history
Search history:
history | grep "text"
Repeat previous command:
!!
Run previous command with sudo:
sudo !!
Pipes
A pipe sends output from one command into another command.
Example:
ps aux | grep ssh
This means:
show processes
send output to grep
filter lines containing ssh
Redirect output
Save output to a file:
command > file.txt
Append output to a file:
command >> file.txt
Redirect errors too:
command > file.txt 2>&1
Useful shortcuts
Ctrl + C = stop current command
Ctrl + L = clear screen
Ctrl + R = search command history
Tab = autocomplete
Up Arrow = previous command
Check command location
which COMMAND
Example:
which ssh
More detail:
type COMMAND
Example:
type cd
Manual pages
Open manual page:
man COMMAND
Example:
man ls
Search inside man:
/type_search_here
Quit:
q
Help option
Many commands support:
COMMAND --help
Example:
ls --help
Common first commands
pwd
ls -lah
cd /path
cat file.txt
less file.txt
tail -n 100 file.txt
grep "error" file.txt
find /path -name "file"
history
Dangerous commands
Be careful with:
rm -rf
chmod -R
chown -R
dd
mkfs
wipefs
fdisk
parted
reboot
shutdown
systemctl restart
Always check the path before running destructive commands.