Few tricks for cat, diff and tail commands

Default featured post

Each time that I work with Linux command line console I learn new things but the problem is I learn different things which are not related to each other and I know if I wait to gather and categorize them, I will forget some of them and there is possibility also to become lazy in categorizing commands and write something comprehensive.

Therefore, what I learn, I share whether they are related to each other or not. This post is about three command line tricks which could be useful for users especially beginners.

First trick: Use cat command to make text file

Usually the usage of cat command is to show and concatenate files but you also use it as dumbest text editor to create text file with it alongside using pipline. For creating text file with cat look at following example,

$ cat  > MyTextFile

After running the above command, the cursor goes to a newline and there you should write what you want to save in your file. Just keep in your mind, if you press enter for a newline you will not be able to go back to the previous line to edit that and that is why it is called dumbest editor. Now, after finishing what you wrote just press Ctrl+D and the file will be saved.

Second trick: Compare two text file with diff command

Diff command is used to compare to sorted text file to find that whether they are equal or not. The key point here is that your files should be sorted but do not worry you could sort them easily if they are not sorted with the sort command as explained before in this post. The following example shows usage of diff.

$ diff T1.txt T2.txt

If both files are identical, the command does not return anything, but if they are not identical, it returns those lines that are different in files.

Third trick: Monitor log files lively with tail command

Basically, tail command is used to show last N lines of the file but you could add another parameter to keep file open and shows all new data that added to the end of the file. It is very handy tool for system administrators who want to track changes in log files online without refreshing files or reopen them. For using tail command to monitor added new data to the end of file see following example,

$ tail -f MyFileToTrack

For more information please visit following links,