Viewing files on the command line 

While a text editor is very handy for viewing and editing files there are times when you only want to view a file or part of a file. But it should be noted that these commands only work with plain text files, that doesn’t mean .txt files. A plain text file is ant file that doesn’t include any formatting so files produced by office applications can not be read with these commands, but you can read configuration files .xml files if you are a programmer the files your source code will be in plain text. As you can see while you can’t view all files you can view many of them, this will be most helpful for system administrators.

The cat command will display the contents of a file or multiple files on the command line 

cat <file>

or multiple files

cat <file1> <file2>

While the you can view the file it is a little messy reading it all on the command line and depending on your terminal emulator you may have found that you can’t view the entire file, this also depends on the size of the file you are viewing.

To overcome this you could use the less command this allows you to move around the file easier than you can using the cat command.

less <file>

you can also pipe the output from other commands into less making the output easier to move through especially if especially if the command outputs a lot of information. For the following example I will use the ps command which lists all the processes running on the computer.

ps | less

The following two commands will allow you to view part of a file more specifically the first few lines and the last few lines. while this would not be of a lot of benefit to the average user it could be very useful for system administrators especially the tail command.

The head command allows you to view the first few lines this can be set by you but there is a default value.

head <file>

if you want more or less lines it can be set using the -n argument 

head -n <number of lines> <file>

The tail command is the same as the head but instead of outputting the first few lines it outputs the last and again there is a default value for the number of lines that will be output if you don’t set one.

tail <file>

if you want more or less lines it can be set using the -n argument 

tail -n <number of lines> <file>

As with earlier posts all I have given you is the beginning of what is capable with these commands, but finding out more is not very hard with the man command.

man <command>


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s