Tuesday, September 20, 2011

UNIX Command: Looking into files.

As it is important to know how to navigate in UNIX to find files it is important to know how to navigate within files. There are obviously few commands in UNIX that helps you to navigate within the files or view the required contend of your files without much difficulty.

Below are the few commands which are helpful to navigate through files in UNIX.

file - to identify file types
head- peak at the first few lines
tail - view last few lines
cat - view content of file
more - view larger files.


file:
A program that can easily offer you a good hint as to the contents of a file by looking at the first few lines. The disadvantage of this command is, it is not 100% accurate
eg:- A text file has executable permission and the initial contents of the files look like a C program the file command interpret it as an executable program rather that an English text file.

% file newtext.txt helloworld.txt 'shiyas resume.doc'
newtext.txt: ascii text
....
...
photos: directory

% file *
will analyze all files in home directory
 
head: Use it to view up to the first few hundred lines of a very long file, actually. You can specify the number as well.
% head newfile.txt
first few lines are displayed

% head -4 newfile.txt
first 4 lines are displayed

Can supply multiple files as well
% head -4 newfile.txt passwd
=====>passwd<====
......................................

=====>newfile.txt<===
.......................................


Can be used along with pipes (|):
% who | head -5
the output of who is supplied to head -5 and lists down first 5 users in the list.


tail:
Provide last lines of the file.
%

cat:
flags: -v -s
head -12 .cshrc | tail -3
combine the two, head and tail, so you can see just the tenth, eleventh, and twelfth lines of a file?

No comments:

Post a Comment