You are on page 1of 7

freeworld.posterous.

com

Linux Bash Shell Cheat Sheet


(works with about every distribution, except for apt- et which is !buntu"#ebian exclusive$

Le end% &verythin in '()* is to be replaced, ex% (file+ame) --) iLove,eanuts.txt #on-t include the -.- in your commands -..- means that more than one file can be affected with only one command ex% rm file.txt file/.txt movie.mov .. ..

Linux Bash Shell Cheat Sheet


Basic Commands Basic 0erminal Shortcuts
C01L L . Clear the terminal C01L # . Lo out S2340 ,a e !p"#own . 5o up"down the terminal C01L 6 . Cursor to start of line C01L & . Cursor the end of line C01L ! . #elete left of the cursor C01L 8 . #elete ri ht of the cursor C01L 9 . #elete word on the left C01L : . ,aste (after C01L !,8 or 9$ 06B . auto completion of file or command C01L 1 . reverse search history ;; . repeat last command
C01L < . stops the current command (resume with f in fore round or b in back round$

Basic file manipulation


cat (file+ame) . show content of file (less, more$ head . from the top -n (7oflines) (file+ame) tail . from the bottom -n (7oflines) (file+ame) mkdir . create new folder mkdir myStuff .. mkdir myStuff"pictures" .. cp ima e.=p cp cp cp cp mv mv mv mv mv rm rm rm rm newima e.=p . copy and rename a file

Basic 0erminal +avi ation


ls ls ls ls ls -a . list all files and folders (folder+ame) . list files in folder -lh . #etailed list, 2uman readable -l >.=p . list =pe files only -lh (file+ame) . 1esult for file only

ima e.=p (folder+ame)" . copy to folder ima e.=p folder"same3ma e+ew+ame.=p -1 stuff otherStuff . copy and rename a folder >.txt stuff" . copy all of >(file type) to folder file.txt #ocuments" . move file to a folder (folder+ame) (folder+ame/) . move folder in folder filename.txt filename/.txt . rename file (file+ame) stuff"newfile+ame (folder+ame)" .. . move folder up in hierarchy (file+ame) .. . delete file (s$ -i (file+ame) .. . ask for confirmation each file -f (file+ame) . force deletion of a file -r (foldername)" . delete folder

cd (folder+ame) . chan e directory if folder name has spaces use ' ' cd " . o to root cd .. . o up one folder, tip% .."..".." du -h% #isk usa e of folders, human readable du -ah% ' ' ' files ? folders, 2uman readable du -sh% only show disc usa e of folders pwd . print workin directory

touch (file+ame) . create or update a file man (command) . shows manual (104A$ ln file@ file/ . physical link ln -s file@ file/ . symbolic link

Linux Bash Shell Cheat Sheet


Basic Commands 1esearchin 4iles &xtract, sort and filter data
rep (some0ext) (file+ame) . search for text in file -i . #oesn-t consider uppercase words -3 . exclude binary files rep -r (text) (folder+ame)" . search for file names with occurrence of the text

0he slow method (sometimes very slow$%


locate (text) . search the content of all the files locate (file+ame) . search for a file sudo updatedb . update database of files find find find find . the -name -name -name best file search tool (fast$ '(file+ame)* 'text* . search for files who start with the word text '>text* . ' ' ' ' end ' ' ' '

9ith re ular expressions%


rep with rep rep with sort sort sort sort sort sort -& B(text) (file+ame) . search start of lines the word text -& (C-D) (file+ame) .shows lines containin numbers C-D -& (a-E6-<) (file+ame) . retrieve all lines alphabetical letters . sort the content of files (file+ame) . sort alphabetically -o (file) (output4ile) . write result to a file -r (file+ame) . sort in reverse -1 (file+ame) . sort randomly -n (file+ame) . sort numbers

6dvanced Search%
Search from file SiEe (in F$ find F -siEe G@CA . search files bi er than.. (A,8,5$

Search from last access find -name '(filetype)* -atime -H (--- . less than, -G- . more than and nothin Search only files or directoryIs find -type d --) ex% find "var"lo find -type f . files Aore info% man find, man locate

. exactly$

-name Jsyslo J -type d

wc . word count wc (file+ame) . nbr of line, nbr of words, byte siEe -l (lines$, -w (words$, -c (byte siEe$, -m (number of characters$ cut . cut a part of a file -c --) ex% cut -c /-H names.txt (cut the characters / to H of each line$ -d (delimiter$ (-d ? -f ood for .csv files$ -f (7 of field to cut$ more info% man cut, man sort, man rep

Linux Bash Shell Cheat Sheet


Basic Commands 0ime settin s
date . view ? modify time (on your computer$ Kiew% date 'GL2* --) 3f it-s M am, then it will show CM date 'GL2%LA%LSs* . (hours, minutes, seconds$ L: . years Aodify% AA##hhmm:::: Aonth P #ay P 2ours P Ainutes P :ear sudo date CR@D/RD/@MMQ . Aarch @D th @MMQ, /R%D/

(continued$
crontab . execute a command re ularly -e . modify the crontab -l . view current crontab -r . delete you crontab 3n crontab the syntax is (Ainutes) (2ours) (#ay of month) (#ay of week (C-N, C . Sunday$) (COAA6+#) ex, create the file movies.txt every day at @H%DQ% DQ @H > > > touch "home"bob"movies.txt > > > > > --) every minute at H%RC in the mornin , from the @st to @Hth each month% RC H @-@H > > at midni ht on Aondays, 9ednesdays and 0hursdays% C C > > @,R,D every two hours% C >"/ > > > every @C minutes Aonday to 4riday% >"@C > > > @-H

&xecute pro rams at another time


use -at- to execute pro rams in the future Step @, write in the terminal% at (timeOf&xecution) &+0&1 ex --) at 16:45 or at 13:43 7/23/11 (to be more precise$ or after a certain delay% at now GH minutes (hours, days, weeks, months, years$ Step /% (&+0&1 COAA6+#) &+0&1 repeat step / as many times you need Step R% C01L # to close input atS . show a list of =obs waitin atrm . delete a =ob nT(x) ex (delete =ob 7D/$ --) atrm D/ sleep . pause between commands with -U- you can chain commands, ex% touch fileU rm file you can make a pause between commands ( minutes, hours, days$ ex --) touch fileU sleep @CU rm file (-- @C seconds to be executed

&xecute pro rams in the back round


6dd a -?- at the end of a command ex --) cp bi Aovie4ile.mpD ? nohup% i nores the 2!, si nal when closin the console (process will still run if the terminal is closed$ ex --) nohup cp bi Aovie4ile.mpD =obs . know what is runnin f in the back round

. put a back round process to fore round ex% f (process @$, fL/ (process /$ fLR, ...

Linux Bash Shell Cheat Sheet


Basic Commands ,rocess Aana ement
w . who is lo ed on and what they are doin

Create and modify user accounts


sudo adduser bob . root creates new user sudo passwd (6ccount+ame) . chan e a user-s password sudo deluser (6ccount+ame) . delete an account add roup friends . create a new user roup del roup friends . delete a user roup usermod - friends (6ccount) . add user to a roup usermod - bob boby . chan e account name usermod -a5 friends bob . add roups to a user without loosin the ones he-s already in

tload . raphic representation of system load avera e (Suit with C01L C$ ps . Static process list -ef --) ex% ps -ef P less -e=2 --) show process hierarchy -u --) process-s from current user top . #ynamic process list 9hile in top% S to close top h to show the help k to kill a process

4ile ,ermissions
chown . chan e the owner of a file ex --) chown bob hello.txt chown user%bob report.txt . chan es the user ownin report.txt to -user- and the roup ownin it to -bob-1 . recursively affect all the sub folders ex --) chown -1 bob%bob "home"#aniel chmod . modify user access"permission V simple way u . user . roup o . other d . l . r . w . x . pro directory (if element is a directory$ link (if element is a file link$ read (read permissions$ write (write permissions$ eWecute (only useful for scripts and rams$

C01L C to top a current terminal process kill . kill a process :ou need the ,3# 7 of the process ps -u (6ccount+ame) P rep (6pplication) 0hen kill (,3#) .. .. .. kill -M (,3#) . violent kill killall . kill multiple process-s ex --) killall locate extras% sudo halt (-- to close computer sudo reboot (-- to reboot

Linux Bash Shell Cheat Sheet


Basic Commands 4ile ,ermissions (continued$
-G- means add a ri ht --- means delete a ri ht -.- means affect a ri ht ex --) chmod Gw some4ile.txt (add to current roup the ri ht to modify some4ile.txt$ more info% man chmod

4low 1edirection (continued$


terminal output% 6lex Cinema Code 5ame !buntu 6nother example --) wc -m (( &+#

4low redirection
1edirect results of commands% -)- at the end of a command to redirect the result to a file ex --) ps -e=2 ) process.txt -))- to redirect the result to the end of a file

Chain commands
-P- at the end of a command to enter another one ex --) du P sort -nr P less

6rchive and compress data


6rchive and compress data the lon way%

1edirect errors%
-/)- at the end of the command to redirect the result to a file ex --) cut -d , -f @ file.csv ) file /) errors.lo -/)?@- to redirect the errors the same way as the standard output 1ead pro ressively from the keyboard (Command) (( (word0o0erminate3nput)
ex --) sort (( &+# (-- 0his can be anythin
) ) ) ) ) ) ) 2ello 6lex Cinema 5ame Code !buntu &+#

Step @, put all the files you want to compress in the same folder% ex --) mv >.txt folder" Step /, Create the tar file% tar -cvf myXarchive.tar folder" -c % creates a .tar archive -v % tells you what is happenin (verbose$ -f % assembles the archive into one file
Step R.@, create Eip file (most current$% Eip myXarchive.tar to decompress% unEip myXarchive.tar. E Step R./, or create a bEip/ file (more powerful but slow$% bEip/ myXarchive.tar to decompress% bunEip/ myXarchive.tar.bE/

you want

Linux Bash Shell Cheat Sheet


Basic Commands 6rchive and compress data (continued$
step D, to decompress the .tar file% tar -xvf archive.tar archive.tar 6rchive and compress data the fast way% Eip% tar -Ecvf myXarchive.tar. E folder" decompress% tar -Ecvf myXarchive.tar. E #ocuments" bEip/% tar -=cvf myXarchive.tar. E folder" decompress% tar -=xvf archive.tar.bE/ #ocuments" Show the content of .tar, . E or .bE/ Eip% Eip -Etf archive.tar. E bEip/% bEip/ -=tf archive.tar.bE/ tar% tar -tf archive.tar tar extra% tar -rvf archive.tar file.txt . add a file to the .tar :ou can also directly compress a sin le file and view the file without decompressin % Step @, use Eip or bEip/ to compress the file% Eip numbers.txt Step /, view the file without decompressin it% Ecat . view the entire file in the console (same as cat$ Emore . view one screen at a time the content of the file (same as more$ Eless . view one line of the file at a time (same as less$ without decompressin it% Step R, decompress the file (if .Eip% unEip (file)$ (-Step D, use -ls-, you should see a new directory Step H, cd to the new directory Step N.@, use ls to verify you have an 3+S06LL file, then% more 3+S06LL 3f you don-t have an 3+S06LL file% Step N./, execute ./configure (-- creates a makefile Step N./.@, run make (-- builds application binaries Step N././ % switch to root --) su Step N./.R % make install (-- installs the software Step Q, read the readme file 3f you download it from the 3nternets in . E format (or bE/$ - 'Compilin from source* Step @, create a folder to place the file% mkdir "home"username"src (-- then cd to it Step /, with -ls- verify that the file is there (if not, mv .."file.tar. E "home"username"src"$

3nstallin

software

9hen software is available in the repositories% sudo apt- et install (nameOfSoftware) ex--) sudo apt- et install aptitude

You might also like