Manipulating Files

Several commands are available that allow you to learn about and search for and through different files.

1. wc
The wc command is used to count the lines, words, and character in any text file.

# wc filename
 1914  9298  76066

These numbers corresponds to the number of lines, words and character in the text file.

      COMMAND       RESULT
 wc -l filename Number of lines in the file
 wc -w filename Number of words in the file
wc -c filename Number of characters in the file


2.find
The find command is used to find the file through directories and subdirectories. For eg; if you want the file named fig06.tif, you use the following command:

# find / -name fig06.tif

This command searches in the root directory and all the subdirectories for the fig06.tif file.

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

3. locate and slocate
An alternative to the find command is the locate command. This command searches through the database of files. In Red Hat Linux, the locate command is actually soft-linked to more secure slocate command. The database is update per the /etc/cron.daily/slocate.cron script.
take a look at the default command at that script:-

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


as we can see from the updatedb man page, the -f switch excludes a number of filesystem types, and -e switch excludes a number of directories that should be accessible to the root user.

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

# locate fig0

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


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

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

The response tells that there is a user named michael jang. It also includes the home directory and default shell for that user.
you can even use grep to search through a series of files with commands like:

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

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

Related Posts:-

Comments

0 Responses to "Manipulating Files"

Post a Comment

 

Managing Shell And its Secrets

Subscribe To Get Update On Mobile

Subscribe
Get updates of latest post on your mobile free