There are thousands of linux commands available. But there are only a few commands we use for most of the time. As a result, we don’t have to learn and recite all the commands. Of course we can check the manuals by “man” or google it but it’s not a fast way for me to search them all the time. Therefore, I list them all together as a cheat sheet.
System info
1 2 3 4 5 6 7 8 9 10 11 12 13
date## show the current date and time cal ## show this month's calendar uptime## show current uptime w ## display who is online whoami## who you are logged in as finger user ## display information about user uname -a ## show kernel information man `command` ## show the manual for command df## show disk usage du## show directory free ## show memory and swap usage whereis app ## show possible location of app which app ## show which app will be run by default
Compression
Tar
For tar files only.
Option
Description
-c
Create new file
-t
List content of an archive
-x
Extract files from an archive. Used with -C to set destination path. Note: -c, -t, -x can’t be used in a single command
-j
Comparess or extract via bzip2
-z
Comparess or extract via gzip
-v
verbosely list files processed
-f filename
use filename as processed file
-C dir
Set directory for compression and extraction
Too many options? I agree. But you need these commands only:
1 2 3 4 5 6 7 8 9 10
tar -jcv -f filename.tar.bz2 `file or path` ## Compress tar -jtv -f filename.tar.bz2 ## list content tar -jxv -f filename.tar.bz2 -C `path` ## Extract
tar -cf file.tar files ## Create a tar named file.tar containing files tar -xf file.tar ## Extract the files from file.tar tar -czf file.tar.gz files ## Create a tar with Gzip compression tar -xzf file.tar.gz ## extract a tar using Gzip tar -cjf file.tar.bz2 files ## Create a tar with Bzip2 compression tar -xjf file.tar.bz2 ## extract a tar using Bzip2
zip
For all the .zip files.
1 2 3
sudo apt-get install unzip
unzip file.zip -d destination_folder
Process Management
1 2 3
ps ## display your currently active process top ## display all running processes kill `pid` ## kill process id pid
Details of PS:
Option
Description
-A
List all the processes
-a
List all the non terminal processes
-u
Display the processes belonging to the specified usernames
-x
It’s used with option a to show more detail information
-l
List details of PID
Network
1 2 3 4 5 6
ping `host` ## ping host and output results whois `domain` ## get whois information from domain dig `domain` ## get DNS information from domain dig -x `host` ## reverse lookup host wget `file` ## download file wget -c `file` ## continue a stopped download
Installation
Install from source:
1 2 3
./configure make make install
1 2
dpkg -i pkg.deb ## Install a package (Debian) rpm -Uvh pkg.rpm ## Install a package (RPM)
Searching
1 2 3 4
grep `pattern` `files` ## Search for pattern in files grep -r `pattern` `dir` ## Search recursively for pattern in dir command | grep pattern ## Search for pattern in the output of command locate `file` ## find all instances of file
SSH
1 2 3
ssh user@host ## connect to host as user ssh -p port user@host ## connect to host on port as user ssh-copy-id user@host ## add your key to host for user to enable a keyed or passwordless login
File permission
chmod
Change permission of files.
1
chmod `octal` file ## Change file permission of file to octal.
Which can be found separately for user, group and world by adding:
Octal
Description
4
read(r)
2
wite(w)
1
execute(x)
Example:
1 2
chmod 777 ## all permission chmod 755 ## rwx for owner, rx for group and world
chown
Change ownership of files.
1
chown [-R] owner:group dirname/files
chgrp
Change group of files.
1
chgrp [-R] group dirname/files
File commands
ls
List files.
Option
Description
-l
List files details in list
-a
List all the files including hidden files
-d
List directories only
-h
List details of files in human readable way. (File size in GB, KB etc.)
-R
List files recursively
cp
Copy files.
Option
Description
-a
Copy file with attributes
-p
Copy file with attributes, similar as -a
-i
If the target file is exist, it will prompt to confirm overwrite.
-r
Copy recursively.
Example:
1 2
cp -a file1 file2 #copy file1 to file2 with all attributes cp file1 file2 file3 dir#Copy file1、file2、file3 to dir
mv
Move files.
Option
Description
-f
Force move. If the target file exists, it will be overwritten without prompt
-p
It will prompt to confirm overwritten if the target file exists.
-u
If the target file is exist, it won’t update it unless it’s newer than the old one
# Relates to time: -mtime n # n is a number. It means files whose last modification time was n days ago and rounded up to the next full 24-hour period -mtime +n # List the files whose last modification time were n days ago (not including nth day itself) -mtime -n # List the files whose last modification time were within n days (including nth day) -newer file # List files which is newer than file # For example: find /root -mtime 0 # List all the files which are modified today # Relates to user and groups -user name # List all the files whose owner is `name` -group name # List all the files whose group is 'name' -uid n # List all the files whose user id is n -gid n # List all the files whose group id is n # For example: find /home/wayson -user wayson # find all the files whose owner is wayson in directory /home/wayson # Relates to file permissions and name: -name filename # List file with name `filename` -size [+-]SIZE # List files which are larger (+) or smaller(-) than SIZE -type TYPE # Find files with type, possible TYPE values are as follows: # b block special # c character special # d directory # f regular file # l symbolic link # p FIFO # s socket -perm mode # Find files with permission value equaled to mode. mode should be number. E.g 0755 -perm -mode # Find files which include all mode permission. mode should be number -perm +mode # Find files which include any mode permission. mode should be number # For example: find / -name passwd # find files with name passwd find . -perm 0755 # find files with permission 0755 in current path find . -size +12k # Find files larger than 12KB in current path
Shortcuts
Command
Description
Ctrl+C
halt the current command
Ctrl+Z
stop the current command, resume with fg in the foreground or bg in the background