UNIX
Reference:
UNIX Concepts and Applications – 4th Edition
By
Handling Ordinary Files
• cat, more and less
• cp, rm and mv
• lp
• wc
• cmp, comm and diff
• tar
• gzip and gunzip
cat: Displaying and Creating Files
• cat is mainly used to display the contents of a
small file on the terminal.
$ cat dept.lst
• cat can also accept more than one filename as
arguments.
$ cat chap1 chap2
Using cat to create a file
$ cat > foo ……..
cp: Copying a File
• The cp command copies a file or a group of files. It creates an exact image of the file on the disk with a different name.
cp: Copying a File
The following example shows two ways of copying a file to the progs directory.
$ cp chap1 progs/unit1
(chap1 copied to unit1 under progs)
$ cp chap1 progs
cp: Copying a File
To copy a file .profile from /home/sharma to your current directory
$ cp /home/sharma/.profile .profile
(Destination is a file)
$ cp /home/sharma/.profile .
cp: Copying a File
To copy the files chap1, chap2 and chap3 to the progs directory
$ cp chap1 chap2 chap3 progs
cp: Copying a File
$ cp chap* progs
(Copying all files beginning with chap.)
• The cp –R command behaves recursively to copy an entire directory structure, say progs to newprogs
$ cp –R progs newprogs
cp: Copying a File
• If newprogs does not exists, cp creates it along with the associated subdirectories. But if
newprogs exists, progs becomes a
rm: Deleting File
• The rm command deletes one or more files.
$ rm chap1 chap2 chap3
(command to delete 3 files)
• A file once deleted can’t be recovered.
• rm won’t normally remove a directory, but it
rm: Deleting File
$ rm progs/chap1 progs/chap2
or
$ rm progs/chap[12]
• To delete all files
rm: Deleting File
$ rm –r *
rm: Deleting File
$ rm –rf *
mv: Renaming File
• The mv command renames (moves) files. It has two distinct functions –
– It renames a file (or directory)
rm: Deleting File
$ mv chap1 man1
(To rename the file chap1 to man1. If the destination file does not exists, it will be created.)
$ mv chap1 chap2 chap3 progs
rm: Deleting File
$ mv pis perdir
(mv command rename a directory pis to perdir.)
CHECK IT YOURSELF –
more: Paging output
• UNIX offers the more pager (originally from Berkeley) which has today replaced pg, the original pager of UNIX.
• Linux also offers more but less is its standard pager.
more: Paging output
$ more chap1
(press q to exit)
lp: Printing a file
• No user is allowed direct access to the printer. Instead, one has to spool (line up) a job along with others in a print queue.
lp: Printing a file
$ lp rfc822.ps $_
file: Knowing the file type
• We know that files are of three types.
• We may often need to know more about these files.
• For example, a regular file may contain plain text, a C program or executable code.
file: Knowing the file type
$ file a.zip
a.zip:ZIP archive
$ file * (* signifies all files)
fork1.c: c program text a.sh: commands text
file: Knowing the file type
• This command identifies the file type by examining the magic number that is embedded in the first few bytes of the file.
• Every file has a unique magic number.
• file recognizes text files and can distinguish
wc: Counting lines, words and
characters
$ wc chap1
3 20 103 chap1
• wc counts 3 lines, 20 words and 103
wc: Counting lines, words and
characters
• A line is any group of characters not containing a newline.
• A word is a group of characters not containing a space, tab or newline.
wc: Counting lines, words and
characters - options
$ wc –l chap1
3 chap1 (Number of lines)
$ wc –w chap1
20 chap1 (Number of words)
$ wc –c chap1
103 chap1 (Number of
cmp: Comparing two files
$ cmp chap1 chap2
chap1 chap2 differ: char 9, line 1
cmp: Comparing two files
• By default, cmp does not bother about possible subsequent mismatches but displays a detailed list when used with –l option.
• If the two files are identical, cmp displays no message, but simply returns the prompt.
comm: What is common
File1 File2
c.k. shukla anil aggarwal
chanchal singhvi barun sengupta
s.n. dasgupta c.k. shukla
sumit chakrobarty lalit chowdury
comm: What is common
• To use comm command we require two
sorted files.
$ comm file1 file2
comm: What is common
• The first column contains lines unique to the first file.
• The second column contains lines unique to the second file.
comm: What is common
• To drop a particular column, simply use its column number as an option.
$ comm -3 file1 file2
(selects lines not common to both files)
$ comm -13 file1 file2
diff: Converting one file to another
• diff command can be used to display file
differences.
diff: Converting one file to another
• Here are the contents of files email and addresses used in this example.
diff: Converting one file to another
• To compare the contents of two files:
$ diff email addresses
2a3,4
> Jean [email protected] > Jim jim@frolix8
diff: Converting one file to another
diff email addresses 2a3,4
> Jean [email protected] > Jim jim@frolix8
tar: Creating an archive file
• To create an archive file use the command:
tar cvf tarfile_name.tar filenames
(directory_name)
• The command options cvf modify the way the tar command works.
• See the manual page for details.
tar: Creating an archive file
• When you create a tarfile of a directory all the subdirectories are added; this can result in very large tarfiles.