creating and removing files

There are two commands to create files – touch and cat

  1. $touch filename: It creates zero byte file size.

Example: $touch k2schools

the size of the k2schools file is zero bites.

Touch doesnot allow to store anyting in a file. It is used for to create several empty files quickly.

Eg: $touch file1 file2 file3 file4 …

  1. Cat command:
  • $cat > filename

e.g $cat > k2schools




ctrl + d (to close a file)

  • $cat >> k2schools ––> to append data to the file.



ctrl + d

  • $cat file1 file2 file3 > file4

This would create file4 which contains content of file1 followed by file2 and followed by that of file3. It concatenates file1, file2 and file3 contents and redirects to file4. If file4 already contains simething it would be over written.

  • $cat < filename (or) $cat filename : to open a file

example: $cat k2schools

it displays k2schools file contents.

Removing files :

rm command:

to remove given file, $rm filename.

  • $rm k2schools : it removes k2schools file.
  • $rm -i k2schools : it asks confirmation before deleting the file.
  • $rm k2schools k2schools1 k2schools2 : it removes three files
  • $rm * : it removes all files from the current directory

Copy a file :

  • $cp sample1 sample2 : this will copy the content of sample1 into a sample2, if sample2 is already existed it overwrites.
  • $cp -i sample1 sample2 : if the sample2 already existed then it asks the confirmation.

Rename a file:

  • $mv old filename new filename : to rename the file

comparision of files:

  • $cmp file1 file2 : it compares file1 and file2, if both file contents are same number output, if files are differnt then it displays line number and character location.

Word count :

  • $wc file1 (10 20 80)

in the above output, 10 represents as total number of lines, 20 represents total number of words, 80 represents total number of characters

  • $wc -l file1 – it displays only line count
  • $wc -w file1 – it displays only wordcount
  • $wc -c file1 – it displays only character count
  • $wc -lw file1 – it displays line & word count
  • $wc -wc file1 – it displays only word and character count
  • $wc -lc file1 – it displays only line and character count
You can leave a response, or trackback from your own site.

Leave a Reply

Powered by k2schools
%d bloggers like this: