UNIX Commands A Brief Look at Common UNIX Commands Lect. 3A 1 Using Unix System Commands Most Unix commands use several options. Generally, your command line will start with the name of the command, followed by the option or options you want to use, followed by the arguments or arguments, if any. Options are usually specified with a - ( minus sign.) For example: ls -l memo Is the ls command with the -l option, and with memo as its sole argument. This causes information about the file memo in your current directory, if it exists, to be listed in long form. ls -l Is the ls command with -l option. It causes files in your current directory to be listed in long form. ls memo Is the ls command with no options, and the argument memo. This command will print the name of the file memo if it exists in the current directory. Lect. 3A 2 Finding online Manual information by Keyword ( man ) Command: man “command / topic” - displays the default manual entry for the topic or command. Examples: Use capital -K for Linux machines Lect. 3A $ man date - display the “date” manual page $ man -k mount - list all commands that have the string “mount” in their discriptions $ man 1m mount - display the “mount” manual page section 1m. $ man 3m mount - display the “mount” manual page section 3m. At the bottom of the manual pages one will find related files which often aid the user in finding more information about a particular topic. 3 The Unix File System Hierarchy “root” / bin usr ls bin man man tmp contrib bin bin vi stand kuss local pine Lect. 3A dev passwd mv mail etc users user1 user2 tmp = directory = file 4 The dot (.) and dot dot (..) directories When any directory is created, two entries are automatically created called dot (.) and dot dot (..) These are commonly used when designating relative pathnames. By using the .. one can traverse up through the hierarchy. Dot (.) The entry called dot represents your current directory position. If you are currently in the directory: /users/user3: . represents the curent directory /users/user3 ./file.dat represents the file: /users/user3/file.dat ./memo/file.dat represents the file: /users/user3/memo/file.dat Dot Dot (..) The entry called dot dot represents the directory immediately above your current directory position, often refered to as the parent directory. Lect. 3A If you are currently in the directory: /users .. represents the / directory ../.. also represents the / directory ../tmp represents /tmp directory ../tmp/file.dat represents /tmp/file.dat file 5 Path Names Absolute Pathname - Gives the complete designation of the location of a file or directory. - Always starts at the top of the hierarchy ( the root “ / “ ) - Not dependent on your current location in the hierarchy. - Always is unique across the entire hierarchy. Eamples of files called numbers.dat /tmp/numbers.dat /users/user1/data/numbers.dat /users/user2/numbers.dat /users/user3/memos/numbers.dat Relative Pathname - Always starts at your current location in the hierarchy. - Never starts with a / unless you are sitting in the root directory - Is unique relative to your current locations only. - Is often shorter than the absolute pathname. Examples that again reference the files numbers.dat but assuming the current position is the directory: /users Lect. 3A user1/data/numbers.dat user2/numbers.dat 6 Display your current working directory ( pwd ) Command: pwd Example: login: user3 password: ****** Welcome to machine mpd306 $ pwd /users/user3 $ cd ../user4 $ pwd /users/user4 The pwd command reports the absolute pathname to your current directory location in a UNIX systems file system, and is a shorthand notation for Present Working Directory. This command is very useful when you are traversing the directory structure and want to know where you presently stand. Lect. 3A 7 Methods to List the Contents of a Directory ( ls ) Command: ls [ -adlFR ] [ directory_name ] The ls command is used to List the names of files and directories. With no arguments, ls displays the names of the files and directories under the current directory. ls will accept arguments designating a relative or absolute pathname of a file or directory. The command: ls -l is often used to display the owner, group, and mode of the file or directory. The HP-Ux Equivalent of the file is the command: ll If the directory is too long to display on one screen this command is often “piped” to a second UNIX command called: more. The total command would be typed like this: ls -l | more The ls command supports many options. Multiple options may be supplied on a single command line to display more complete file or directory information: Lect. 3A -a Lists all files including those whose names start with a dot (.) Normally these dot files are “hidden” except when the -a options is specified. These dot files commonly hold configuration information for your user session or applications. -l Provides a long listing which describes attributes about each file, including: type, mode, number of links, owner, group, size (in bytes), the modification date, and the name. 8 Permissions Displayed with ls -l mode - rw- r-- r-- 1 user3 class 37 - rwx r-x r-x 1 user3 class 37 d rwxr-x r-x 2 user3 class 1024 owner type owner access world access group access Lect. 3A Jul 24 11:06 Jul 24 11:07 Jul 24 11:09 group file1 file2 docs file name Size (bytes) Last edit time Number of links to this file. 9 Display Contents of Files ( cat / more / head / tail ) Command: cat [ filename ......] The cat command will concatenate and display the contents of file(s). If the file is too big for the terminal’s screen, the text will go by too quickly to read. A pipe can be used to direct the output of one command to the input of another command. The more command is used to scroll the text to the screen one page at a time. The following command is used: cat filename | more If a file contains control characters, such as a compiled program, and you cat it to your terminal, your terminal may become disabled. Command: more [ filename ..... ] The more command prints out the contents of the named files to the screen one page at a time. To see the next screen of text you press the space bar. To see only the next line you press the return key. To quit from the command, use the q key. Command: tail [-n] [filename] The tail command is used to display the last n lines of a file. The default is 10 lines if it is not specified. This command is useful if you only want to see the last few lines in the file, and don’t want to scroll through the entire file with the cat or more commands. Lect. 3A 10 Change From one Directory to Another Command: (cd) cd [ dir_pathname ] The cd command allows you to change directory, and move to some other location in the hierarchy. The path can be specified as an absolute or relative pathname. When executed with no arguments, the cd command will return you to you login or HOME directory. The command is used if a user is lost within the UNIX directory structure and wishes to return to the HOME directory. The pwd command is used to tell where one is presently located. Example: $pwd /users/user3 $cd memo; pwd /users/user3/memo $cd ../..; pwd /users cd /tmp; pwd /tmp Lect. 3A 11 Create or Remove Directories ( mkdir / rmdir ) Command: mkdir [ -p ] dir_pathname rmdir dir_pathname The mkdir command allows you to make a directory. When each directory is created, it is automatically given the two sub-directories: dot (.) and dot dot (..), representing the current and parent directories. Note: creating the directories does not change your location in the hierarchy. By default, when specifying a relative or absolute path to the directory being created, all intermediate directories must exist. Alternatively, you can use the option: -p Creates intermediate directories if they do not already exist. Example: the following command would make the fruit directory if it did not already exist: $ mkdir -p fruit/apple fruit/grape fruit/orange Lect. 3A 12 Remove Files ( rm ) Command: rm [ -if ] filename [ filenames ......] individual files Removes The rm command is used to remove files. The files are irretrievable once they are removed. The command will take many arguments, file names or directory names. The following are options: -i Interrogate or interactive mode which requires that the user confirm that the removal be completed. One responds with “y” or “n”. Entering return is the same as no. This is often useful when a file has been created with binary control characters and is difficult to remove. rm -i *filename* Example: Lect. 3A $ ls -F file1 file2 documents/ memo/ $ rm file1 $ ls -F file2 documents/ memo/ $ rm -i file2 file2 ? y $ rm -i documents rm: documents directory 13