Anda di halaman 1dari 7

freeworld.posterous.

com
Linux Bash Shell
Cheat Sheet
(works with about every distribution, except for apt-et which is !buntu"#ebian exclusive$
Leend%
&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 Basic file manipulation
C01L L . Clear the terminal cat (file+ame) . show content of file
C01L # . Loout (less, more$
S2340 ,ae !p"#own . 5o up"down the terminal head . from the top
C01L 6 . Cursor to start of line -n (7oflines) (file+ame)
C01L & . Cursor the end of line
C01L ! . #elete left of the cursor tail . from the bottom
C01L 8 . #elete riht of the cursor -n (7oflines) (file+ame)
C01L 9 . #elete word on the left
C01L : . ,aste (after C01L !,8 or 9$ mkdir . create new folder
06B . auto completion of file or command mkdir myStuff ..
C01L 1 . reverse search history mkdir myStuff"pictures" ..
;; . repeat last command
C01L < . stops the current command (resume with f in foreround or b in backround$ cp imae.=p newimae.=p . copy and rename a file
Basic 0erminal +aviation cp imae.=p (folder+ame)" . copy to folder
cp imae.=p folder"same3mae+ew+ame.=p
ls -a . list all files and folders cp -1 stuff otherStuff . copy and rename a folder
ls (folder+ame) . list files in folder cp >.txt stuff" . copy all of >(file type) to folder
ls -lh . #etailed list, 2uman readable
ls -l >.=p . list =pe files only mv file.txt #ocuments" . move file to a folder
ls -lh (file+ame) . 1esult for file only mv (folder+ame) (folder+ame/) . move folder in folder
mv filename.txt filename/.txt . rename file
cd (folder+ame) . chane directory mv (file+ame) stuff"newfile+ame
if folder name has spaces use ' ' mv (folder+ame)" .. . move folder up in hierarchy
cd " . o to root
cd .. . o up one folder, tip% .."..".." rm (file+ame) .. . delete file (s$
rm -i (file+ame) .. . ask for confirmation each file
du -h% #isk usae of folders, human readable rm -f (file+ame) . force deletion of a file
du -ah% ' ' ' files ? folders, 2uman readable rm -r (foldername)" . delete folder
du -sh% only show disc usae of folders
touch (file+ame) . create or update a file
pwd . print workin directory
ln file@ file/ . physical link
man (command) . shows manual (104A$ ln -s file@ file/ . symbolic link
Linux Bash Shell Cheat Sheet
Basic Commands
1esearchin 4iles &xtract, sort and filter data
0he slow method (sometimes very slow$% rep (some0ext) (file+ame) . search for text in file
-i . #oesn-t consider uppercase words
locate (text) . search the content of all the files -3 . exclude binary files
locate (file+ame) . search for a file rep -r (text) (folder+ame)" . search for file names
sudo updatedb . update database of files with occurrence of the text
find . the best file search tool(fast$ 9ith reular expressions%
find -name '(file+ame)*
find -name 'text* . search for files who start with the word text rep -& B(text) (file+ame) . search start of lines
find -name '>text* . ' ' ' ' end ' ' ' ' with the word text
rep -& (C-D) (file+ame) .shows lines containin numbers C-D
6dvanced Search% rep -& (a-E6-<) (file+ame) . retrieve all lines
with alphabetical letters
Search from file SiEe (in F$
find F -siEe G@CA . search files bier than.. (A,8,5$ sort . sort the content of files
sort (file+ame) . sort alphabetically
Search from last access sort -o (file) (output4ile) . write result to a file
find -name '(filetype)* -atime -H sort -r (file+ame) . sort in reverse
(--- . less than, -G- . more than and nothin . exactly$ sort -1 (file+ame) . sort randomly
sort -n (file+ame) . sort numbers
Search only files or directoryIs
find -type d --) ex% find "var"lo -name JsysloJ -type d wc . word count
find -type f . files wc (file+ame) . nbr of line, nbr of words, byte siEe
-l (lines$, -w (words$, -c (byte siEe$, -m
Aore info% man find, man locate (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 settins (continued$
date . view ? modify time (on your computer$ crontab . execute a command reularly
-e . modify the crontab
Kiew% -l . view current crontab
date 'GL2* --) 3f it-s M am, then it will show CM -r . delete you crontab
date 'GL2%LA%LSs* . (hours, minutes, seconds$ 3n crontab the syntax is
L: . years (Ainutes) (2ours) (#ay of month) (#ay of week (C-N,
Aodify% C . Sunday$) (COAA6+#)
AA##hhmm::::
Aonth P #ay P 2ours P Ainutes P :ear ex, create the file movies.txt every day at @H%DQ%
DQ @H > > > touch "home"bob"movies.txt
sudo date CR@D/RD/@MMQ . Aarch @D
th
@MMQ, /R%D/ > > > > > --) every minute
at H%RC in the mornin, from the @
st
to @H
th
each month%
&xecute prorams at another time RC H @-@H > >
at midniht on Aondays, 9ednesdays and 0hursdays%
use -at- to execute prorams in the future C C > > @,R,D
every two hours%
Step @, write in the terminal% at (timeOf&xecution) &+0&1 C >"/ > > >
ex --) at 16:45 or at 13:43 7/23/11 (to be more precise$ every @C minutes Aonday to 4riday%
or after a certain delay% >"@C > > > @-H
at now GH minutes (hours, days, weeks, months, years$
Step /% (&+0&1 COAA6+#) &+0&1 &xecute prorams in the backround
repeat step / as many times you need
Step R% C01L # to close input 6dd a -?- at the end of a command
ex --) cp biAovie4ile.mpD ?
atS . show a list of =obs waitin to be executed
nohup% inores the 2!, sinal when closin the console
atrm . delete a =ob nT(x) (process will still run if the terminal is closed$
ex (delete =ob 7D/$ --) atrm D/ ex --) nohup cp biAovie4ile.mpD
sleep . pause between commands =obs . know what is runnin in the backround
with -U- you can chain commands, ex% touch fileU rm file
you can make a pause between commands (minutes, hours, days$ f . put a backround process to foreround
ex --) touch fileU sleep @CU rm file (-- @C seconds ex% f (process @$, fL/ (process /$ fLR, ...
Linux Bash Shell Cheat Sheet
Basic Commands
,rocess Aanaement Create and modify user accounts
w . who is loed on and what they are doin sudo adduser bob . root creates new user
sudo passwd (6ccount+ame) . chane a user-s password
tload . raphic representation of system load averae sudo deluser (6ccount+ame) . delete an account
(Suit with C01L C$
addroup friends . create a new user roup
ps . Static process list delroup friends . delete a user roup
-ef --) ex% ps -ef P less
-e=2 --) show process hierarchy usermod - friends (6ccount) . add user to a roup
-u --) process-s from current user usermod - bob boby . chane account name
usermod -a5 friends bob . add roups to a user with-
top . #ynamic process list out loosin the ones he-s already in
9hile in top%
S to close top 4ile ,ermissions
h to show the help
k to kill a process chown . chane the owner of a file
ex --) chown bob hello.txt
C01L C to top a current terminal process chown user%bob report.txt . chanes the user ownin
report.txt to -user- and the roup ownin it to -bob-
kill . kill a process -1 . recursively affect all the sub folders
:ou need the ,3# 7 of the process ex --) chown -1 bob%bob "home"#aniel
ps -u (6ccount+ame) P rep (6pplication)
0hen chmod . modify user access"permission V simple way
kill (,3#) .. .. .. u . user
kill -M (,3#) . violent kill . roup
o . other
killall . kill multiple process-s
ex --) killall locate d . directory (if element is a directory$
l . link (if element is a file link$
extras% r . read (read permissions$
sudo halt (-- to close computer w . write (write permissions$
sudo reboot (-- to reboot x . eWecute (only useful for scripts and
prorams$
Linux Bash Shell Cheat Sheet
Basic Commands
4ile ,ermissions (continued$ 4low 1edirection (continued$
-G- means add a riht terminal output%
--- means delete a riht 6lex
-.- means affect a riht Cinema
Code
ex --) chmod Gw some4ile.txt 5ame
(add to current roup the riht to modify some4ile.txt$ !buntu
more info% man chmod 6nother example --) wc -m (( &+#
4low redirection Chain commands
1edirect results of commands% -P- at the end of a command to enter another one
ex --) du P sort -nr P less
-)- at the end of a command to redirect the result to a file
ex --) ps -e=2 ) process.txt 6rchive and compress data
-))- to redirect the result to the end of a file
6rchive and compress data the lon way%
1edirect errors%
Step @, put all the files you want to compress in
-/)- at the end of the command to redirect the result to a file the same folder% ex --) mv >.txt folder"
ex --) cut -d , -f @ file.csv ) file /) errors.lo
-/)?@- to redirect the errors the same way as the standard output Step /, Create the tar file%
tar -cvf myXarchive.tar folder"
1ead proressively from the keyboard -c % creates a .tar archive
-v % tells you what is happenin (verbose$
(Command) (( (word0o0erminate3nput) -f % assembles the archive into one file
ex --) sort (( &+# (-- 0his can be anythin you want
) 2ello Step R.@, create Eip file (most current$%
) 6lex Eip myXarchive.tar
) Cinema to decompress% unEip myXarchive.tar.E
) 5ame
) Code Step R./, or create a bEip/ file (more powerful but slow$%
) !buntu bEip/ myXarchive.tar
) &+# to decompress% bunEip/ myXarchive.tar.bE/
Linux Bash Shell Cheat Sheet
Basic Commands
6rchive and compress data (continued$ 3nstallin software
step D, to decompress the .tar file% 9hen software is available in the repositories%
tar -xvf archive.tar archive.tar sudo apt-et install (nameOfSoftware)
ex--) sudo apt-et install aptitude
6rchive and compress data the fast way%
3f you download it from the 3nternets in .E format
Eip% tar -Ecvf myXarchive.tar.E folder" (or bE/$ - 'Compilin from source*
decompress% tar -Ecvf myXarchive.tar.E #ocuments" Step @, create a folder to place the file%
mkdir "home"username"src (-- then cd to it
bEip/% tar -=cvf myXarchive.tar.E folder"
decompress% tar -=xvf archive.tar.bE/ #ocuments" Step /, with -ls- verify that the file is there
(if not, mv .."file.tar.E "home"username"src"$
Show the content of .tar, .E or .bE/ without decompressin it%
Step R, decompress the file (if .Eip% unEip (file)$
Eip% (--
Eip -Etf archive.tar.E Step D, use -ls-, you should see a new directory
bEip/% Step H, cd to the new directory
bEip/ -=tf archive.tar.bE/ Step N.@, use ls to verify you have an 3+S06LL file,
tar% then% more 3+S06LL
tar -tf archive.tar 3f you don-t have an 3+S06LL file%
Step N./, execute ./configure (-- creates a makefile
tar extra% Step N./.@, run make (-- builds application binaries
tar -rvf archive.tar file.txt . add a file to the .tar Step N././ % switch to root --) su
Step N./.R % make install (-- installs the software
:ou can also directly compress a sinle file and view the file Step Q, read the readme 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$

Anda mungkin juga menyukai