Linux Commands Cheat Sheet

Quick reference Linux commands you should know.

Basic Linux commands
ls
Lists all files and directories in the present working directory
ls -R
Lists files in sub-directories as well
ls -a
Lists hidden files as well
ls -al
Lists files and directories with detailed information like permissions,size, owner, etc.
cd or cd ~
Navigate to HOME directory
cd ..
Move one level up
cd
To change to a particular directory
cd /
Move to the root directory
cat > filename
Creates a new file
cat filename
Displays the file content
cat file1 file2 > file3
Joins two files (file1, file2) and stores the output in a new file (file3)
mv file "new file path"
Moves the files to the new location
mv filename new_file_name
Renames the file to a new filename
sudo
Allows regular users to run programs with the security privileges of the superuser or root
rm filename
Deletes a file
man
Gives help information on a command
history
Gives a list of all past commands typed in the current terminal session
clear
Clears the terminal
mkdir directoryname
Creates a new directory in the present working directory or a at the specified path
rmdir
Deletes a directory
mv
Renames a directory
pr -x
Divides the file into x columns
pr -h
Assigns a header to the file
pr -n
Denotes the file with Line Numbers
lp -nc , lpr c
Prints “c” copies of the File
lp-d lp-P
Specifies name of the printer
apt-get
Command used to install and update packages
mail -s 'subject'
-c 'cc-address'
-b 'bcc-address'
'to-address'
Command to send email
mail -s "Subject"
to-address < Filename
Command to send email with attachment
File Permission commands
ls -l
to show file type and access permission
r
read permission
w
write permission
x
execute permission
-=
no permission
Chown user
For changing the ownership of a file/directory
Chown user:group filename
change the user as well as group for a file or directory
File Permission commands
ls -l
to show file type and access permission
r
read permission
w
write permission
x
execute permission
-=
no permission
Chown user
For changing the ownership of a file/directory
Chown user:group filename
change the user as well as group for a file or directory
Bash Commands
uname -a
Show system and kernel
head -n1 /et c/issue
Show distri­bution
mount
Show mounted filesy­stems
date
Show system date
uptime
Show uptime
whoami
Show your username
man command
Show manual for command
Bash Shortcuts
CTRL-c
Stop current command
CTRL-z
Sleep program
CTRL-a
Go to start of line
CTRL-e
Go to end of line
CTRL-u
Cut from start of line
CTRL-k
Cut to end of line
CTRL-r
Search history
!!
Repeat last command
!abc
Run last command starting with abc
!abc:p
Print last command starting with abc
!$
Last argument of previous command
ALT-.
Last argument of previous command
!*
All arguments of previous command
^abc^123
Run previous command, replacing abc with 123
Bash Variables
env
Show enviro­nment variables
echo $NAME
Output value of $NAME variable
export NAME=value
Set $NAME to value
$PATH
Executable search path
$HOME
Home directory
$SHELL
Current shell
IO Redire­ction
cmd < file
Input of cmd from file
cmd1 <(cmd2)
Output of cmd2 as file input to cmd1
cmd > file
Standard output (stdout) of cmd to file
cmd > /dev/null
Discard stdout of cmd
cmd >> file
Append stdout to file
cmd 2> file
Error output (stderr) of cmd to file
cmd 1>&2
stdout to same place as stderr
cmd 2>&1
stderr to same place as stdout
cmd &> file
Every output of cmd to file
cmd
refers to a command.
Pipes
cmd1 | cmd2
stdout of cmd1 to cmd2
cmd1 |& cmd2
stderr of cmd1 to cmd2
Command Lists
cmd1 ; cmd2
Run cmd1 then cmd2
cmd1 && cmd2
Run cmd2 if cmd1 is successful
cmd1 || cmd2
Run cmd2 if cmd1 is not successful
cmd &
Run cmd in a subshell
Directory Operations
pwd
Show current directory
mkdir dir
Make directory dir
cd dir
Change directory to dir
cd ..
Go up a directory
ls
List files
ls Options
-a
Show all (including hidden)
-R
Recursive list
-r
Reverse order
-t
Sort by last modified
-S
Sort by file size
-l
Long listing format
-1
One file per line
-m
Comma-­sep­arated output
-Q
Quoted output
Search Files
grep pattern files
Search for pattern in files
grep -i
Case insens­itive search
grep -r
Recursive search
grep -v
Inverted search
grep -o
Show matched part of file only
find /dir/ -name name*
Find files starting with name in dir
find /dir/ -user name
Find files owned by name in dir
find /dir/ -mmin num
Find files modifed less than num minutes ago in dir
whereis command
Find binary / source / manual for command
locate file
Find file (quick search of system index)
File Operations
touch file1
Create file1
cat file1 file2
Concat­enate files and output
less file1
View and paginate file1
file file1
Get type of file1
cp file1 file2
Copy file1 to file2
mv file1 file2
Move file1 to file2
rm file1
Delete file1
head file1
Show first 10 lines of file1
tail file1
Show last 10 lines of file1
tail -F file1
Output last lines of file1 as it changes
Watch a Command
watch -n 5 'ntpq -p'
Issue the 'ntpq -p' command every 5 seconds and display output
Process Management
ps
Show snapshot of processes
ps -ef
Show all the currently running processes
top
Show real time processes
htop
Interactive process viewer (top alternative)
kill pid
Kill process with id pid
pkill name
Kill process with name name
killall name
Kill all processes with names beginning name
bg
Display stopped or background jobs
fg
Brings the most recent background job to foreground
fg n
Brings job n to the foreground
Nano Shortcuts
Files
Ctrl-R
Read file
Ctrl-O
Save file
Ctrl-X
Close file
Cut and Paste
ALT-A
Start marking text
CTRL-K
Cut marked text or line
CTRL-U
Paste text
Navigate File
ALT-/
End of file
CTRL-A
Beginning of line
CTRL-E
End of line
CTRL-C
Show line number
CTRL-_
Go to line number
Search File
CTRL-W
Find
ALT-W
Find next
CTRL-\
Search and replace
Screen Shortcuts
screen
Start a screen session.
screen -r
Resume a screen session.
screen -list
Show your current screen sessions.
CTRL-A
Activate commands for screen.
CTRL-A c
Create a new instance of terminal.
CTRL-A n
Go to the next instance of terminal.
CTRL-A p
Go to the previous instance of terminal.
CTRL-A "
Show current instances of terminals.
CTRL-A A
Rename the current instance.
File Permis­sions
chmod 775 file
Change mode of file to 775
chmod -R 600 folder
Recurs­ively chmod folder to 600
chown user:group file
Change file owner to user and group to group
File Permission Numbers
First digit is owner permis­sion, second is group and third is everyone. Calculate permission digits by adding numbers below.
4
read (r)
2
write (w)
1
execute (x)
Try These Related Tools



If you think technology can solve your security problems, then you don’t understand the problems and you don’t understand the technology.

Bruce Schneier


Contribute to the Growth

If you've found our tools helpful, please consider fueling our mission by buying us a coffee. Your support ensures the continuous operation of CodersTool, allowing us to keep our tools accessible to all, free of charge.

Buy Us A Coffee