Manipulating Files

Several commands are available that allow you to learn about and search for and through different files. The wc command allows you to get a count of the number of lines, words, and characters in a file. The find and locate commands let you search for specific files. The grep command enables you to search through a file for a text string without opening it. The slocate and egrep commands are variations on these commands.

wc

The wc command is fairly straightforward. With any text file, you have a certain number of lines, words, and characters. Using the wc command, you can find all three characteristics. For example, you can check the showoff text file as follows :

 #  wc showoff  1914   9298   76066 

These numbers correspond to the number of lines, words, and characters in this file, respectively. You can get any individual figure based on the commands shown in Table 6.10.

Table 6.10: Examples of the wc Command

Command

Result

wc -l showoff

Number of lines in the file showoff

wc -w showoff

Number of words in the file showoff

wc -c showoff

Number of characters in the file showoff

find

The find command looks through directories and subdirectories for the file(s) of your choice. For example, if you want to find a file named fig0606.tif , you use the following command:

 # find / -name fig0606.tif 

This command searches in the root directory and all subdirectories for the fig0606.tif file. The search can take quite some time. If you have more information, you may want to substitute a lower-level directory for the root ( / ).

With the find command, you can also use wildcards, such as the asterisk ( * ) and question mark ( ? ), in your search term .

locate and slocate

An alternative to find is the locate command. This command searches through a database of your files. By default, if you keep Linux running on your computer, the database associated with the locate command is refreshed every day at 4:02 a.m. If you re searching for a file that wasn t created since the last database update, the locate command finds files much more quickly.

In Red Hat Linux, the locate command is actually soft-linked to the more secure slocate command. The database is updated per the /etc/cron.daily/slocate.cron script. Take a look at the default command in that script:

 /usr/bin/updatedb -f "nfs,smbfs,ncpfs,proc,devpts" \ ?-e "/tmp,/var/tmp,/usr/tmp,/aft,/net" 

As you can see from the updatedb man page, the -f switch excludes a number of filesystem types, and the -e switch excludes a number of directories that should be accessible only to the root user . You can customize this script to exclude other directories, such as /root , or filesystem types, such as vfat .

Once you have a locate database, it is more flexible; for example, if you use the following command, it returns all files that include the text string fig0 :

 # locate fig0 

The locate command works as if asterisks are assumed before and after the search term.

grep

The grep command is a handy way to search through a file. As a system administrator, you may have long lists of users. If you want to search through your /etc/passwd file for a user named michael jang , try the following command:

 #  grep "michael jang" /etc/passwd  mj:x:500:500:michael jang:/home/mj:/bin/bash 

This response tells you that there is a user named michael jang . It also includes the home directory and default shell for that user. If the search string exists in more than one line, you ll see those lines as well. You can even use grep to search through a series of files with commands like the following:

 # grep mj * # grep -c bash /etc/passwd 

The first command looks for the string mj in all files in the current directory. The second command, with the -c switch, counts the number of lines that include the word bash .

Command Combinations

It s a common practice to use more than one Linux command in a line. For example, if you re using the find command and you know that the result will have a large number of files, you can use a command like grep to search through the result. Specifically, let s say you want to find some of the .html files on your system. You might start with the following command:

 # find / -name *.html 

However, you might get discouraged when you see hundreds of files flashing past you on your terminal screen. An alternative is to combine commands like this:

 # find / -name *.html  grep bookmark 

This command searches through the results of the find command for the text string " bookmark ". Only those files with both strings are output to the screen. Other possible command combinations include the following:

 # who  grep mj # ps aux  grep mozilla 

The first command, who , lists all users currently logged onto your Linux system. When you pipe ( ) the result to the grep mj command, you ll find the number of times that user mj is currently logged onto your system.

The second command, ps , lists the processes currently running on your Linux system. The three switches, aux (a dash is not required for ps command switches), leads to a very long list of processes, because it includes all processes run by all users ( a ), each associated with the username ( u ), independent of the virtual terminal ( x ). You need a tool like grep to search through these processes. This combined command returns all processes with the word mozilla .

 


Mastering Red Hat Linux 9
Building Tablet PC Applications (Pro-Developer)
ISBN: 078214179X
EAN: 2147483647
Year: 2005
Pages: 220

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