Sunday, 6 March 2016

Useful Unix Commands

1)cat - display or concatenate files

cat takes a copy of a file and sends it to the standard output (i.e. to be displayed on your terminal, unless redirected elsewhere), so it is generally used either to read files, or to string together copies of several files, writing the output to a new file.

cat ex
displays the contents of the file ex.

cat ex1 ex2 > newex
creates a new file newex containing copies of ex1 and ex2, with the contents of ex2 following the
contents of ex1.

2)cd - change directory

cd is used to change from one directory to another.

cd dir1
changes directory so that dir1 is your new current directory. dir1 may be either the full pathname
of the directory, or its pathname relative to the current directory.

changes directory to your home directory.

cd ..
moves to the parent directory of your current directory.

3)chmod - change the permissions on a file or directory

chmod alters the permissions on files and directories using either symbolic or octal numeric codes. The symbolic codes are given here:-

u user + to add a permission r read
g group - to remove a permission w write
o other = to assign a permission explicitly x execute (for files), access (for directories)

The following examples illustrate how these codes are used.
chmod u=rw file1
sets the permissions on the file file1 to give the user read and write permission on file1. No
other permissions are altered.

chmod u+x,g+w,o-r file1
alters the permissions on the file file1 to give the user execute permission on file1, to give
members of the user’s group write permission on the file, and prevent any users not in this group
from reading it.

chmod u+w,go-x dir1
gives the user write permission in the directory dir1, and prevents all other users having access to
that directory (by using cd. They can still list its contents using ls.)

4)compress - compress a file

compress reduces the size of named files, replacing them with files of the same name extended by .Z .
The amount of space saved by compression varies. If no saving of space would occur, then the file will not be altered.

compress file1
results in a compressed file called file1.Z, and deletes file1.

compress -v file2
compresses file2 and gives information, in the format shown below, on the percentage of the
file’s size that has been saved by compression:-

file2 : Compression 50.26 -- replaced with file2.Z
To restore files to their original state use the command uncompress. If you have a compressed file
file2.Z, then

uncompress file2
will replace file2.Z with the uncompressed file file2.

5)cp - copy a file

The command cp is used to make copies of files and directories.

cp file1 file2
copies the contents of the file file1 into a new file called file2. cp cannot copy a file onto itself.

cp file3 file4 dir1
creates copies of file3 and file4 (with the same names), within the directory dir1. dir1 must
already exist for the copying to succeed.

cp -r dir2 dir3
recursively copies the directory dir2, together with its contents and subdirectories, to the directory
dir3. If dir3 does not already exist, it is created by cp, and the contents and subdirectories of
dir2 are recreated within it. If dir3 does exist, a subdirectory called dir2 is created within it,
containing a copy of all the contents of the original dir2.

6)date - display the current date and time

date returns information on the current date and time in the format shown below:-
Tue Mar 25 15:21:16 GMT 1997

It is possible to alter the format of the output from date. For example, using the command line
date ’The date is d/m/y, and the time is H:M:S.’

at exactly 3.10pm on 14th December 1997, would produce the output
The date is 14/12/97, and the time is 15:10:00.

7)diff - display differences between text files

diff file1 file2 reports line-by-line differences between the text files file1 and file2. The default
output will contain lines such as n1 a n2,n3 and n4,n5 c n6,n7 , (where n1 a n2,n3 means that file2
has the extra lines n2 to n3 following the line that has the number n1 in file1, and n4,n5 c n6,n7
means that lines n4 to n5 in file1 differ from lines n6 to n7 in file2). After each such line, diff prints
the relevant lines from the text files, with < in front of each line from file1 and > in front of each line
from file2.

There are several options to diff, including diff -i, which ignores the case of letters when comparing
lines, and diff -b, which ignores all trailing blanks.

diff -cn
produces a listing of differences within n lines of context, where the default is three lines. The
form of the output is different from that given by diff, with + indicating lines which have been
added, - indicating lines which have been removed, and ! indicating lines which have been

diff dir1 dir2
will sort the contents of directories dir1 and dir2 by name, and then run diff on the text files
which differ.

8)echo - echo arguments to the standard output

echo echoes given arguments to the standard output, and is generally used in shell programs.

echo argument1
writes argument1 to the standard output.

9)file - determine the type of a file

file tests named files to determine the categories their contents belong to.

file file1
can tell if file1 is, for example, a source program, an executable program or shell script, an
empty file, a directory, or a library, but (a warning!) it does sometimes make mistakes.

10)mkdir - make a directory

mkdir is used to create new directories. In order to do this you must have write permission in the parent directory of the new directory.

mkdir newdir
will make a new directory called newdir.

mkdir -p can be used to create a new directory, together with any parent directories required.

mkdir -p dir1/dir2/newdir
will create newdir and its parent directories dir1 and dir2, if these do not already exist.

No comments:

Post a Comment