Skip to main content

File Commands

Redirecting output and piping

Before we see file commands, let us see two operations which are quite useful. These are redirecting output (and input) and piping the output.

We know that ls command lists the files in current directory. Can we save this listing to a file?

Yes. That can be done using > - redirection operator.

ls>a.txt

redirects the output of ls command to a file called a.txt. Remember that is exactly how we created our file earlier - by redirecting output of cat command to a file.

So what we see in screen shot is - output of echo command is redirected to cc.txt. And of course cc.txt has complete path information of our system.

Next we pipe the output of echo $PATH  command to cat, so that it gets displayed.

Piping is done using "|". It sends the output of a command as input to another command. So output of echo command is sent as input to cat.

In the next line we are piping output of echo command to wc command. wc is word count command.

wc gives number of lines, number of words and number of characters of input. So we see output as 1 line (no line break in middle), 1 word and 233 characters.

Cat command


Next let us see some file commands - cat, cp, mv, rm and wc.

Cat command was already covered in previous post. It just displays the content of a file.

cat
cat without any parameters will take input from stdin (standard input device - normally keyboard) and show it on stdout(standard output device - display)

cat a b
displays contents of file a followed by contents of file b.
 We wrote two lines to a.txt by saying cat and redirecting output to a.txt. Then we wrote one line to b.txt by using cat>b.txt. Now by giving command cat a.txt b.txt we see the concatenated output of these two files.

We see in the next line, cat without any file name. It takes input from keyboard and displays it on screen. 

Relative and absolute filenames 

A relative file name will be the current directory + the given path name. 
e.g. if we are in our home directory and we give command

cp a.txt b.txt
it is taken as
cp /home/usha/a.txt  /home/usha/b.txt


We can also give commands such as 
cp /home/usha/a.txt /home/usha/temp/b.txt

Here  the filenames are given as absolute.

Special characters in paths 
  1.  dot (.) stands for current directory
  2.  double dot (..) stands for parent directory of current.
  3.   tilde (~) stands for home directory  
Now let us see some more commands. Note that prompt shows us where we are.


We created a child directory of temp called temp1 (how original). Then we moved this directory and gave ls command.

As there are no files in this, there was no output.

Then we gave ls .. command which says list the files in the parent of current directory. So we saw listing of temp directory.

Then we copied a file one.txt from parent directory to three.txt (two.txt is already present :) ).

cp command and mv command

cp (copy) command copies the content of one file to another. Both the file names can be absolute or relative.

We have already seen cp command in the screen shot above. 

Can we copy multiple files?


In first command we copied list1.cpp from cppPrograms subdirectory of parent directory (.. is for parent ). We copied it to current directory (dot - .). Under the same name.

Next we copied all files whose names start with c and which have extension of cpp from /home/usha/cppPrograms directory to current directory.
  • * in filename is called a wildcard and it stands for 0 or more unknown characters.
  • ? in filename stands for one unknown character
All the lines in the middle are my efforts in using tab key to get filenames. Tab key auto completes commands and filenames. Try it.

 cp ~/b1.sh b2.sh
command copies the file b1.sh from my home directory to current directory. But this time, the name of file copied is b2.sh. 

This is unlike all previous commands where we did not give destination filename - so it was same as source filename. 

Remember that in cp command, the original file is intact and we have two copies of same file.  

mv command

mv command - move command is used to move a file from one location to another. It can also be used for renaming a file. 

mv b2.sh first.sh

Renames the file b2.sh to first.sh

Unlike cp command, mv command does not create an extra file.

 mv ~/a.sh a.sh

This command moves the file from my home directory to current directory.

You can know current directory by giving pwd command. But in most cases prompt tells you current directory.


rm command

rm command is used for removing a file/s. You must have write permission to remove a file.



rm with wild card * can be used to delete all files in current directory.

But this command will not delete any child directories or files in them. But by adding option "-R" (recursive) you can delete subdirectory structures too.

Be very cautious with this option.

By the way, the command to delete a directory (only if it is empty ) is rmdir

Type of file - file command

If you want to know if a given file is just a text file or some type of binary file, you can use file command. file gives you type of the file. 

So we see that it tells us whether the given file is plain text file, or a source file of a language or an executable file or even a database. 

Quite helpful! Don't you agree?

In the next post, we will discuss about links and little bit more about passwords.

Comments