Difference between cat and touch command.

It has been always a confusion for the Unix beginners , difference between cat and touch command and where and how to use the commands, so in this tutorial i will write few basic understanding between those commands and use of it.

cat: cat is one of most used and well known command in UNIX system and mainly used to view files. It can also be used to create, display, concatenate and append to files. cat is a versatile command.

Syntax:

$ cat > filename.txt  (for crating new file)
$ cat filename.txt    (to view file)

cat Options: I have mentioned the mostly used options.

# -v — show-nonprinting characters , use ^ and M- notation, except for LFD and TAB

# -n — number all output lines

touch: Update the access and modification times of each FILE to the current time.
Syntax:

$ touch filename
$ touch file{a,b,c}  (for creating multiple files)

touch Options:

# -m — change only the modification time

# -a — change only the access time

Few Differences between cat and touch command

As both the commands are used for file creation still they have few differences which i have discussed below.

cat:

  • If already a file exits with the name file1 cat will not create a duplicate file with the same name under that directory.
  • If there is no file exits with the name file1 it will create new file and allows you to insert any content.
  • cat command allows you to create a single file at a time, so you can not create multiple files with a single command as you can do in touch command.

touch:

  • As you know touch command is used to update the time stamp, and if the file exists it will update the time stamp.
  • If the file doesn’t exits it will create a new file with size of 0 KB.
  • touch command can not be used to insert any data to a file.
  • You can use touch command to create multiple files at a time.

Leave a Reply

Your email address will not be published. Required fields are marked *