Copyright 2014-2005, Free Electrons Free to share under the terms of the Creati!e Commons "ttri#ution-Share"li$e %0 license &http'((creati!ecommonsorg) Sources, translations, updates, command and concepts details on our free training materials' http'((free-electronscom(training(intro*uni+*linu+ ,han$s to -ichel .lanc, /ermann 0 .ec$ers and ,hierry 1rellier 2atest update' "pr 22, 2014 Handling fles and directories Create a directory' mkdir dir Create nested directories' mkdir -p dir1/dir2 Changing directories' cd newdir cd .. &parent directory) cd - &pre!ious directory) cd &home directory) cd 3bill &home directory of user bill) 4rint the 5or$ing ¤t) directory' p5d Copy a 6le to another' cp source_file dest_file Copy 6les to a directory' cp file1 file2 dir Copy directories recursi!ely' cp -r source_dir dest_dir rsync -a source_dir/ dest_dir/ Create a sym#olic lin$' ln -s linked_file link 7ename a 6le, lin$ or directory' mv source_file dest_file 7emo!e 6les or lin$s' rm file1 file2 7emo!e empty directories' rmdir dir 7emo!e non-empty directories' rm -rf dir Listing fles 2ist all 8regular9 6les ¬ starting 5ith .) in the current directory' ls :isplay a long listing' ls -l 2ist all the 6les in the current directory, including 8hidden9 ones &starting 5ith .)' ls -a 2ist #y time &most recent 6les 6rst)' ls -t 2ist #y si;e &#iggest 6les 6rst) ls -S 2ist 5ith a re!erse sort order' ls -r 2ong list 5ith most recent 6les last' ls -ltr Displaying fle contents Concatenate and display 6le contents' cat file1 file2 :isplay the contents of se!eral 6les &stopping at each page)' more file1 file2 less file1 file2 &#etter' e+tra features) :isplay the 6rst 10 lines of a 6le' head -10 file :isplay the last 10 lines of a 6le' tail -10 file File name pattern matching Concatenate all 8regular9 6les' cat * Concatenate all 8hidden9 6les' cat .* Concatenate all 6les ending 5ith .log' cat *.log 2ist 8regular9 6les 5ith bug in their name' ls *bug* 2ist all 8regular9 6les ending 5ith . and a single character' ls *.? Handling fle contents Sho5 only the lines in a 6le containing a gi!en su#string' grep substring file Case insensiti!e search' grep -i substring file Sho5ing all the lines #ut the ones containing a su#string' grep -v substring file Search through all the 6les in a directory' grep -r substring dir Sort lines in a gi!en 6le' sort file Sort lines, only display duplicate ones once' sort -u file &uni<ue) Changing fle access rights "dd 5rite permissions to the current user' chmod u+w file "dd read permissions to users in the 6le group' chmod g+r file "dd e+ecute permissions to other users' chmod o+x file "dd read = 5rite permissions to all users' chmod a+rw file -a$e e+ecuta#le 6les e+ecuta#le #y all' chmod a+rX * -a$e the 5hole directory and its contents accessi#le #y all users' chmod -R a+rX dir &recursi!e) Comparing fles and directories Comparing 2 6les' diff file1 file2 Comparing 2 6les &graphical)' gvimdiff file1 file2 tkdiff file1 file2 meld file1 file2 Comparing 2 directories' diff -r dir1 dir2 Looking for fles Find all 6les in the current &.) directory and its su#directories 5ith log in their name' find . -name *log* Find all the .pdf 6les in dir and su#directories and run a command on each' find . -name *.pdf -exec xpdf {} ';' >uic$ system-5ide 6le search #y pattern &caution' inde+ #ased, misses ne5 6les)' locate *pub* Redirecting command output 7edirect command output to a 6le' ls *.png > image_files "ppend command output to an e+isting 6le' ls *.jpg >> image_files 7edirect command output to the input of another command' cat *.log | grep error Job control Sho5 all running processes' ps -ef 2i!e hit-parade of processes &press P, M, T' sort #y 4rocessor, -emory or ,ime usage)' top Send a termination signal to a process' kill <pid> &num#er found in ps output) /a!e the $ernel $ill a process' kill -9 <pid> ?ill all processes &at least all user ones)' kill -9 -1 ?ill a graphical application' xkill &clic$ on the program 5indo5 to $ill) File and partition sizes Sho5 the total si;e on dis$ of 6les or directories &dis$ usage)' du -sh dir1 dir2 file1 file2 @um#er of #ytes, 5ords and lines in 6le' wc file &word count) Sho5 the si;e, total space and free space of the current partition' df -h . :isplay these info for all partitions' df -h Compressing Compress a 6le' gzip file &.gz format) bzip2 file &.bz2 format, #etter) lzma file &.lzma format, #est compression) xz file &.xz format, #est for code) Ancompress a 6le' gunzip file.gz bunzip2 file.bz2 unlzma file.lzma unxz file.xz Archiing Create a compressed archi!e &tape archi!e)' tar zcvf archive.tar.gz dir tar jcvf archive.tar.bz2 dir tar Jcvf archive.tar.xz dir tar --lzma -cvf archive.tar.lzma Test &list) a compressed archi!e' tar tvf archive.tar.[gz|bz2|lzma|xz] Extract the contents of a compressed archi!e' tar xvf archive.tar.[gz|bz2|lzma|xz] tar options' c' create t' test x' extract j' on the By bzip2 &un)compression J' on the By xz &un)compression z' on the By gzip &un)compression /andling ;ip archi!es zip -r archive.zip <files> &create) unzip -t archive.zip &test ( list) unzip archive.zip &e+tract) !rinting Send 4ostScript or te+t 6les to queue' lpr -Pqueue f1.ps f2.txt &local printer) 2ist all the print Co#s in queue' lpq -Pqueue Cancel a print Co# num#er in queue' cancel 123 queue 4rint a 4:F 6le' pdf2ps doc.pdf lpr doc.ps Die5 a 4ostScript 6le' ps2pdf doc.ps xpdf doc.pdf "ser management 2ist users logged on the system' who Sho5 5hich user E am logged as' whoami Sho5 5hich groups user #elongs to' groups user ,ell more information a#out user' finger user S5itch to user hul$' su - hulk S5itch to super user &root)' su - &s5itch user) su &$eep same directory and en!ironment) #ime management Fait for G0 seconds' sleep 60 Sho5 the current date' date Count the time ta$en #y a command' time find_charming_prince -cute -rich Command help .asic help &5or$s for most commands)' grep --help "ccess the full manual page of a command' man grep $isc commands .asic command-line calculator bc -l %asic system administration Change the o5ner and group of a directory and all its contents' sudo chown -R newuser.newgroup dir 7e#oot the machine in 5 minutes' sudo shutdown -r +5 Shutdo5n the machine no5' sudo shutdown -h now :isplay all a!aila#le net5or$ interfaces' ifconfig -a "ssign an E4 address to a net5or$ interface' sudo ifconfig eth0 207.46.130.108 .ring do5n a net5or$ interface' sudo ifconfig eth0 down :e6ne a default gate5ay for pac$ets to machines outside the local net5or$' sudo route add default gw 192.168.0.1 :elete the default route' sudo route del default ,est net5or$ing 5ith another machine' ping 207.46.130.108 Create or remo!e partitions on the 6rst E:E hard dis$' fdisk /dev/hda1 Create &format) an e+t% 6lesystem' mkfs.ext3 /dev/hda1 Create &format) a F",%2 6lesystem' mkfs.vfat -v -F 32 /dev/hda2 -ount a formatted partition' mkdir /mnt/usbdisk &Cust do it once) sudo mount /dev/uba1 /mnt/usbdisk -ount a 6lesystem image &loop de!ice)' sudo mount -o loop fs.img /mnt/fs Anmount a 6lesystem' sudo umount /mnt/usbdisk Chec$ the system $ernel !ersion' uname -a