Untitled Document

Chapter - 5 File Handling Commands

In our previous chapter we have seen command by which a user can create , delete and go to the directories and files at different locations .

In this chapter we will see commands which will help us to work on the files for our day to day task .

cat : Display File contents -

This is the most comman command used in Unix/Linux to dispaly the content of a file . To display the content of file test.txt on screen run below command

cat test.txt

We can even dispaly multiple file using cat command e.g test.txt , report.csv .The command display the contents of first file on the screen and then second

cat test.txt report.csv

Like other commands , cat commands also have option - we will see the two different options (-v and -n) in our current text .

  • Displaying Non Printing Characters (-v) - some times we have the ascii characters in our files which prints junk of characters on screen .To print these characters we can provide -v oprion
  • Numbering Lines (-n) - cat when used with -n option will dispaly the line numbers . If your cat command doesn't support -n option then you can use pr command to do the same job

Creating file using cat command -

We can even create a file using cat command. The command is useful in creating the small files . e.g - type the following code on your terminal and see the result . Run cat command with The > (right chevron) chracter and then type your text , to end your text press [ctrl-d] which will act as end of file character( to terminate cat command) which tells unix that no furher i/p will be provided to cat .

 cat > abc.txt [ press-enter]  
type your text here [press enter after each line]
 [ctrl-d] will act as end of file

To verify this simply run cat abc.txt

cp : Copy Command -

cp command copies file or group of files from source to destination . Lets see by examples how cp copies files and directories from one place to other .

cp test.txt test1.txt

The above command copies file test.txt to test1.txt , If destination file doesn't exists then command first creates it then copying takes place . If destination file exists then it simply overwrites the file without giving any warning from the system .So , before running cp command , just check with ls command whether destination file exists or not .

If we have only one file to be copied , the destination can be either an ordinary file or directory . Then following commands shows two ways of copying a file to the scipts directory .

cp test.txt scripts/test1.txt -- test.txt copied to test1.txt under scripts directory
cp test.txt scripts -- test.txt copied as it is ( retains its name ) under scripts directory

cp support the shorthand notation dot (.) to represents current directory as destination . Consider that you are in some other location and you want to copy status.ksh file to be copied tou your current directory , then here is the cp command for it .

cp /home/singh/status.ksh .status.ksh -- Destination is a file ( where . is a current directory)
cp /home/singh/status.ksh . -- Destination is a directory ( where . is a current directory)

cp can also be used to copy multiple files with a single invocation of the command . In that case the last filename must be a directory .e.g to copy files test.txt , test1.txt , test2.txt you have to use cp like this -

cp test.txt test1.txt test2.txt logs

The command copies the above 3 files in the logs directory . If these files already present in destination directory and then it will be overwritten . The command will work if the logs directory exists , cp won't create it .

We have already seen * to frame a pattern for matching more than one file. In above example all the 3 files are starting with test so we can run cp command using * as -

cp test* logs copies all the files begining with test in logs directory

Like cat command we have some option in cp command as well -Lets see of of the most common and usful options that we need in our da to day task .

cp options -

cp -i - The -i (interactive options ) gives you warning before copying if destination file is already present .If you select option yes then file will be overwritten by new file.

cp -i test.txt test1.txt

cp -R copying Directories recursively -The command will recursively copy all the files and sub directories of source to destination - Note - if destination file alreday exist then unix will create the directory under it and copy all the contents under it . so in this case directory should not exist so that unix will create it and copies the content . If it exist then it will be the sub directory under that.

cp -R programs scripts
Sometimes , its not possible to copy a file, this can happen if it is read-protected or the destination directory is write protected . We will see file permissions in next chapter .

rm : Removing/deleting Files -

The rm command deletes one or more files at a time . To delete a file from a specific location , go to the location and - rm (file name) . Once the file gets deleted it can not be recovered.

rm test.txt test1.txt test2.txt

normally rm won't remove a directory , but it can remove file from it .Sometime we may need to remove all the files in a directory as a part of cleanup operation . The * used rm command removes all the files from the directory .

rm *
You are able to or not able to remove files from a directory is not depend on the files permission , but it depends upon the directory permission you have .

Like cp and cat command we do have different options in rm command as well . Lets see some most commonly used option with rm command .

rm option - 

Interactive deletion (i) - For all the above examples we haven't seen that command is prompting user before deleting file , so to give user second chance and to confirm before the removal , we can run rm command with -i option

rm -i test.txt test1.txt test2.txt

Recursive Deletion -r or -R - recursive option deletes all the files and subdirectories from present directory . One important thing - rm command does not deletes directory , but if uesd with -r or -R options directories will be deleted .

rm -r * removes all the files and directories ( behaves as rmdir command )

forcing removal ( -f ) -rm commands prompts for files with write protection , to override this protection we can use rm -f option which allows user to do forceful removal of such files .

rm -rf * Deletes everything from current directory and below

mv: Renaming Files

mv commands renames files. The command has 2 distinct features

  • It renames a file . ( or a direcctory)
  • It moves a group of files to a different directory

mv doesnot create any copy of the files - it simply replaces the filename with new filename . By default mv doesn't prompt for overwriting the destination file if exists .

mv status.ksh monitor.ksh

The above command renames status.ksh file with new name - monitor.ksh

We can move the group of files from one directory to other -The following command moves 3 files to logs directory

mv test1.txt test2.txt test.txt logs

mv command can be used to rename a directory e.g scripts directory can be renamed to my_progs as -

mv scripts my_progs

more -

To view the contents of files or to read a file we have used cat command . We have more command which display the file page by page on your screen unlike cat it displays all the contents on single stroke . e.g more test.txt - you will see the content of file one page at a time and at the bottom of the screen you will see filename and percentage of files that has been viewed.

more test.txt

As more command displays file page by page , it has few navigation keys which helps us to navigate through file . lets see some of the navigation keys

  • To move forward press f or spacebar
  • To move back one page use b
  • press q to quit more
  • To scroll forward 10 lines you can press 10f and if you want to move back press 10b
  • more command has one very useful feature repeaing the last command you can run same 10f or 10b by pressing . The . will automatically invoke last run command in more
  • To search a prticular pattern or word in a file use / followed by word e.g to search word - mangesh from file test.txt , run the following command
  • more test.txt
    /mangesh [press enter] - will search for the word provided in file
  • To find next matching word in file press n and to move back press b

less - The standard pager in Linux

Like more , we have one more pager command - less . Its name is less , but it is more powerful than more command . less is modeled on vi editor so learning less should be breeze for vi users .

less test.txt

We have the similar set of navigation keys as that of more command . Here is the summary of navigation keys for both the commands

Internal Commands for more and less -

more less Action
spacebar or f spacebar or f or z One page forward
10f - 10 pages forward
b b One page back
15b - 15 pages back
[Enter] j or [Enter] One line forward
- k One line back
- p or 1G Begining Of file
- G End of file
/key /key Searches forward for the word key
n n Repeats Search forward
- ?key Searches back for the word key
. (dot) - Repeats last command
v v Starts vi editor
q q Quit
h h View Help

file : Know Your File types -

In our previous discussion we have seen the three types of files , but we often need to know about the files which are present in our directory to know the dfferent types of files we can run -

file test.txt

To know the file types of all the files in current directory the run command as -

file *

wc : Counting Lines , Words and characters -

To know the number of lines , words and characters present in a file , we can run wc command

wc test.txt

The command will display the number of lines , words and characters present in the file

wc has 3 options

  • wc -l displays line number
  • wc -w dipslys number of words in a file
  • wc -c displays number of characters

when used with multiple files , wc produces a line for each file , as well as total as a count :

wc test.txt  test1.txt test2.txt

cmp : Compairing two Files -

We often need to run compare on the files to check whether the files are identical or not and if identical then we can safely delete any one file from a directory . Unix / Linux offers three different comparison command . In this section we will take a look at the cmp (compare command)

cmp  test1.txt test2.txt

The command runs comparison on files byte by byte and the location of the first mismatch is displayed on the screen . By default cmp does not bother about subsequent possible mismatches but displays a detailed list when used with (-l) option . If two files are identical then cmp does not display any message and simply returns the prompt .

comm: Common in Two Files-

Suppose you have two files having list of employees and you are asked to find out the names available in one and not in the other or even those common to both . comm is the common you need for this work . It requires two sorted files and lists the differing enteries in different colummns . let's try it on emp.csv emp1.csv

$ cat emp.csv                                 $ cat emp1.csv 
  mangesh pande                               nikhil muthal 
  sambit kumar                                amey deshapnde 
  deepak sharma                               mangesh pande
  gunjan verma                                Ashish rao 
                                              deepak sharma 

Both files are sorted and have some differences . When you run comm , it displays a three columnar output :

   $ comm emp.csv emp1.csv 
            Ashish rao  
            amey Deshpande 
                       Deepak Sharma 
   gunjan verma 
                       mangesh pande  
            nikhil muthal
   sambit kumar 

The column one represents the lines which are unique to file one . Column two represents the lines unique to file2 and the third column displays the lines which are common to both the files .

We can produce column based results suppose if you want to run comman command on column then we can use the options -1,-2 or -3.To drop particular column , simply use its column number as an option prefix . We can also combine options and display only those lines that are common :

   $ comm -3   emp.csv emp1.csv                 Selects lines not common to both files  
   $ comm -13  emp.csv emp1.csv                 Selects lines present only in second file  

diff : Converting One File to Other -

diff is the third command that can be used to display file differences . Unlike its fellow members, cmp and comm , it also tells you which lines in one file have to be changed to make the two files identical .When used with the same files , it produces a detailed output :

   $ diff emp.csv emp1.csv 
   0a1,2                      Append after line o of first file 
   > Nikhil Muthal            this line and 
   > Gunjan Verma             this line 
   2c4                        change line 2 of first file 
   < Makarand Bhalekar        Replacing this line 
   ---                        with 
   > Amey Deshpande           this line
   4d5                        Delete line 4 of first file 
   Sambit Kumar               containing this line 

diff uses certain special symbols and instructions to indicate the changes that are required to make two files identical . You should understand these instruction . each instruction uses an address combined with an action that is applied to the first file . The instruction 0a1,2 means appending two lines after line 0, whcih become lines 1 and 2 in the second file . 2c4 change line 2 which is line 4 in the second file .4d5 deletes line 4

Compressing and Archiving Files -

To conserve our disk space we often need to compress the large data . Unix / Linux system has some compressing and archiving commands which helps us to do this task- here are some file compression and de compression commands .

  • gzip and gunzip (.gz)
  • bzip2 and bunzip2 (.bz2)
  • zip and unzip (.zip)

The extension acquired by the compressed filename is shown in the parentheses. The degree of compression achieved by the commands depends on the filetype that we have compressed . Apart from compression we also need to group a set of files into a single file , called archive . The tar and zip commands can pack an entire directory structure into an archive. We can then send these single files via email if needed .

Lets start with some standard compression commands and then we will see the archiving of compressed files .

Compressing And Decompressing Files - gzip and gunzip

gzip is a very popoular compression command, that works with one or more filenames . It provides the extension.gz to the compressed filename and removes the original file.To compress a file log.txt run the command

gzip log.txt

- o/p will be file with log.txt.gz will be created and orignal file will be removed .

To see how much compression we have achieved on the files use the -l option with the compressed filename or filenames

gzip -l log.txt

Like all other commands , gzip also has different options lets see some of the most frequently used options -

gzip options -

Uncompress/Decompress a zipped file - To restore the orignal file and uncompressed a file , we have two options , we can either run gzip -d < file_name> or gunzip command with one or more file names .

gzip -d log.txt.gz
gunzip log.txt.gz

Recursive compress (-r) - Like cp we have (-r) option in gzip command which will recursivley compress all the files and sub-directories found in a directory .You need -r option and the argument to gzip should be a directory .

gzip -r scripts

It compresses all the files and sub-directories inside scripts directory .

The above option can be used for uncompression also. To decompress all the files in this directory you need to use

gunzip -r scripts or gzip -dr scripts
To view the compress files you don't need to unzip the files instead you can run zcat , gzmore or unzip -c

tar :Archiving the files -

tar is an acriving command which will combine all the filles into a single file . In above examples we have zipped our files , now to collect these files into a single file we need to archive them using tar command. Here are some key options that we can use with tar command .

  • -c Creates an archive
  • -k Extracts files from archive
  • -t dispalys files in archive)
  • -f Specify the archive arch

Creating an Archive -

To create an archive we need provide the name of the archive with (-f) and copy or write operation (-c) with filename as an argument . Additionally we will give -v option (verbos) to display the progress while tar works . The following command will archive three files into a single file archive.tar.

tar -cvf archive.tar mangesh.txt log.txt test.txt

We can gives directories to the tar command inorder to create a single archival of multiple directories .Scripts and mangesh directories are now archived in a single file temp.tar

tar -cvf temp.tar scripts mangesh

Now lets try to compress the archive file - to compress the archive file run - This will create file - archive.tar.gz which is nothing but archived and zipped file .

gzip archive.tar

Extracting files from archive (-x) -

To extract the files from archive file , tar command uses (-x) option . Now to extract all the files from the archive.tar file run the command -

tar -xvf archive.tar

To extract the files from archive which are compressed/zipped - we need to first unzip the file and then we need to untar it - here is the command for it - e.g to extract the files from archive.tar.gz ( which is a archived and zipped file) we need to first unzipped it and then we need to un tar it.

gunzip archive.tar.gz
tar -xvf archive.tar

Selective extraction of files can also be possible , We can even extract a single file which we want from archive using tar -xvf <file_name> only the file provided with command will be untar from archive .In below example only test.txt is extracted from the archive file .

tar -xvf archive.tar test.txt

Viewing the archive - (-t) -

To see the contents of the archive ( in a table format ) run the command tar command wih (-t) option .Run the below command and see the results.

tar -tvf archive.tar

In all this discussion , we have seen that if we have to create compressed file of the archived file then we need to run the commands tar and gzip respectively . Unix/Linux one more command (zip and unzip ) and option with tar . Lets see what are the options avaialble in tar and later we will see zip command

tar has -z option which do the compression and archiving in single stroke . This dual activity is summarised below
  • tar -cvzf archive.tar.gz mangesh test.txt log.txt test1.txt - ( compress and archived the files).
  • tar -xvzf archive.tar.gz ( uncompress an untar files ).
  • tar -xvzf archive.tar.gz test1.txt ( uncompress and untar single fiel)
  • tar -tvzf archive.tar.gz ( will display the list)

Zip and unzip command -

As of now we have seen two command for compressing and archiving - we can do the same activity in single zip or command without using 2 different commands i.e gzip and tar .

e.g if i want to zip and archive the same files which we have used in previous example , the command would be like this

zip archive.zip mangesh test.txt log.txt test1.txt

zip requires the first argument to be the compressed filename , the remaining arguments are interpreted as files and directories .

The command will compress and archive the files . The most imp feature of this command is if archive.zip exists then it will append the file without overwriting it .

To unzip a file run command -

unzip archive.zip

files will be restored . If the uncompressed filenames are already present then it will prompt you before it gets overwritten .

To see the archive - run command -

unzip -v archive.zip

Untitled Document Scroll To Top Untitled Document