2.9 Miscellaneous File Handling Commands

   

Here are some other useful commands related to file handling.

The Head and the Tail

Sometimes you need to view only the first or last few lines of a text file. By default, the head command lists the first ten lines of a text file, and the tail command lists the last ten lines of a file. For example, if you want to see the first ten lines of the /etc/passwd file (used to store user names and passwords), the command and its output will be:

 $  head /etc/passwd  root:8JgNSmFv806dA:0:3:,,,:/home/root:/sbin/sh mmsecad:ETxUQ5wSQZCAk:0:3::/:/sbin/sh daemon:*:1:5::/:/sbin/sh bin:*:2:2::/usr/bin:/sbin/sh sys:*:3:3::/: adm:*:4:4::/var/adm:/sbin/sh uucp:*:5:3::/var/spool/uucppublic:/usr/lbin/uucp/uucico lp:*:9:7::/var/spool/lp:/sbin/sh nuucp:*:11:11::/var/spool/uucppublic:/usr/lbin/uucp/uucico hpdb:*:27:1:ALLBASE:/:/sbin/sh $ 

Additional parameters can be used with both the head and tail commands to view any number of lines of text. A tail -n 3 /etc/passwd will show the last three lines of the file. If you want to see what is being added to a text file by a process in real time, you can use the tail -f command. This is a very useful tool to see text being added to a log file.

Counting Characters , Words, and Lines in a Text File

Many times, you want to know how many characters, words, or lines there are in a file. In the /etc/passwd file, for example, there is one line for every user. You can count the number of users on the HP-UX system if you count the number of lines in the file. We use the wc ( word count ) command for this purpose. It displays the number of lines, words, and characters, respectively.

 $  wc /etc/profile  171 470 3280 /etc/profile $ 

It shows that there are 171 lines, 470 words, and 3280 characters in the /etc/profile file. If you want to count only the number of lines in a file, you can use wc -l . Similarly, for counting words, wc -w , and for counting characters, wc -c , can be used.

 $  wc -l /etc/passwd  2414 /etc/passwd $ 

It shows that there are 2414 lines in /etc/passwd , which is an indirect way to find out the number of users on this system.

Link Files

Many times you need to refer to the same file that has different names. You can create a link file that is not the actual file but points to some other file to which it is linked. There are two types of links, hard and soft. Soft links may be established across file systems. The soft link is a special type of file; the first character of the ll command list is " l " for link files. To create a link, the ln command is used. For example, to create a hard link, abc , to a file, myfile , we use:

 $  ln myfile abc  $ 

To create a soft link, we use the -s option.

 $  ln -s myfile abc  $ 

   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net