Unix command line concepts


Published on

Unix (Linux) command line concepts

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Unix command line concepts

  1. 1. Unix/Linux command lineconceptsJan 24, 2012 Juche 101Artem Nagornyi
  2. 2. Chapter 1ls, mkdir, cd, pwd
  3. 3. 1.1 Listing files and directories$ ls The ls command (lowercaseL and lowercase S) lists thecontents of your currentworking directory.
  4. 4. ...$ ls -a Lists all files in the currentdirectory including thosewhose names begin with adot (.) which are consideredas "hidden".
  5. 5. 1.2 Making directories$ mkdir dirname This will create a new sub-directory in the currentdirectory.
  6. 6. 1.3 Changing to a different directory$ cd mydir$ cd /local/usr/binChanges the current workingdirectory to mydir directory.You can also use full path ofthe directory.
  7. 7. 1.4 The directories . and ..$ cd .$ cd ..$ cd ./mydir/innerdir$ ls ./mydir/innerdir$ cd ../../anotherdir$ ls ../../anotherdirIn UNIX, (.) means thecurrent directory, so typingthis command means staywhere you are (the currentdirectory).(..) means the parent of thecurrent directory, so typingthis command will take youone directory up thehierarchy.Feel free to use (.) and (..)symbols when changing and
  8. 8. 1.5 Pathnames$ pwd Prints path of the currentdirectory (working directory).
  9. 9. 1.6 Home directory~$ ls ~/mydirThis is the symbol of yourhome directory. Each user ofthe Unix system has its ownusername and own homedirectory under /home.For example these are homedirs: /home/artem,/home/johnWill list the contents of mydirsub-directory of your homedirectory, no matter whereyou currently are in the filesystem.
  10. 10. Chapter 2cp, mv, rm, rmdir, cat, less, head,tail, grep, wc
  11. 11. 2.1 Copying files$ cp ./myfile /home/artem$ cp myfile /home/artem/mf2$ cp /usr/local/myfile .Copies myfile file from thecurrent directory to/home/artem directory.Copies myfile file from thecurrent directory to/home/artem directory,renaming it to mf2.Copies /usr/local/myfile to thecurrent directory.
  12. 12. ...$ ln -s /usr/local/ff/firefox /usr/bin/firefoxThis command will make a symbolic link /usr/bin/firefox to thefile /usr/local/ff/firefoxSymbolic links have l symbol in the beginning of ls -l outputstring.$ ln /usr/local/ff/firefox /usr/bin/firefoxThis will make a hard link. The difference between a symboliclink and a hard link is that a hard link to a file isindistinguishable from the original directory entry; just considerit as a file alias. Hard links may not normally refer to directories.
  13. 13. ...$ ln myfile hlink$ rm myfile$ cat hlinkThis experiment proves that ahard link is just another namefor a file. Even after deletingoriginal file it still existsbecause we havent deletedthe hard link. Simply there isreally no such thing as "hardlink", we just create anothername for a file.
  14. 14. 2.2 Moving files$ mv ./myfile /home/artem$ mv myfile /home/artem/mf2$ mv /usr/local/myfile .Moves myfile file from thecurrent directory to/home/artem directory.Moves myfile file from thecurrent directory to/home/artem directory,renaming it to mf2.Moves /usr/local/myfile to thecurrent directory.
  15. 15. 2.3 Removing files and directories$ rm myfile$ rm /usr/local/myfile$ rm -R mydir$ rmdir mydirRemoves myfile file in thecurrent directory.Removes /usr/local/mydir file.Removes mydir sub-directoryin the current directory.Removes mydir sub-directoryin the current directory.
  16. 16. 2.4 Displaying the contents of a file onthe screen$ clear$ cat myfile$ less myfile$ head myfile$ head -5 myfile$ tail -5 myfileWill clear screen.Will display the content of afile on the screen.Will display the content of afile page-by-page.Will display the first 10 linesof myfile on the screen.Will display the first 5 lines.Will display the last 5 lines.
  17. 17. 2.5 Searching the contents of a file$ less myfile This will display the contentsof myfile page-be-page.Then, still in less, type aforward slash [/] followed bythe word to search. less findsand highlights the keyword.Type [n] to search for thenext occurrence of the word.
  18. 18. ...$ grep Science myfile$ grep spinning top myfile$ grep -i Science myfileThis will print each line ofmyfile containing the wordScience (it is case-sensitive).To search for a phrase orpattern, you must enclose itin single quotes.Key -i will ignore upper/lowercase in the search results.
  19. 19. ...Some of the other options of grep are:-v display those lines that do NOT match-n precede each matching line with the line number-c print only the total count of matched lines
  20. 20. ...$ wc -w myfile$ wc -l myfileWill return the number ofwords in myfile.Will return the number oflines in myfile.
  21. 21. Chapter 3>, >>, <, |, sort, who
  22. 22. 3.1 Redirection$ cat Type cat without specifing afile to read. Then type a fewwords on the keyboard andpress the [Return] key.Finally hold the [Ctrl] keydown and press [d] (writtenas ^D for short) to end theinput.It reads the standard input(the keyboard), and onreceiving the end of file(^D), copies it to the standardoutput (the screen).
  23. 23. ...$ cat > myfile Type something, then press[Ctrl-d] to end the input.The output will be redirectedto myfile.If the file already containssomething, it will beoverwritten.
  24. 24. ...$ cat >> myfile Type something, then press[Ctrl-d] to end the input.The output will be redirectedand appended to myfile.If the file already containssomething, it will beappended.
  25. 25. ...$ cat myfile1 myfile2 > file3 This will join (concatenate)myfile1 and myfile2 into anew file called file3.What this is doing is readingthe contents of myfile1 andmyfile2 in turn, thenoutputting the text to the filefile3.
  26. 26. 3.3 Redirecting the input$ sort Enter this command. Thentype in the names of someanimals. Press [Return] aftereach one.dogcatbirdape[Ctrl-d]The output will be:apebirdcatdog
  27. 27. ...$ sort < file1 > file2 Input redirection is <In this command we use bothinput and output redirection.The unsorted list will be takenfrom file1 and already sortedlist will be redirected to file2.
  28. 28. 3.4 Pipes$ who > usernames$ sort < usernames$ who | sortwho command returns thelist of all users currentlylogged in the system. This isa method to get a sorted listof names by using atemporary file usernames.This way we can avoidtemporary file creation. Herewe connect the output of thewho command directly to theinput of the sort command.This is exactly what pipes do.Pipe symbol is |
  29. 29. ...$ who | wc -l$ cat myfile | grep scienceAnd this is the way to find outhow many users are loggedin. We are using a pipebetween who and wccommands.This displays the line ofmyfile that contains sciencestring. We are using pipebetween cat and grepcommands.
  30. 30. Chapter 4*, ?, man, whatis, apropos
  31. 31. 4.1 Wildcards$ ls list*$ ls *listThe character * is called awildcard, and will matchagainst none or morecharacter(s) in a file (ordirectory) name.This will list all files in thecurrent directory starting withlist...This will list all files in thecurrent directory ending with...list
  32. 32. ...$ ls ?ouseThe character ? will matchexactly one character.So ?ouse will match files likehouse and mouse, but notgrouse.
  33. 33. 4.2 Unix filename conventionsUnix-legitimate filenames are any combination of these threeclasses of characters:1. Upper and lower case letters: A - Z and a - z (nationalcharacters are also supported in Unicode and otherencodings)2. Numbers 0 - 93. Periods, underscores, hyphens . _ -Some other characters can be also supported, but they are notrecommended to use.
  34. 34. 4.3 Getting help$ man wc$ whatis wcThis will display the manualpage for wc command.Gives a one-line descriptionof the command, but omitsany information about optionsetc.
  35. 35. ...$ apropos -s "1" copy If you are not sure about theexact name of the command,this will give you the list ofcommands with keyword intheir manual page header.-s key defines section of Unixmanual:1. General commands2. System calls3. Library functions4. Special files5. File formats and conventions6. Games and screensavers7. Miscellanea8. System administration commands anddaemons
  36. 36. Chapter 5ls -lag, chmod, command &, bg,jobs, fg, kill, ps
  37. 37. 5.1 File system access rights$ ls -l● The left group of 3 gives the filepermissions for the user that owns thefile (or directory) (artem in the aboveexample).● The middle group of 3 gives thepermissions for the group of people towhom the file (or directory) belongs(softserve in the above example);● The rightmost group of 3 gives thepermissions for all others.The symbol in the beginning of the string indicates whether this is a file, directory or alink:d indicates a directory, - indicates a file, l indicates a symbolic link.The permissions r, w, x (read, write, execute) have slightly different meanings dependingon whether they refer to a simple file or to a directory.-rw-rw-r-- 1 artem softserve 83 Feb 3 1995 myfileGroup ofthe ownerOwner of thefileModificationdateFilenameFilepermissionsNumber ofsubdirectories (1for a file)Size
  38. 38. ...Access rights on files● r indicates read permission (or otherwise), that is, the presence or absence ofpermission to read and copy the file● w indicates write permission (or otherwise), that is, the permission (or otherwise) tochange a file● x indicates execution permission (or otherwise), that is, the permission to execute a file,where appropriateAccess rights on directories● r allows users to list files in the directory;● w means that users may delete files from the directory or move files into it;● x means the right to access files in the directory. This implies that you may read files inthe directory provided you have read permission on the individual files.So, in order to read a file, you must have execute permission on the directory containingthat file, and hence on any directory containing that directory as a subdirectory, and so on,up the tree.Also file can be written if its permissions allow Write, but it can only be deleted if itsdirectorys permissions allow Write.
  39. 39. 5.2 Changing access rightsAccess classes:u (user)g (group)o (other)a (all: u, g and o)Operators:+ (add access)- (remove access)= (set exact access)Examples:chmod a+r myfileadd permission for everyone to read afile (or just: chmod +r myfile)chmod go-rw myfileremove read and write permission forgroup and other userschmod a-w+x myfileremove write permission and addexecute for all userschmod go=r myfileexplicitly state that group and otherusers access is set only to read
  40. 40. ...The other way to use the chmod command is the absolute form. In thiscase, you specify a set of three numbers that together determine allthe access classes and types. Rather than being able to change onlyparticular attributes, you must specify the entire state of the filespermissions.The three numbers are specified in the order: user (or owner), group,other. Each number is the sum of values that specify: read (4), write(2), and execute (1) access, with 0 (zero) meaning no access. Forexample, if you wanted to give yourself read, write, and executepermissions on myfile; give users in your group read and executepermissions; and give others only execute permission, the appropriatenumber would be calculated as (4+2+1)(4+0+1)(0+0+1) for the threedigits 751. You would then enter the command as:chmod 751 myfile
  41. 41. 5.2.1 Changing owner of the file$ sudo chown artem myfile$ sudo chown -hR artemmyfolderChange the owner of myfileto artem.We are using sudo beforechown to temporarily givethe current useradministrative permissions(you will need to enter theroot user password).Change the owner ofmyfolder folder to artemincluding all nested sub-directories and filesrecursively.
  42. 42. 5.2.2 Changing file timestamps$ touch -d "2005-02-03 14:04:25" myfile$ touch -md "2005-02-03 14:04:25" myfile$ touch -ad "2005-02-03 14:04:25" myfile$ touch myfileThis command will set bothmodification and accessdate/time for the file ordirectory.Set only modificationdate/time for the file ordirectory.Set only access date/time forthe file or directory.Will create a new empty filemyfile if it doesnt exist.
  43. 43. 5.3 Processes and jobs$ ps auxUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND...This command lists all currently running processes in the system.Output columns (this isLinux, output on other Unixes may slightly differ):USER = user owning the processPID = process ID of the process%CPU = it is the CPU time used divided by the time the process has been running%MEM = ratio of the process’s resident memory size to the physical memoryVSZ = virtual memory usage of entire process (including swapped memory)RSS = resident set size, the non-swapped physical memory that a task has usedTTY = controlling tty (terminal)STAT = multi-character process state (running, sleeping, zombie, etc.)START = starting time or date of the processTIME = cumulative CPU timeCOMMAND = command with all its arguments
  44. 44. ...$ ps aux | more$ ps aux | grep pname_or_idThis will allow listing long listof processes page-by-page.This is the way to search fora given process name orprocess id in the list ofprocesses. Only the linescontaining the process nameor id will be displayed.
  45. 45. ...$ sleep 5$ sleep 5 &This will wait 5 seconds andthen return the command lineprompt.This will run the sleepcommand in background andreturn the command lineprompt immediately, allowingyou do run other programswhile waiting for that one tofinish.
  46. 46. ...$ sleep 15 You can suspend the processrunning in the foreground bytyping ^Z, i.e.hold down the[Ctrl] key and type [z]. Thento put it in the background,type bg and [Enter].
  47. 47. 5.4 Listing suspended and backgroundprocesses$ jobs$ fg %2$ fg %1When a process is running,backgrounded or stopped, it will beentered onto a list along with a jobnumber.The output of this commandwill be such as this:[1] Stopped sleep 1000[2] Running vim[3] Running matlabThis will foreground the processnumber 2.This will resume and foreground thestopped process number 1.
  48. 48. 5.5 Killing/signalling a process$ sleep 5[Ctrl+c]$ kill pid$ kill -9 pid$ kill n[Ctrl+c] combination will killthe foreground process.This will kill the process usingits process id (you can get itfrom the output of pscommand).This will forcibly kill theprocess even if it hanged (orjust stopped).This will kill the job withnumber n from background.
  49. 49. ...$ sleep 15[Ctrl+z]$ kill -stop pid$ kill -cont pid$ pkill processname$ killall processnameThis will stop (temporarilysuspend) the process.This will stop (temporarilysuspend) the process.This will resume the stoppedprocess.To kill all processes with thename processname.To kill all processes with thename processname.
  50. 50. Chapter 6df, du, gzip, zcat, file, diff, find,history
  51. 51. 6.1 Other useful Unix commands$ df -h$ df -h .$ du -ahc mydirThis will show the amount ofused/available space on allmounted filesystems.This will show the amount ofused/available space only onthe current filesystem.This will show the disk usageof each subdirectory and fileof mydir directory, and mydirdirectory itself.
  52. 52. ...$ gzip myfile$ gunzip myfile$ zcat myfile.gz$ zcat myfile.gz | lessThis will compress myfileusing Gzip compressing tool.The original file will bedeleted.This will uncompress myfile.This will read gzipped fileswithout needing touncompress them first.And now you can view thegzipped file page-by-page.
  53. 53. ...$ file *$ diff file1 myfile2Classifies the named files inthe current directoryaccording to the type of datathey contain, for exampleASCII text, pictures,compressed data, directory,etc.Compares the contents oftwo files and displays thedifferences. Lines beginningwith a < denotes file1, whilelines beginning with a >denotes file2.
  54. 54. ...$ find . -name "*.txt" -print$ find . -size +1M -lsSearches for all files with theextension .txt, starting at thecurrent directory (.) andworking through all sub-directories, then printing thename of the file to the screen(simple output).To find files over 1Mb in size,and display the result as along listing (similar to lscommand output).
  55. 55. ...$ history | less$ set history=100This will give an ordered listof all the commands that youhave entered. Piping theoutput to less commandallows both forward andbackward scrolling of the list(more command only allowsforward scrolling).This way you can change thesize of the history buffer (setcommand changes runtimeparameters).
  56. 56. Chapter 7export, printenv, unset, .bashrc,source, ssh, mount, reboot,shutdown, crontab
  57. 57. 6.2 Environment variables$ export MYVAR=myvalue$ printenv$ printenv | grep MYPAR$ unset MYVARAdds a new environmentvariable MYVAR with valuevalue myvalue (exportcommand works forDebian/Ubuntu Linux).Prints all environmentvariables.Displays the value of MYVARenvironment variable.Unsets (deletes) environmentvariable MYVAR.
  58. 58. ...$ export $PATH:/mydir This way we can add newdirectories in the end ofPATH environment variable(all directories are divided by: symbol).
  59. 59. ...$ vi ~/.bashrc$ source ~/.bashrcThis way we can addenvironment variables onpermanent basis. Just insertexport MYVAR=myvalue inthe end of file opened in VI.This variable will be loadedautomatically at shell start.Force reload of environmentvariables from ~/.bashrc file.Note: This is for Bash, if youuse a different shell, youshould use another file.
  60. 60. 6.3 Remote shell$ ssh user@host$ exitThis way we can connect toanother Unix machine thathas OpenSSH server runningand port 22 opened. Uponconnect you will be asked toenter a password for user.host parameter can be ahostname or IP address.You can leave the remoteshell by entering exitcommand.
  61. 61. 6.4 Mounting filesystems$ mkdir mydir$ sudo mount -t vfat /dev/sdc1 mydirThis way we can create a mount point and mount FAT32filesystem to this mount point (only root user can do this).$ umount mydirAnd now we have unmounted the filesystem, the directory willbe empty.Noticed /dev/sdc1 ? This is a device file for the filesystem. If itexists, than the filesystem is physically present, but notmounted until we execute the mount command.Other fs types exist (-t option): ext3, ext4, reiserfs, ntfs, etc.
  62. 62. ...$ mkdir alpha$ sudo mount -t smbfs //alpha.softservecom.com/install alpha-o username=yourusername,password=yourpasswordThis way we can mount remote SMB network filesystem,providing credentials for authentication.If you want the filesystem to be mounted automatically, thenyou need to edit /etc/fstab file that has its own format (see theman page for details).
  63. 63. 6.5 Shutdown and Reboot$ sudo reboot$ sudo shutdown -h now$ sudo shutdown -h 18:45"Server is going down formaintenance"Reboot the systemimmediately.Shutdown the systemimmediately.Shutdown the system at 18:45.
  64. 64. 6.6 Scheduling$ crontab -eThis command opens crontab file where you can schedule commandsexecution. (use sudo if you need the command to be executed withroot permissions)The format of a line is:minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12),weekday (0-6, 0 = Sunday), commandExample:01 04 1 1 1 /usr/bin/somedirectory/somecommandThe above example will run /usr/bin/somedirectory/somecommandat 4:01am on January 1st plus every Monday in January.
  65. 65. ...An asterisk (*) can be used so that every instance (every hour, everyweekday, every month, etc.) of a time period is used. Example:01 04 * * * /usr/bin/somedirectory/somecommandThis command will run /usr/bin/somedirectory/somecommandat 4:01am on every day of every month.Comma-separated values can be used to run more than one instanceof a particular command within a time period. Dash-separated valuescan be used to run a command continuously. Example:01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommandThe above example will run /usr/bin/somedirectory/somecommand at01 and 31 past the hours of 4:00am and 5:00am on the 1stthrough the 15th of every January and June.
  66. 66. ...Usage: "@reboot /path/to/executable" will execute/path/to/executable when the system starts.string meaning@reboot Run once, at startup.@yearly Run once a year, "0 0 1 1 *".@annually (same as @yearly)@monthly Run once a month, "0 0 1 * *".@weekly Run once a week, "0 0 * * 0".@daily Run once a day, "0 0 * * *".@midnight (same as @daily)@hourly Run once an hour, "0 * * * *".
  67. 67. The end.