Show Menu
Cheatography

Linux Terminal Cheat Sheet Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Rebooting and Shutting Down

shutdown at 10:00
$ sudo shutdown -h 10:00
shutdown now
$ sudo shutdown -h now
restart at 10:00
$ sudo shutdown -r 10:00
restart now
$ sudo shutdown -r now
with message "­Shu­tting down for scheduled mainte­nance.
$ sudo shutdown -h 10:00 "­Shu­tting down for scheduled mainte­nan­ce."­

Locate applic­ation

locate applic­ation only
which "­app­lic­ation name"
locates applic­ation , source and man
whereis "­app­lic­ation name"

Locate / Find

The locate utility program performs a search through a previously constr­ucted database of files and direct­ories on your system, matching all entries that contain a specified character string

updatedb to update the database

can use grep program as a filter; grep will print only the lines that contain one or more specified strings
$ locate zip | grep bin

Finding Files

Searching for files and direct­ories named "­gcc­"
find /usr -name gcc
Searching only for direct­ories named "­gcc­":
find /usr -type d -name gcc
Searching only for regular files named "­gcc­"
$ find /usr -type f -name gcc
*To find files based on time created "+_ n" days
find / -ctime n
To find files based on size +- n
$ find / -size n
Another good use of find is being able to run commands on the files that match your search criteria. The -exec option is used for this purpose.
To find and remove all files that end with .swp:

$ find -name "­*.s­wp" -exec rm {} ’;’
*mtime for modifi­ed/­written time
atime for access time/read time
 

Accessing Direct­ories

Displays the present working directory
pwd
Change to your home directory
cd ~ or cd
Change to parent directory
cd ..
Change to previous directory
cd -

Exploring the FileSystem

Changes your current directory to the root (/) directory (or path you supply)
cd /
List the contents of the present working directory
ls
List all files including hidden files and direct­ories (those whose name start with . )
ls –a
Displays a tree view of the filesystem
tree

Direct­ories

to create a directory.
mkdir
create a sample directory named sampdir under the current directory,
mkdir sampdir
create a sample directory called sampdir under /usr
mkdir /usr/s­ampdir.
Removing a directory *
rmdir
To remove a directory and all of its contents
rm -rf
* for rmdir The directory must be empty or it will fail
* usage sample for rmdir and rm -rf is same as mkdir

Renaming / removing directory

Rename a directory
mv
Remove an empty directory
rmdir
Forcefully remove a directory recurs­ively
rm -rf

Basic Packagaing Commands

Install package
dpkg --install foo.deb
Install package, depend­encies
apt-get install foo
Remove package
dpkg --remove foo.deb
Remove package, depend­encies
apt-get autoremove foo
Update package
dpkg --install foo.deb
Update package, depend­encies
apt-get install foo
Update entire system
apt-get dist-u­pgrade
Show all installed packages
dpkg --list
Get inform­ation on package
dpkg --list­files foo
Show packages named foo
apt-cache search foo
Show all available packages
apt-cache dumpavail foo
What package is file part of?
dpkg --search file
 

Viewing Files

for viewing files that are not very long; it does not provide any scroll­-back.
cat
o look at a file backwards, starting with the last line
tac
Used to view larger files
less (/ for forwad and ? for backward)
Used to print the last 10 lines
tail
Used to print the first 10 lines
head
Used to print the last X lines
tail -n X or tail -X
Used to print the first X lines
head -n X or head -X

File Linking

Hardlink file1 and file2
$ ln file1 file2
Softlink file1 and file2
$ ln -s file1 file2

New file / change time of file

Create newfile
$ touch "­fil­ena­me"
sets the "­myf­ile­" file's time stamp to 4 p.m., March 20th (03 20 1600).
$ touch -t 03201600 myfile

Removi­ng/­Ren­aming file

Rename a file
mv
Remove a file
rm
Forcefully remove a file
rm –f
Intera­ctively remove a file
rm –i