Linux Cheatsheet

My personal compilation of Linux cheatsheet throughout the years. I am putting up here so I can easily access whenever I need to recall.

>>

print output to target file

Examples list files and output to a txt file

# ls -al >> ls_log.txt

 

| (pipeline)

command combinations using pipe

Examples creating a long list of files and piping the output into the more command

# ls -al | more
get a list of all sub-directories in the current directory, using grep to show the file records that begin with the letter d
# ls -al | grep '^d'
list all the processes running that include the string 'test'
# ps auxwww | grep test | more
how many processes are currently running on the system
# ps aux | wc -l

 

./program name

Run an executable in the current directory

Examples

./coldfusion

 

[Tab]

autocompletion function

Examples

Tab

 

[ctrl] + [c]

terminate the process (kill pid)

Examples

ctrl+c

 

[ctrl] + [z]

suspend the process (suspend pid)

Examples

ctrl+z

 

[ctrl] + [a]

go to beginning of line

Examples

ctrl+a

 

[ctrl] + [e]

go to end of line

Examples

ctrl+e

 

[shift] + [delete]

copy from terminal

Examples

shift+delete

 

[shift] + [insert]

paste from terminal

Examples

shift+insert

 

[shift] + [PageUp]

scroll terminal output up

Examples

shift+PageUp

 

alias

create a 'shortcut' to a command

Examples create an alias called home which will put you in the /home/test/public_html directory

# alias home='cd /home/test/public_html'
to see a list of aliases set up
# alias

 

cat

displays the contents of a file to screen. It can also display multiple files concatenated together, and using the shell, its standard output can be redirected from the screen to the file.

Useful options -n :line numbering
-s :squeeze sequences of blank lines into one
-v :show non-printing characters

Examples
displays content of example.txt

# cat example.txt
merge 2 files into 1 file
# cat file_1 file_2 > file_3
merge 1 file into another one
# cat file_2 >> file_1

 

cd

changes the current directory.

Examples
change to web directory

# cd web
change to home directory
# cd
change back 1 directory
# cd ..

 

clear

clear screen

Examples

# clear

 

cp

copies a file

Useful options
-r : copies directories (recursively)
-p : preserves permissions, ownership, and timestamps
-i : prompt before overwrite
-v : verbose, show filenames as they are being copied
-f : copies directories (force)

Examples
copy test.txt to /home/user/ directory

# cp test.txt /home/user/
copy test.txt to /home/user/ directory, force
# cp -f test.txt /home/user/
copy of directory 'mydir' (and all its contents) and put it at /tmp
# cp -ir mydir/ /tmp

 

chmod

changes permissions of files. 3 permissions: read(r), write(w) & execute(x). Sets of permissions for the owning user of the file(u), for the group of the file(g) & for other users(o).

Useful options
-R : recursively change or set permissions on an entire directory tree

Examples
chmod usr|grp|othr operator permission (chmod u=rw o=x file)
chmod test.html 777 (rwx rwx rwx)

 

chown

changes the owner and group of files.

Useful options
-R : recursively change owner/group on an entire directory tree
-h : do not follow symbolic links i.e. changes owner of the link, not the target file
-f : indicate no errors if change failed

Examples
change the ownership to user root, group web.

# chown root:web /etc/passwd /etc/shadow
change the ownership including files in subdirectories
# chown -R root:web /etc/ssh
The same, but excluding files in directories (and the invisible files)
# chown root:web /etc/ssh/*

 

date

displays date and time.

Examples

# date

 

df

reports the amount of free disk space available on each partition.

Useful options
-h : reports disk usage in human readable format with block-sizes in Kilo,Mega,Gigabytes.

Examples

# df
# df -h

 

find

search the entire filesystem for one or more files that you specify. It's an extremely helpful Linux command to understand, especially when you need to find a file or directory

Examples
find a file or directory named 'test' somewhere below your current directory

# find . -name test
similar to the top but with wildcard
# find . -name "tes*"
find a file type instead of directory
# find . -type f -name "tes*"
find a directory type instead of file type
# find . -type d -name "tes*"
find tes in multiple directories
# find folder1 folder2 folder3 -type f -name "tes*"
find recently changed file, specify time in 24-hour period
# find . -mtime -30
find and delete target files
# find . -name xxx -exec rm {} \
find and change chmod 664 for all the files in a folder in or below 'test'
# find test/ -type f -exec chmod 664 {} \;
find and change chmod 775 for all the folders in or below 'test'
# find test/ -type d -exec chmod 775 {} \;
find and change chmod 644 for all files in a folder, 1 level deep
# find test/ -maxdepth 1 -type d -exec chmod 775 {} \;
Find all files in or below test directory and use a case-insensitive search to look for the string 'find me' in each those files
# find /test -type f -exec grep -il "find me" {} \;
find through the root filesystem (/) for the file named "chapter1" then print to the screen
# find / -name Chapter1 -type f
find extension .html and change permission to 644
# find /usr/local -name "*.html" -type f -exec chmod 644 {} \;
take 1 file and copy it to a whole bunch of other directories
# find dir1 dir2 dir3 dir4 -type d -exec cp test.html {} \;

 

finger

display the user's login name, real name, login time, ect ...

Useful options
-l : print all the information described by -s option
-m : match arguments only on user name
-p : supress the printing format of -l
-s : print output in short format

Examples

# finger -s user

 

free

This command displays statistics on memory usage. The values represent KB's.

Examples

# free

 

grep

uses to search for text strings and regular expressions within one or more files

Examples search for 'testuser' in the /etc/passwd

# grep 'testuser' /etc/passwd
search for testuser using regular expression
# grep '^testuser' /etc/passwd
search for joe within all files of the current directory; * wildcard matches all files in the current directory
# grep 'joe' *
case-insensitive searching with grep
# grep -i test text.txt
show all the Apache httpd processes running on Linux system
# ps auxwww | grep httpd
reversing the meaning of a search, show all the lines that don't contain the string testuser
# grep -v testuser /etc/passwd

 

head

writes the first ten lines of a file to the screen

Examples

# head xxx.txt

 

id

prints user group privileges

Examples
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)

# id

 

iptables

configure ip table. Entries are executed in order so entries at the bottom should be restrictions, entries at the top should be exceptions

Useful options
-L : limits to INPUT, OUTPUT, FORWARD, ect...
-n : shows numbers, not domains & port names
--line-numbers : the line number of the rule (allows you to delete it)
-A : add at the end of the defined table: INPUT, OUTPUT, FORWARD, etc.
-l : inserts at the beginning of the defined table
-p : Source IP
-sport : Source port(s) (optional)
-j : Action (ACCEPT, REJECT, DROP, etc.)

Examples
add basic entry

# iptables -I INPUT -s xxx.xxx.xxx.xxx -p tcp --dport ftp -j ACCEPT
a more detailed entry
# iptables -A INPUT -s xxx.xx.xxx.xxx -p tcp --sport 123:5656 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
delete entry, will remove the corresponding entry independent of location. The rules must match exactly.
# iptables -D INPUT -s xxx.x.xxx.xxx -p tcp --sport 123:5656 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
delete entry by rule number, this will remove rule 2. use iptables -L INPUT --line-numbers to see the line numbers for deleting an INPUT entry
# iptables –D INPUT 2
Drop ip address
# iptables -A INPUT -s xxx.xxx.xx.xxx -p ALL -j DROP
List ip to verify ip has been dropped
# iptables –L –n
save iptable after entry
# iptables-save > /etc/sysconfig/iptables

 

kill

terminate for example the emacs process, you would need to look up the process identifier (PID) in the table using command ps ux

Examples
-9 will ensure execution

# kill -9 3216

 

last

uses to display the last logged in users list. Last logged in users informations are read from the file /var/log/wtmp.

Useful options
-n : specify how many lines to show
-R : suppresses the display of the hostname field
-a : display the hostname in the last column

Examples
display all the last logged in users list

# last
display the last 5 logged in users list
# last -5

 

history

shows last commands

Useful options -5 : show last 5 commands

Examples displays up to 1000 commands

# history
displays last 5 commands
# history -5
displays commands page by page
# history | more

 

ls

for listing the files in a directory.

Useful options -a : all files (include files with . prefix)
-l : long detail (provide file statistics)
-t : order by creation time
-u : sort by access time (or show when last accessed together with -l)
-S : order by size
-r : reverse order
-s : show filesizes
-h : 'human readble'; show filesizes in kilobytes and megabytes (-h can be used together with -l or -s)

Examples
list long details

ls -l
list all files in long details and sort by time
ls -al --sort=t

 

locate

find indexed files

Examples find index.html

locate index.html
find index.html with case-insensitive search
locate -i test

 

more

display results one page at a time ([enter] moves on line down, [space] moves one page down)

Examples

# ls | more

 

rm

deletes a file from the filesystem

Useful options -f : force, ignore nonexistent files, never prompt
-i : interactive, prompt before any removal
-r, -R : recursive, remove the contents of directories recursively (the force option must often be used to successully run rm recursively)

Examples remove a file by force

rm -f test.txt
remove a folder by force
rm -f /home/user/test

mv

move or rename files/directories

Examples move log1.txt to /home/web folder

# mv log1.txt /home/web
rename a file
# mv log1.txt newlog.txt

 

passwd

changes passwords for user and group accounts. A normal user may only change the password for his/her own account, the super user may change the password for any account.

Examples
set or change user password

# passwd
change for any user password (must be login as a root)
# passwd user
change group password
# passwd -g web

 

ps

lists process status.

Useful options
-a : list the processes of all users on the system rather than just those of the current user
-u : provide detailed information about each process
-x : add to the list processes that have no controlling terminal, such as daemons, which are programs that are launched during booting
-e : generate a list of information about every process currently running
-f : generate a listing that contains fewer items of information for each process than the -l option
-l : generate a long listing

Examples
show all active processes

# ps -a
show all all active processes of all users, provide detailed information about each process, add to the list processes that have no controlling terminal, | less output 1 screenful at a time
# ps -aux | less
an alternative set of options for viewing all the processes running on a system
# ps -ef | less
find processes belonging to a user with username testuser
# ps -ef | grep testuser

 

pwd

print working directory

Examples

# pwd

 

su

change user from shell

Examples
change user

# su user

 

tar

archive, extract archive files, create compressed archives

Useful options
c : create archive
v : verbose which tells tar to print all the filenames as they are added to the archive
f : the name of the archive appears next (right after these options)
z : compress this archive with gzip
x : extract

Examples create an archive project of a subdirectory project

# tar cvf project.tar project
create a compressed archive of the current directory; '.' refers to the current directory
# tar czvf mydirectory.tgz .
to list the contents of an uncompressed tar archive but does not extract them
tar tvf mytar.tar
to list all the files in a compressed archive but does not extract them
# tar tzvf myarchive.tgz
to extract the contents of an archive for uncompressed
# tar xvf my-archive.tar
to extract the contents of compressed archives
tar xzvf my-archive.tar.gz

 

uptime

displays a one line summary showing current time, number of days since last boot, number of users logged in, system utilization for the last 1,5 &15

Examples

# uptime

 

useradd

adding a new user

Useful options
-d : home directory
-p : password
-g : primary group assigned to the user
-G : other groups the user belogs to
-m : create the user's home directory

Examples
to add a new user with primary group users, a second group sale, password test, home directory newuser, create home directory, a login name new user

# useradd -gusers -Gsale -ptest -d/home/newuser -m newuser

 

userdel

deleting a user

Useful options
-r : remove home directory

Examples to remove the user 'newuser' and home directory

# userdel -r newuser

usermod

modifying existing user

Useful options
-d : home directory
-p : password
-g : primary group assigned to the user
-G : other groups the user belogs to

Examples
to add the group 'others' to the user newuser

# usermod -Gothers newuser

 

w

show uptime and some users informations

Examples

# w

 

who/whoiam

who will display all users logged in, tty device, when they logged in, iddle time, where they logged in from; whoami will display the current user login

Useful options
-i : display idle time
-H : print column headings

Examples

# who

 

Latest News

8.08.2010
Migrated to Linux server

3.20.2010
Added Lightbox for YouTube Videos

 

2.22.2010
Added more Linux commands

 

2.6.2010
Added/updated content.

 

2.6.2010
Appied a simple, modern new template to the website.