Search

Learn Linux-7 More Commands

Aim: Introduce the reader to a few more commands of linux.

The commands that we will look into in this chapter are

cat
head
tail
cp
mv

To work with the above commands we would need to create a few files, and put some content into it. So go into one of the folders that we have created in the previous chapter and create two files, "file1" and "file2" with any content that you wish to put in them.

Note: Any thing put between [ ] while mentioning the syntax means, its not a mandatory field, and is meant to specify the options.
cat:
Cat basically stands for concatenate, meaning what ever files are passed to the command all of them will be printed on the output screen at once.
The major use of the command is to view the contents of the file with out opening them.

syntax: cat [options]  "filename"

For eg:

user@desktop:~/folder1$ cat file1       
creating the first file in vi editor           
user@desktop:~/folder1$   
                 

What ever you would have put into the file "file1" would be output on to the terminal.
You can also pass multiple files at the same time to the "cat" command, the contents of each file will be displayed one after the other.

For eg:

user@desktop:~/folder1$ cat file1 file2
creating the first file in vi editor          
this is second  file                                
user@desktop:~/folder1$
                    


"cat" comes with a few options, which you can see using the "man" command. One of the useful options is "-n" . This prefixes a line numbers to every line of the output.
Open the "file1" that you have created and enter a few lines of text.
Now run the "cat" on it as shown below.

user@desktop:~/folder$cat -n file1                                    
     1 creating the first file in vi editor                                  
     2 VI is a widely used editor.                                             
     3 There are other editors that can also be used.            
user@desktop:~/folder1$      
                                               

As you can see in the output, every line has been prefixed with the line number. 


Try it out:


1. What happens if you use the -n options giving more than one file as input to cat, does each file's output start with "1"
2. What happens when you run "cat" with out giving any input file.




Head:

Assume your file has 1000 lines, and you want to view the top 5 lines of the file. If you used the "cat" command, it will print out all the 1000 lines on to the screen and you will have to scroll up all the way to have a look at the top 5 lines.
This is surely not the optimum way to work.
So Linux provides you a work around, the command head.

syntax: head  "filename"
 

The number of lines that you need is passed as an option  i.e.

head -n filename

Where the "n" is the number of lines to be displayed.

From the previous run of "cat"  we know that file 1 has 3 lines. Let's use head to view only the first line

Foreg:

user@desktop:~/folder1$ head -1 file1
creating the first file in vi editor         
user@desktop:~/folder1$
                    

The other options available with "head"  are not as useful as the -n, you can try them out when you feel you are comfortable

Tail:

We just saw how to view the top lines of a file, how about when you want to view only the bottom few lines of a file.

syntax : tail "filename"

Just like "head" tail also takes number of lines to be displayed as a option.
i.e. tail -n "filename" where "n" is the number of lines you want to view.

For Eg:

user@desktop:~/folder1$
 tail -1 file1               
There are other editors that can also be used.
user@desktop:~/folder1



cp: 

"cp" is a command used for copying, files or folders.

syntax:  cp [options] "source"  "destination"

source: The file or the folder name, or path to the file or folder which you want to copy
Destination:
The file or the folder name, or path to the file or folder into which you  want to copy the file

Lets us use the cp command to create a copy of file1 by the name of file1_copy.

user@desktop:~/folder1$ cp file1 file1_copy
user@desktop:~/folder1$ 
                           




Run the command "ls" after running the above "cp" command. 

user@desktop:~/folder1$ ls              
file1 file2 file1_copy                           
user@desktop:~/folder1$   
              


The new file that we just created using the "cp" command is also visible.

some useful options that are available with cp are

 -i : Interactive, i.e. it will prompt a confirmation question before copying in case a file name by the same name exits in your current directory.

For eg:

If we try to make a copy of file2 by the same name file1_copy,  and we don't use the -i option. Then the file3 will be over written, and we would lose the previous copy of file3.
In case you want to make sure that the system checks before it does the over write use the option -i.

user@desktop:~/folder1$ cp -i  file1 file1_copy
cp: overwrite `file1_copy'?



If you type "y" when the above prompt comes the the "file1_copy" will be over written, if you press "n" it will not be over written.

-f:  Force
In some systems the "cp" command is such that it prompt for confirmation every time you try to copy, in such systems if you do not want to be prompted then use the "-f"  option, this will force the copying with you asking you the question.

-r: Recursive

Suppose you want to copy a whole directory instead of a file. In our example suppose we want to create a copy of folder1 and call it folder1_copy.
If you try using the copy command directly like we used it for files you would get the following error

user@desktop:~/folder1$ cp folder1 folder2
cp: omitting diretory `folder1'                    
user@desktop:~/folder1$   
                         


This is because by default "cp" is meant only to copy files and not folders. In case you want to copy folders use the option "-r".

This means recursively copy all the files in the folder to the new folder mentioned.

user@desktop:~/folder1$ cp -r folder1 folder2

You can combine any of the options together and use it. For eg you can interactively copy a folder by using the options "i" and "r" together.

Try it out:
1. What do you think will be the output of the following command
"cp file1 file2 folder1" ?
Where file1 and file2 are simple files and folder1 is a directory.

No comments:

Post a Comment