Linux command-line cheatsheet

Using help

apropos topic_name
Show list of manual pages related to topic
man topic_name
Show manual page for specified topic
info topic_name
Show info page for specified topic (sometimes easier to read than man)
whatis command_name
One-line summary for command
whereis command_name
Displays information where binary command is located; alternatively: "type command_name"

Working with filesystem

ls
List current directory content, options: -l (detailed), -l - h (detailed with size in kB/MB), -l -S (detailed sorted by size), -a (plus hidden files whose names starts with dot), -t (sort by access time)
List current directory content, detailed
ls -l | more
List current directory content, detailed, piping output to more
du -hs /usr
Get /usr directory size
pwd
Get full path of current directory
cd
Change directory, i.e. cd /home/tomeko, cd .. to go one level up, cd without parameters moves to current user home directory, "cd -": switches between current and last directory; if path contains space use quotes, doublequotes or escape each space with backslash
mkdir
Make directory; mkdir -p path creates also all needed upper level directories needed
rmdir
Remove directory; rm -r for recursive, rm -rf for recursive with no confirmation
rm
Another way of removing files and directories, switch -r for recursive, -f for force, -i for interactive
cp source destination
Copy files; use * to match all; options: -a (archive, copy file properties as well), -p (preverse, copy file owners and permissions), -r (recursive), -u (update: copy only if destination is older or does not exist)
mv source destination
Move (or rename) files
touch filename
Update access and modification of specified file to current; if file does not exist new file is created (unless -c option is used)

File-matching metacharacters

*
matches any number of characters
?
matches single character
[...]
Matches any one of the characters between the brackets; may include dash-separated range of letters or numbers; use exclamation for negation
~
Replaces user home directory

File ownership and permissions

Every file and directory has an owner. To determine user permissions to access file system checks first if user is file owner. In this case user gets permissions specified for file owner. If not, it checks if user is a member of group owner. Otherwise permissions for "other" users are used. File/directory ownership can be listed with ls -l. Newly created file group ownership is the same as user primary group, type groups to get list of groups user belongs to.

chown username filename
Change file ownership
chown -R username directory
Change ownership recursively
chgrp groupname filename
Change group ownership
chmod 751 filename
Change permissions to read+write+execute for user (4+2+1), read+execute (4+1) for group and execute (1) for others
chmod -R 777 directory
Change permissions recursively
chmod +x filename
Add execute permission for all users
chmod g+w filename
Add write permission for group; multiple changes can be combined with colons, i.e. chmod g+w,o-r,u+x filename

Searching for files

find /var/log -name "*log*"
Find files with names containing "log" in /var/log
find /usr/sbin -executable
Find executables
find /sbin -executable -size +100k
Find executables larger than 100 kB; all numeric arguments can be specified as +n (more than), -n (less than) or n (exact value)
find /var/log -mmin -10
Find files that were modified less than 10 minutes ago
find /var/log -amin -100
Find files that were accessed less than 100 minutes ago
locate file_pattern
Search tool working with indexed database - may be much faster than find

Working with archives

tar -czvf home.tar.gz /home
Create (c) zipped (z) file (f) with verbose output (v); more than one directory can be listed after space; option -C before source would cause storing paths as relative
tar -xzvf ./home.tar.gz
Extracts archive to current directory
tar -xvf /file.tar -C /dest_dir
Extracts archive to specified directory

Creating whole device (raw) backups

dd if=/dev/cdrom of=/image.iso
Create device image
mount -o loop /image.iso /mnt
Mount device image

Symbolic and hard links

ln -s file symlink
Create symbolic link; symbolic link will fail if original link would be removed
ln file hardlink
Create hard link; hard link behaves like a copy of the file that is synchronized continuously

Process management

ps aux
Process list
ps aux | grep keyword
Process list with filter
pstree
Process tree
top
Process monitor; use top -u username to limit to only specified user processes; Shift+M to sort by memory usage, Shift+P to sort by CPU usage, k to kill process with specified signal (15 = SIGTERM, 9 = SIGKILL), q to quit
kill [-s signal] process_pid
Kill process with specified PID; by default uses signal 15, SIGKILL
program_name &
Start program in background
jobs
Get the list of programs running in background
fg job_number
Bring job to foreground
bg job_number
Resume (stopped with Ctrl+Z) job in background
kill %job_number
Kill process related to job number

Network

netstat -platune
Information about current network connections, ports listening and programs involved
nmap ip_address
Port scan

Kernel

lsmod
List currently loaded kernel modules
modinfo module_name
Detailed module informations
modprobe module_name
Load module (-v for verbose, -f to force ignoring kernel version)
modprobe -r module_name
Unload module
dmesg
Display kernel message log

Other usefull commands

env
List environment variables
history
Recently used commands (not necessary this session only); add number to limit to N recent entries
!command_number
Execute command from history from specified position
!cd
Execute last "cd" command instance from history
!?string?
Run again last command containing specified string
passwd
Change password for the current user (also applies to root)
su [username]
Temporary change your identity; type exit to go back to your account
shutdown -r now
Reboot computer
shutdown -h now
Shut down (halt)
w
Get list of currently logged users and their activity
clear
Clear screen
ldd executable_name
Show shared library dependencies for specified executable (required system libraries)
lsof
List open files; lsof -u username to limit to specified user
uname -a
Shows basic informations about kernel, processor, current user, helps to identify distro
whoami
Display current user account name
last
Recent logins, reboots and shutdowns
date
Shows current date and time
cal
Display calendar (whole year if without arguments)
man ascii
Displays ASCII table
ssh username@ip_address
Initiate ssh session
scp filename username@ip_address:/home/my_path
Transfer file to remote computer (-r for recursive directory copying)

Some shortcuts

Ctrl+Alt+F1 ... Ctrl+Alt+F6
Switching to tty1 ... tty6
Ctrl+Alt+F7
Switching back to graphical environment
Tab, double Tab
While typing command: file/directory name autocompletion

Vi(m): text editor

Pretty non-intuitive command line editor. Use nano if available.

vi filename
Create new or open existing file for editing
Esc
switching from input mode to command mode
i
switching from command mode to input mode
:w newfile (command mode)
Save file with new name
:q!
Quit without saving
:w
Save file
:wq!
Save current file and exit
u
Undo last changes
G
Go to the bottom of the file

Displaying text file content

cat filename
Plain lister that just dumps content on the screen
cat filename
Plain lister; tac displays lines in reverse order
cat filename | grep keyword
Show file lines containing specified keyword
tail -f /var/log/syslog
Shows last lines (10 by default) of specified file and waits for file updates
head /var/log/syslog
Shows first lines (10 by default) of specified file
less /var/log/syslog
Lister with browsing capabilities. Type :q (similar to vim) to exit.
diff --side-by-side file1 file2
Shows diff in human-readable form

Common directories

/bin
Essential binaries available to all users
/sbin
Binaries for the system administrator (i.e. fdisk)
/boot
Kernel image (vmlinuz) and other vital components
/dev
Files corresponding to system hardware
/etc
Configuration files
/home
Users personal files. Current user home directory can be referred to with shortcut ~
/mnt and/or /media
Mounted data storage
/usr
Most all-user accessible files; most programs are stored in /usr/bin
/opt
Used as directory for complex software packages (i.e. office suite)
/proc
Interface to system memory and some system features, current state of the kernel, i.e. /proc/cpuinfo (print processor specification: cat /proc/cpuinfo), /proc/meminfo, /proc/net/sockstat, /proc/sys/fs/file-max, /proc/process_PID_number
/root
Used as root home directory (instead of /home)
/srv
Important services like HTTP or FTP and their files
/sys
Directory similar to /proc, but stored directly on disk
/tmp
Directory where every user can write to
/var
i.e. print spool

 "Cookie monsters": 2935528    Parse time: 0.000 s