Show Menu
Cheatography

Linux Command Line Cheat Sheet by

A complete guide to Linux command Line and it can be used as a reference for both beginners and advanced level users

shell prompt

#
Super User
$
normal user

simple commands

date
display current date and time
cal
display current month calendar
df
displays disk usages
free
display memory usage

Navigation

cd
change the current diretcory
ls
list the directory
cd - : change to previous working directory
cd ~username : change to home directory

Exploring Filesystem

file
Determine file type
less
view file contents
ls
-l diplay in long format
-h human readable
-a list all files even those hidden,
-d directory
-F option will append an indictor charactor to the end of listed names
-r display result in reverse order
-S sort by file Size
-t modifi­cation time

Manipu­lating Files and Direct­ories

cp
copy the files
mv
move the files from one folder to other
mkdir
creates a directory
rm
remove files and direct­ories
ln
create Hard and symbolic link
* any character
? any single character
[ character] any character that is a member of character
[ ! character] any character that is not a member of character
[[:cla­ss:]] any character that is a member of class

command Options : cp, mv

i
intera­ctive
same in mv
a
copy files and direct­ories of all attrib­utes, ownership and permission
-
r
recurs­ively copies direct­ories and contents
same in mv
u
copy the files that don't exists
same in mv
v
verbose, inform­ative message
same in mv

Command Substi­tution

echo $(ls)
ls -l $(which cp)
new method
ls -l which cp
back tick, old method

Process

ps
reports snap shot of current process
top
diskplays task
jobs
list active jobs
bg
place a job in the background
fg
place a job in the foreground
kill
send a signal to process
killall
kill process by name

Process commands

ps aux
BSD style ouptut
fg %1
jobs
kill -1 13456
kill process id 13456
SIGNALS: HUP, INT, KILL, TERM,C­ONT­,ST­OP,­QUIT, SEGV, TSTP. WINCH

Find command

cmin
match attrib­ute­s/f­ile­s/dir modified, n minutes
cnewer
match file/dir whose content or attribute were last modified n*24 ago
ctime n
match file or direct whose content or attribute were modified n*24 hrs ago
empty
match empty files and direct­ories
group name
match files or direc belonging to group name
iname
pattern like the iname test but case insens­itive
inum n
match files with inode number
mmin n
match files or dir whose content were modified n minutes ago
mtime n
match files or dir whose content were modified n*24 hours ago
name pattern
match files and dir with specified wildcard pattern
nouser
match files and dir that don't belong to valid user
nogroup
match files and dir that don't belong to valid group
perm mode
match files and direct­ories set to specified mode
samefile name
matches files that share the same inode number as file name
size n
match file size of n
type c
match file type of c
user
match file and direct­ories belong to user
find %test %action %options
find ~
find ~ | wc -l
find ~ -type d | wc -l
------­---­---­---­---­---­-------

b block
c character special device
d directory
f regular file
l symbolic link

Find Logical Operator

and
or
not
find ~ \( -type f -not -perm 0600 \) -or \( -type d -not -perm -700 \)

find -actions

delete
ls
print
quite
find ~ -type f -name '*.BAR] -print
find ~ -type f -and -name '*.BAR -and -print

Userde­fined actions
-exec comman{}

find ~ -type f -name 'foo*'­ -exec ls -l '{}' +
find ~ -type f -name 'foo*'­ -exec ls -l '{}' ';'

Find Options - scope

depth
to process a directory files before the dir itself
maxdepth
max num of level that find will descend into a directory when performing test and action
mindepth
min num of level that find will descend into a directory when performing test and action
mount
direct find not to traverse direct­ories that mounter on other filesystem
 

Tricks

clear
clears the screen
history
stores the history
script file
capture all command execution in a file
history: CTRL + {R, P }
!!number : command history number
!! : last command
!?string : history containing last string
!string : history containing last string
export HISTCO­NTR­OL=­ign­oredups
export HISTSI­ZE=­10000

Working With Commands

type
Indicate how command name is interp­reted
which
display which executable program will be executed
man
Display manual page
apropos
Display approp­riate command
info
Display command Info entry
whatis
Display brief descri­ption of command
alias
create an alias for command
 

man page options

1
User commands
2
Progra­mming interface for system calls
3
Progra­mming interface for C Library
4
Special files such devies nodes and drivers
5
file formats
6
Games and screen savers
7
misc
8
system admini­strator commands

Redire­ction

cat
concat­enates files
sort
sort the file
uniq
report or omit repeated lines
grep
print lines matching pattern
head
prints first few lines of file
tail
prints las few lines of file
tee
reads stdin and send output to stdout and file
wc
count number of line, words, and bytes

Redire­ction Operator

ls -l /usr/bin >file
default stdout to file
ls -l /usr/bin 2>file
redirects stderr to file
ls -l /usr/bin > ls-output 2>&1
redirects stderr & stdout to file
ls -l /usr/bin &> ls-output
redirects stderr & stdout to file
ls -l /usr/bin 2> /dev/null
/dev/null bitbucket

Pipelines

|
pipelines
ls -l /usr/bin/ | tee out | sort
tee reads stdin and writes to files and stdout

view the world as SHELL viewed

echo *
expands all files in the path
echo D*
expands all files with starting D
$((exp­res­sion))
Arithe­matic expression
echo $(($((5*2))3))
75
ls -l which cp
backtick instead of expression

Braces Expansion

echo Front-­{A,­B,C­}-Back
Front-­A-Back, Front-­B-Back, Front-­C-Back
echo {Z..A}
Z Y X W V U .....A
mkdir {2009..20­11}­-0{­1..9} {2009..20­11}­-{1­0..12}
creates a directory for 12 months in 2009 to 2011

Enviro­nment

printenv
Print part of all of the enviro­nment
set
set shell options
export
export enviro­nment to subseq­uently executed programs
alias
create an alias for command

Login & Non Login Shell

Login Shells :
/etc/p­rofile : global config­uration
~/.bas­h_p­rofile : personal startup efile
~/.bas­h_l­ogin. : if ~/.bas­h_p­rofile is not found, bash attempts to read this script
~/.profile : if neither ~/.bas­h_p­rofile & ~/.bas­h_login bash reads this file

Non Login shells :

/etc/b­ash.bashrc : Global config­uration
~/.bashrc : User config­uration

Quoting

echo This is a test
This is a test; space is stripped
echo "This is a test"
This is a test
echo '$(echo foo) $((2+2))'
$(echo foo) $((2+2))
echo the total is $500.00
$5 is undefined variable so it supress the value, output will be like the total is 00.00
" " : all special character looses its meaning, exception ($, \, `)
' ' : all special character looses its meaning, and no exceptions

Searching For Files

locate
find files by name
find
search for files in a dir
xargs
build and exec cmd lines from stdin
touch
change the file times
stat
display file or filesystem status

Networking

Ping
Send ICMP packets
traceroute
Print route packets to a network
netstat
print network connec­tion, routing table, interface stats
ftp/lftp
Internet file transfer program
wget
Non Intera­ctive network downloader
ssh
OpenSSH SSH Client (remote login program)
scp
secure copy
sftp
Secure File transfer program
OpenSSH package includes two programs that can make use of an SSH encrypted tunnel to copy file across the network.
a) scp
b) sftp

SFTP doesn't require ftp server to connect, it needs only SSH running in machines, any machine that has ssh running, we can able to transfer files to server

Archiving and Backup

gzip
Compress or expand files
bzip2
A block sorting file compressor
tap
tape archving utility
zip
Package and compress files
rsync
remote file and dir synchr­oni­zation
unzip
Unzip the gzip files
compre­ssion algorithm: Lossy and Lossless

Hacks

Examples:

ssh remote-sys 'tar cf - Document' | tar xf -
find playground -name 'file-A' | tar czf playgr­oun­d.tgz -T -
find playground -name 'file-A' | tar cjf playgr­oun­d.tgz -T -

rsync -av --delete --rsh=ssh /etc /home /usr/local remote­-sy­s:/­backup
- or Hyphen is the stdin and stdout

Text processing examples

cat -A $FILE
To find any CTRL character introduced
sort file1.txt file2.txt file3.txt > final_­sor­ted­_li­st.txt
sort all files once
ls - l | sort -nr -k 5
key field 5th column
sort --key=1,1 --key=2n distor.txt
key field 1,1 sort and second column sort by numeric
sort foo.txt | uniq -c
to find repetition
cut -f 3 distro.txt
cut column 3
cut -c 7-10
cut character 7 - 10
cut -d ':' -f 1 /etc/p­assword
delimiter :
sort -k 3.7nbr -k 3.1nbr -k 3.4nbr distro.txt
3 rd field 7 the character, 3rd field 1 character
paste file1.txt file2.txt > newfil­e.txt
merge two files
join file1.txt file2.txt
join on common two fields

Text processing

cat
concat­enate files and print stdout
sort
sort lines of text files
uniq
report or omite repeated lines
cut
remove section from each line of files
paste
merge lines of files
join
join lines of two files on a common field
comm
compare two sorted files line by line
diff
compare files
patch
apply a diff file to original
tr
translate
sed
stream editor
aspel
intera­ctive spell checker
 

Permis­sions

id
display user identity
chmod
change's file mode
umask
set the default file permission
su
Run a shell as another user
sudo
Execute command as another user
chown
changes file's owner
chgrp
change file groups owner
passwd
change a user password

Read,W­rite, Execute

- | rwx | rw- | r--
type | owner | group | world
type
- regular file
d directory
l symbolic link
c character device
b block device

File Mode [Octal]

000
---
001
--x
010
-w-
011
-wx
100
r--
101
r-x
110
rw-
111
rwx
chmod 0600 file [ rw- is set for owner ]

Mode symbolic notation

ugo
user, group, others
u+x
giving exeute permission for user
u=rw,o=x
giveing user read write and others execute perm
chmod u=rw,o=x file.txt

Packaging Ssystem

Debian Style (.dep)
Debain, ubuntu, xandros, Linspire
RedHat style(.rpm)
Fedora, CentOS, Redhat Enterp­rise, OpenSUSE, Mandriva, PCLinuxOS

Install from Repo

debian
apt-get install packag­e_name
redhat
yum install packag­e_name

Operation on Package File

Debian :

dpkg --install packag­efile
apt-get remove packag­e_name
apt-get update
dpkg --list
dpkg --status packag­e_name
apt-cache show packag­e_name (info about package)
dpkg --search file_name (finding which package installed)

Redhat :

rpm -i packag­efile
yum erase packag­e_name
yum update
rpm -u packag­efile
rpm -qa. (list)
rpm -q packag­e_name (check package installed)
yum info packag­e_name
rpm -qf file_name (finding which package installed)

storage media

mount
mount a filesystem
ummount
unmount a file system
fdisk
partition table manipu­lator
fsck
check and repair filesystem
fdformat
format floppy disk
mkfs
create a file system
dd
write a block oriented data directly to a device
geniso­image
create an ISO 9660 image file
wodim
Write data to optical storage device
md5sum
calculate md5sum
dd if=/de­v/cdrom of=ubu­ntu.iso (create image file)
fsck /dev/sdb1 (check file system)
mkfs -t ext3 /dev/sdb1 (create ext3 file system)
unmount /dev/sdb1 (unmount the file system, before changing the partition table)
fdisk /dev/sdb (create a partition table )

POSIX charcter class

character class
alnum, word , alpha blank, cntrl, digit, graph,­lowe, punct, print, space, upper, xdigit
format: [ :alnum: ]
echo $LANG
export LANG=POSIX

grep Options

i
ignore case
v
invert match
c
print number of matches
l
print the name of each file that contain matches
L
print only the names of files that don't match
n
print match line with the number
h
for multiple output supress the output of filename
grep [options] regex [file....]

metach­ara­cters : ^ $ . [ ] {} - ? * + ( ) | \

grep examples

grep -h '.zip' file.list
. is any character
grep -h '^zip' file.list
starts with zip
grep -h 'zip$' file.list
ends with zip
grep -h '^zip$' file.list
containing only zip
grep -h '[^bz]zip' file.list
not containing b and z
grep -h '^[A-Z­a-z­0-9]' file.list
file containing any valid names

grep Quanti­fiers

?
match element zero or one time
*
match an element zero or more times
+
Match an element one or more times
{}
match an element specfic number of times
It always match the preceding element
{n} match the preceding element occurs n times
{n,m} match the preceding element occurs at least n times, but no more than m times
{n,} match the preceding element if it occurs n or more times
{,m} match the preceding element if it occurs no more than m times

grep hacks

grep -E '^\([0­-9]­{3}­\).[­0-­9]{­3}.[­0-­9]{3}$' phone.txt

locate --regex 'bin/(­bz|­gz|­zip)'
for i in {1..10}; do echo "­(${­RAN­DOM­:0:­3})­-${­RAN­DOM­:0:­3}-­${R­AND­OM:­0:3­}"; done >>p­hon­e.txt

Text Processing Hacks

diff -c
context format
diff -u
unified format
diff -Naur oldfile newfile > diff_file
write the diff to diff file
patch < diff_file
applied to oldfile directly.
echo "­secret text" | tr a-zA-Z n-za-m­N-ZA-M
frperg grkg | ROT13 Encoding
echo "­frperg grkg" | tr a-zA-Z n-za-m­N-ZA-M
secret text | ROT13 Decoding
-, +, ! : deleted, added, line changes.

$diff -Naur file1.txt file2.txt > diff_f­ile.txt
$patch < diff_f­ile.txt
patching file file1.txt

rotate by 13 places (ROT13)
       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Linux Command Line Cheat Sheet
          bash Shortcuts Cheat Sheet

          More Cheat Sheets by Sathyanarayanan

          Perl shortcuts Cheat Sheet