Programming in UNIX Environment : Commands – sort, tail, cmp, diff

The command ‘sort’ is for sorting the line of the text. ‘tail’ prints the last ten lines of the file. ‘cmp’ for showing the difference between the files. It shows only the first difference. But ‘diff’ shows all the difference between two files.

sort filename1 file2
sort -r // reverse normal order.
sort -n // numeric order.
sort -f // fold upper and lower case together.
sort -b // Ignore the leading blanks.

tail filename // prints last 10 lines.
tail -3 filename // print last 3 lines.

cmp filename1 filename2 // prints only the 1st difference.

diff filen1 filen2 // prints all differences.

pwd – For printing current working directory.
cp – For copying a file.

cp /home/abhi/Pclass/num.c num.c

ed /home/abhi/Pclass/num.c

Advertisement

Programming in UNIX Environment : Commands – mv, rm, wc, grep

The ‘mv’ command is using for renaming. And ‘rm’ command for removing files. ‘wc’ for counting lines,words,characters. The ‘grep’ command is for searching a word. ‘-v’ for searching all words except the indicating word in a line.

mv filename newname

rm filename1 filename2

wc filename1 filename2 // count lines,words,characters.
wc -l //only counts the new lines.
wc -w //print the word count.
wc -m //print the character counts.
wc -c //printf the byte counts.

grep int filename // for searching the word int.
grep -v int filename // all except int.

Programming in UNIX Environment : Commands – kill, ps, nohup, nice

The ampersand at the end of the command says that run this command and also take another from the terminal without stopping the first. It prints the process id. For stopping a process we use ‘kill’ command followed by process id. ‘ps’ command prints what that user running. Using ‘nohup’, the command will run even if the user logged out. ‘nice’ is for using another system to run our job.

$wc a* >wc.out &

$kill 6578

$ps

$ps -ag // all process that are currently running.

$nohup command & // No hangup for this command even if user logged out.

$nice expensive-command & // if your command take a lot of processor resources.

The ‘at’ command followed by time is to run our job at whatever time we want.

$ at 2340 <file

There is a file ‘.profile’ in the user’s login directory, this file contains commands which the user want to run when the user logged in. A user can set things in it.

a=/long/directory/name

$cd $a

We can put ‘a’ as given above in our profile. So we can refer that directory by the value of shell variable.

Unix file system : Devices

/dev/rp00 and /dev/rp01 are named after the DEC RP06 disc drive attached to the system. We cannot make a link to a file in another subsystem. One reason is that the inode numbers are not unique in different file systems.
$ df
$ tty
$ mesg n
$ mesg y

The ‘df’ command tells the available space in the mounted file subsystem. The ‘tty’ command tells which terminal we are using. So we can send files and informations to that terminal and it appears in the corresponding terminal. ‘mesg n’ turn off the messages. /dev/tty is a synonym for our terminal.

If we want to run a program and its output files are not importent we can redirect it to ‘/dev/null’ causes its output to be thrown away.

Unix file system : The directory hierarchy

When the system starts the file /boot is read, it then reads /unix (it is the program for the UNIX kernal itself).

The following shows some directories which reside in root and its contents.

/bin : basic programs like who,ed reside.

/etc/rc : This is a file of shell commands which is executed after the system is bootstrapped.

/etc/group : lists the members of each group.

/lib : Contains the parts of the C compiler.

/tmp : Contains temporary (short-lived) files that are created in the execution of programs.

Unix file system : Inodes

The administrative infomation is stored in the inode. It contains information such as how long it is, where the contents of the file are stored on disc etc.
System’s internal name for a file is its i-number.

$ ls -lut
$ ls -lct
$ ls -i
$ ls -ict

‘rm’ command is for removing a file. ‘mv’ command is for moving and renaming the files.

$ rm filename1 filename2
$ mv filename1 filename2

The -ut lists the file in order of usage time. -ct lists the files in the order of inode change time. The ‘-i’ option is for listing the i-numbers of the directories contents. ‘-ict’ lists inode number in order of the modifications in the inode (most recently first).

Unix file system : Permissions

The file /etc/passwd is the password file. It contains all the login information about each user. We can discover each users id and group id by looking name in /etc/passwd.

$grep name /etc/passwd

The fields in the password file looks like ‘login-id:encrypted password:uid:group-id:miscellany:login-directory:shell’. The -l option of ls command prints the permissions.

$ls -l

We can find a string like ‘-rw-r–r–‘ . The first – means that it is an ordinary file. If it is a directory there prints a ‘d’. The next three field is the permissions of the owner followed by the next three field is the permissions of the group and the next is the others permissions. We cannot edit the passwd file . But the super user can do it.

The ‘chmod’ command changes the permissions of a file. ‘permissions’ may occupy octal numbers. The ‘+’ sign is for ‘ON’ permission modes and to OFF the ‘-‘ sign is using. The ‘-w’ option is for turn off the write permisssion for all incliding the owner. ‘+x’ allows every one to execute the following command.

$ chmod permissions filename
$ chmod -w filename
$ chmod +x command
$ chmod -w .
$ chmod 775 filename //Restores the permission.

The ‘-w .’ means removing the write permission of all files in that directory.