You are on page 1of 12

UNIX Programming Lab K.

Murugan

UNIX COMMANDS
The basis of all UNIX interaction is the command. Some simple but useful UNIX command is given in this section. Command Syntax: Commands are entered at the shell prompt. You must see the prompt, such as the korn shell $ prompt, before you can enter a command. UNIX is case sensitive. Every command must have a verb and may also have options and arguments. The command format is: $ verb options(s) argument1, argument2 argumentn The verb is the command name. The option modifies how the action is applied. The argument provides additional information to the command.

I. COMMON COMMANDS 1. date Date command gives us the current date and time. Syntax : $date Mon Jan 2 08:45:24 PST 2009 Arguments:
Format Code a A b B d e D H I j m M n p r R S t T U W y Y Z Explanation Abbreviated weekday name, such as Mon Full weekday name, Monday Abbreviated month name ,such as Jan Full month name, January Day of the month with 2 digits such as 01,02..31 Day of the month with spaces replacing leading zeros such as 1, 2... Date in the format mm/dd/yy Military time two -digit hour such as 00,01.23 Civilian time two-digit hour such as 00,01,,,12 Julian date such as 001,002.366 Numeric two-digit month such as 01,02.12 Two-digit minute such as 00,01..59 New line character Display am/pm Time in format hour:minute:second with am/pm Time in format hour: minute Seconds as decimal number[00-61] allows for leap seconds Tab character Time in format hour:minute:second Week number of year such as 00,01.53 Week of year [00-53] with Monday being first day of week; all days preceding the first Sunday of the year are in week 0 Year with century(offset from %C) as a decimal number[00..99] Year as ccyy [four digits] Time zone name, or no characters if no time zone is determinable

UNIX Programming Lab K.Murugan 2. Calendar (cal) The calendar command, cal, displays the calendar for a specified month or a year Syntax: $cal Example: $cal 1 2009 (displays the calendar for January 2009) January 2009 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3. who who command gives us the details of the users who have logged in to the UNIX system currently. Syntax : $who sunuser25 sunuser26 sunuser27

tty0 tty5 tty2

Jan 02 10:23 Jan 02 11:10 Jan 02 11:29

Example: $who H (-H option, displays a header for each column) NAME LINE TIME sunuser25 tty0 Jan 02 10:23 sunuser26 tty5 Jan 02 11:10 sunuser27 tty2 Jan 02 11:29 4. who am i who am i command gives your user ID.It is also used to show the user id on a report. Syntax : $who am i Example: user20 ttya Sep 18 20:02

5. Change password (passwd) The password command, passwd, is used to change your password. It has no option or attributes but rather does its work through a dialogue of questions and answers.

Syntax: $passwd Changing password for Old password: New password: Re-enter new password:

UNIX Programming Lab K.Murugan 6. Print message (echo) The echo command copies its argument back to the terminal

Syntax: $echo <argument> Example : $echo hello world hello world 7. Online Documentation (man) The man command displays online documentation. When you cant remember exactly what the options are for a command, you can quickly check the online manual and look up the answer. Syntax: $man cal Example: $man cal Cal(1) NAME Cal print calendar SYNOPSIS Cal [ [month ] ] 8. Terminal (tty) The tty utility is used to show the name of the terminal you are using. Syntax: $tty Example: $tty /dev/ttyq0 9. Clear screen (clear) The clear command clears the screen and puts the cursor at the top. Syntax: $clear II TYPES OF FILES There are seven file types in UNIX. They are,

i. Regular files: Regular files contain user data that need to be available for future processing. Sometimes called ordinary files. 1. Text files: o A text file is a file of characters drawn from the computers character set. 2. Binary files: o A binary is a collection of data stored in the internal format of the computer. The two types of binary files are data files: contain application data, and program files: contains instructions that make a program work. 3

UNIX Programming Lab K.Murugan ii. Directory files: o A directory is a file that contains the names and locations of all files stored on a physical device. iii. Character special files: o A character special file represents a physical device, such as a terminal, that reads or writes one character at a time. iv. Block special files: o A block special file represents a physical device, such as a disk, that reads or writes data a block at a time. v. Symbolic link files: o A symbolic link is a logical file that defines the location of another file somewhere else in the system. vi. FIFO files: o A first-in, first-out file, also known as a named pipe, is a file that is used for interprocess communication. vii. Socket: o A socket is a special file that is used for network communication

III. FILE COMMANDS 10. cat cat command help us to list the contents of a file we specify. If we do not specify the file name, it takes input from standard input and saves as a new file with a Ctrl+d at the end, which takes to $ prompt again. In unix Ctrl +d indicate the EOF or end of file character. Syntax :$cat [option..] [filename]

Example1: $cat >sample1 Welcome to seminar! <Ctrl+d> $cat sample1 Welcome to seminar! Example 2:$cat sample1 > sample2 This would create sample2 which contains the contents of sample1. Example 3:$cat sample 1 sample2>>sample3 Welcome to seminar! Welcome to seminar! Append output redirection operator (>>) is used to append the contents of sample1 and sample 2 to the file sample3.

11. cp cp command is used to create duplicate copies of ordinary files. If the source file is a file, the new file contains an exact copy of the data in the source file. If the source file is a directory, all the files in the directory are copied to the destination, which must be a directory. If the destination file

UNIX Programming Lab K.Murugan already exists then the original contents are erased and the source file contents are copied. The cp command copies both text and binary files. Syntax : $cp [source filename] [destination filename] Example: $cp sample2 sample3 12. mv The move command is used to move an old file to a new file, where the old file no longer exists in the directory. Syntax : $mv [old name] [new name] Example: $mv file1 file2

13. rm The rm command is used to remove one or more files from a directory. This can be used to delete all files as well as the directory. Syntax: $rm [option]filename Example: $rm file1 We can use i and r option to interact with the rm command and then delete a directory. rm-i asks the user if he wants to delete the file mentioned. When this is combined with r option, rm asks whether to examine each file in that directory. rm-r recursively delete the entire contents of the directory as well as the directory itself.

14. ln Using link (ln) command we can establish additional file names for the same file .changes made to file using either original name or the second name is reflected in both. Syntax: $ln [firstname] [secondname] Example: $ text1 text2

IV WILDCARD A wildcard is a token that specifies that one or more different characters can be used to satisfy request.In otherwords,wildcards are like blanks that can be filled in by any character. The 3 wildcards in UNIX are: the single character (?)wildcard Example1: File Matches Does Not Match c? c1 c2 c3 ca ac cat c?t cat cet cit clt cad dac

UNIX Programming Lab K.Murugan the set ([]) wildcard Example2: File Matches Does Not Match f [aoei]d fad fed fod fid ac cat f [a-d]t fat fbt fct fdt fab fet the multiple character (*) Example3: wildcard Matches Example Does not Match every file * every file whose name begins with f f5c2 afile,cat f* Example4:$ls cha* Char1.c Characters.txt Example5:$ls char?.? Chara.c Charb.c Example6:$ls ex[1-3] Ex1 Ex2 Ex3 V. DIRECTORY COMMANDS 16. pwd pwd command displays the full pathname for the current directory. It has no options . Syntax :$pwd Example: $pwd /sunuser56/temp/programfiles 17. ls Displays the list of files in the current working directory. ls command comes with different options. Syntax :$ls Example 1:$ls Abs.java Rep.text Some of them are listed below, ls-l lists files in the long format. The files are displayed along with their mode ,number of links ,owner of file ,file size ,modification date and time and the file name. ls-t lists in order of last modification time(most recent first) 6

UNIX Programming Lab K.Murugan ls-a ls-d ls-p ls-u lists all entries, including the hidden files lists directory file instead of its contents puts a slash after each directory lists in order of last access time

Example 2: $lsl Total 3 drwxr--r-- 1 sunuser group1 45 Jan 01 2009 12:13 sample.txt -rw-rw-rw 4 sunuser group1 20 Jan 02 2009 09:28 inherit.java Example3: $ls l total 2 -rw-r--r--rw-r--r-1 1 sunuser sunuser staff staff 12 May 17 08:45 file1 12 May 17 08:45 file2

file type perm links owner group file size -issions 18. mkdir This command is used to create a new directory. Syntax :$mkdir directory name

last mode file name

Example: $mkdir 20080501 To verify the creation of the above directory, use the ls command. 19. cd cd command is used to change from the working directory to any other directory specified. There are no options connected with this. Syntax :$cd

Example1: $cd bin $pwd \bin Example2: $cd The command cd without argument bring you back to the home directory. 20. rmdir rmdir command is used to remove a directory specified in the command line. it requires that the specified directory to be empty before removing it. Syntax : $rmdir [directory name]

UNIX Programming Lab K.Murugan Example: $rmdir 20080501 VI REDIRECTION COMMANDS OUTPUT REDIRECTION When output from a process is redirected to sources other than the standard output, it is called Output Redirection. Syntax :$command > file Example: $date >today $cat today Fri Mar 10 10:00:02 PST 2006 INPUT REDIRECTION We can redirect the input for a command using input redirection. Syntax : $ command < file Example: $ cat < names Roopa Mini REDIRECTING BOTH STANDARD INPUT AND STANDARD OUTPUT Syntax : $command <source> destination Example:$cat <names> list 21. wc wc command is used to count the number of characters,words,and lines in one or more documents. Wc command is capable of accepting input directly from the keyboard. By entering wc without arguments, it waits for the user to type in the input. On terminating input use Ctrl + d. Syntax : $wc [-loc] file Example: wc file1 2 12 50 file1 To count only the lines, words or characters use l,-w,-c individually. 22. find find command is used to locate the files in a directory and in a subdirectory. Syntax : $find [option] Example: find. print . ./.profile ./.last login

UNIX Programming Lab K.Murugan -name option -type option Lists out the specific files in all directories beginning from the named directory. Used to identify whether the name of files specified are ordinary files or directory files. If directory file the use type d option, if ordinary file then use -type f. Used to find a file which has been modified before of after a specified time. $-exec games/play type f name p* mtime +5 -exec rm {} \; Helps in executing the command that follows it on those files found by the find command. The only difference between the ok and exec Option is that ok option is interactive in nature.

-mtime option -exec option -ok option

23. head and tail head command is used to display the initial part of a text file and the tail command displays the last part of a file . By default head command displays first 10 lines of a file and tail command displays the last 10 lines of a file. Syntax :$ head [ - count ] [filename ] Example: $ head -3 sample One Two Three Syntax : $ tail [+|- count ] [filename ] 24. grep The grep command is used to search for a particular pattern from a file or from the standard input and display those lines on the standard output, grep stands for global regular expression print. Example: $ grep n ;$ The Raven | head -5 OPTION -v -c -n -i DESCRIPTION Displays only those lines that do not match the specified pattern. Displays only the count of the lines that match the specified pattern. Displays those lines that match the specified pattern along with the line number at the beginning of the line. Displays those lines that match the specified pattern ignoring the case distinction.

Wild card patterns can also be used with grep command.

Example1: $grep new[a-c] new newa , newb and newc Example2: $grep e$ new 9

UNIX Programming Lab K.Murugan This is a new file Newa ,newb and newc are Example3: $grep ^t new This is a new file There are two types of files egrep The egrep command is an extension of the grep command. egrep means extended global search for regular expression. Multiple patterns can be searched in a file by issuing a single command. These multiple patterns should be separated by a pipe ( | ) symbol. Example:$ cat countries france india $egrep f countries data france europe india asia fgrep fgrep stands for fixed grep . this command is used to extract only fixed strings without the use of any regular expression. Example:$ cat old this is a test for files Why this is possible $fgrep this is old this is a test why this is possible 25. sort The sort utility uses options, field specifiers, and input files. The field specifiers tell it which fields to use for the sort. Option Check sequence Alternate delimiter Numeric sort Merge Unique sort fields Reverse order Ignore leading blanks Dictionary dort Fold lowercase Code -c -t -n -m -u -r -b -d -f Results Verifies that data are correctly sorted Specifies alternate delimiter.Default:tab and space Data are numeric, not string.Default:string data Input files are sorted. Merges them. Deletes duplicate sort fields, keeping last one Sorts data in descending sequence Considers leading blanks as one blank for field separation Sorts special characters first Sorts upper- and lowercase together

Syntax: sort[-options][field-specifiers][file-list]

10

UNIX Programming Lab K.Murugan Example: $ sort +0 -2 file1 26. pipe A pipe is a mechanism in which the output from one command can be redirected to input to another command. The pipe command is represented by a (|). Example1: $who | wc l 16 who command gives us the details of the users who have logged in to the UNIX system currently. Example2:$cat text | head -3 This Is UNIX VII FILTER Filter work naturally with pipes. Because a filter can send its output to the monitor, it can be used on the left of a pipe; because a filter can receive its input from the keyboard, it can be used on the right of a pipe. In other words, a filter can be used on the left of a pipe, between two pipes, and on the right of a pipe. The following commands are comes under filter. Cat, pg, more, head, tail, grep, sort, tee, tr, cut, paste, wc 27. cut cut is to extract one or more columns of data from either input or from one or more files Option Character Field Delimiter Suppress Code -c -f -d -s Results Extracts fixed columns specified by column number Extracts delimited columns Specifies delimiter if not tab (default) Suppresses output if no delimiter in line

Syntax: cut[-options][file-list] Example:$ cut c1-14,19-25 censusFixed 28.paste The paste command combines lines together. It gets its input from two or more files. Syntax: paste [-options][file-list] Example: $ paste file Odd file Even 29. cmp cmp examines two files byte by byte. The action it takes depends on the option code used. -l : list -s : Suppress list 11

UNIX Programming Lab K.Murugan Syntax: cmp [-options]file1 file2 Example: 1 $ cmp -l cmpFile1 cmpFile2 Example: 2 $ cmp cmpFile1 cmpFile1.cpy Echo $? 30. uniq uniq command deletes duplicate lines, keeping the first and deleting the others. To be deleted, the lines must be adjacent. Duplicate lines that are not adjacent are not deleted. To delete nonadjacent lines, the file must be sorted. -u: only nonrepeated lines output -d: only repeated lines output -c: add count to lines -f: field -s: character Syntax: unique [-options] [input_file] Example: uniq d uniqfile 31. comm comm Command finds lines that are identical in two files. It compares the files line by line and displays the results in three columns. Syntax: comm. file1 file2 Example: $comm comm1 comm2 32. tr tr command replaces each character in a user-specified set of characters with a corresponding character in a second specified set. -d: delete characters -s: squeeze duplicates -c: complement set Syntax:tr [-options] [string 1] [string 2] Example: $ tr s ie dd 33. tee Tee command copies standard input to standard output and at the same time copies it to one or more files. -a: append Syntax: tee [-options] file-list Example: $tee teeout

12

You might also like