UNIX Hints & Hacks

ContentsIndex

Chapter 6: File Management

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

6.1 Copy Files with Permissions and Time Stamps

 

6.5 Finding Files with grep

 

6.8 Moving and Renaming Groups of Files

 

6.11 Splitting Files

6.2 Copy Files Remotely

 

6.6 Multiple grep

 

6.9 Stripping the Man Pages

 

6.12 Limit the Size of the Core

6.3 Which tmp Is a Good Temp?

 

6.7 Executing Commands Recursively with find

 

6.10 Clean Up DOS Files

 

6.13 uuencode and uudecode

6.4 Dealing with Symbolic Links

 

 

 

 

 

 

 

6.7 Executing Commands Recursively with find

6.7.1 Description

6.7.1 Description

The find command processes through a system and displays all the files, but find also executes a search through the files with grep to find patterns within files and within subdirectories.

Example One: Searching Recursively

Flavors: AT&T, BSD

Syntax:

find [dir] -type f -print | xargs grep [-il] [pattern]
find [dir] -type f -exec grep [-il] [pattern] {} \;

There are a couple of ways that you can grep through files recursively down subdirectories. This can really be a great benefit when a file contains a piece of code, a record, or information that you are unable to locate. The command uses find to search a directory tree and send the files through grep to search for a matching pattern in each file it locates. In using the find command in combination with grep, you can go through the system and search for every occurrence of the hostname that can exist in a file.

rocket 50% find / -type f -print | xargs grep -il planet
/etc/ethers
/etc/hosts
/etc/hostname.le0
/usr/local/bin/sysinfo
/var/adm/messages

This find pipes any file it comes across to xargs. The xargs command takes an argument and executes it. In this case, as a file is passed to xargs, a grep is run on the file for the word planet. If the word planet exists in one of the files, the file and its path is displayed. To shorten this recursive grep command even further, add it to your personal alias list:

alias rgrep    'find . -type f -print | xargs grep -i $1`

rocket 51% rgrep planet
/etc/ethers
/etc/hosts
/etc/hostname.le0
/usr/local/bin/sysinfo
/var/adm/messages

By typing rgrep and a search pattern, the pattern is searched in all the files and subdirectories from the current directory you execute the command from.

rocket 51% find / -type f -exec grep -il planet {} \;
/etc/ethers
/etc/hosts
/etc/hostname.le0
/usr/local/bin/sysinfo
/var/adm/messages

To show multiple ways to perform the same task, this command also processes through a tree or the system and searches for the word planet in every file it comes across. In this version, when every file is found, a grep for the word planet is executed on the file. If the word is found, the filename and path are displayed.

An alias can be set up for this version of the recursive grep as well. This too allows for the command to be entered faster by the administrator and get the results more quickly.

alias grep   'find / -type f -exec grep -il $1 {} \;'

Example Two: Removal of Old Files

Flavors: AT&T, BSD

Syntax:

find [dir] -type f -atime +n -exec rm {} \;

To help in cleaning up files, this command can be used to clean up old and unnecessary files. When the find command reaches a file that has not been accessed in n days, the file is removed. If disk space is tight on a disk and you have to find directories to clean up, you typically target the temporary areas first. Users can still occasionally place files vital to their work in this area. So it is best to remove older files first. If a user hasn't touched a file in n days, it is deemed removable.

# find /disk2/tmp -type f -atime +10 -exec rm {} \;

This find command progresses through the temporary space on /disk2/tmp and looks for files that have not been accessed in 10 days. If one is found, it then removes it. You do not see anything being output on the display if it finds it. All you should notice is an increase in available disk space. If a large amount of files are still filling up the area, you might need to cut back to a seven-day removal.

You might want to notify your users by sending out periodic notices that files are removed every seven days in the temporary areas. Then you can set this up in the crontab to remove all files in temporary areas every seven days.

0 4 * * * find /tmp -type f -atime +7 -exec rm {} \;
2 4 * * * find /var/tmp -type f -atime +7 -exec rm {} \;
5 4 * * * find /diske2/tmp -type f -atime +7 -exec rm {} \;

Reason

Deleting specific files or searching through files can be tedious, especially if it requires searching through multiple directories. It be time consuming if done manually. Having an understanding of how the find command is used can help speed things up for an administrator.

Real World Experience

When setting anything into the crontab that potentially removes large numbers of files, make sure that the entire path is defined for the files that are to be removed. If not, there's a chance of removing all the files on your system.

In the Eastern United States, a group of programmers and a system administrator spent three days setting up four servers from the West Coast to go into production. The programmers worked feverishly through each night on one machine to get everything in the code right. The plan was to duplicate the system drive off the one server and propagate it to the other servers. By 10:00 the night before the scheduled production date, all was ready to go to production to hundreds of people in the morning. The bugs were thought to be worked out of the programs and the duplicating of the disks to the other three servers began. At 11:30 p.m. the process was all finished.

The programmers spent the next half-hour verifying that all was well for the morning. Everyone was excited that they would be out by midnight. They all started heading for the door when one of the programmers noticed something strange and asked whether anyone knew what was happening. Two of the server disks showed constant activity at exactly the same time and then the two servers crashed. The other two servers were fine. The two that crashed would not even boot to single-user. The third server was halted immediately and stayed down until they could figure out what happened. The fourth server was left up and running to see if they could use it to troubleshoot the problem.

The system administrator began to boot the first server into miniroot from the CDs and, to his surprise, most of the operating system was gone. Because both machines acted exactly the same at midnight he checked the clocks on all the servers. He found that the two that crashed were set to the Eastern timezone and the other two still had the Pacific timezone set. They now believed that they had about two-and-a-half hours left to find the cause before the other two servers crashed.

It appeared that the crash that took place was a timed event. On the fourth server, the system administrator went straight to the crontab and searched for any jobs that started at midnight. There it was. A programmer had an incorrect entry in the crontab:

0 0 * * * find / -type f -exec rm {} \;

The programmers were writing a lot of files out to the system's temporary directory. They always wanted the files to be deleted whenever the server might have to be rebooted or cleaned up on a nightly basis. The only problem was that they had typed the crontab entry incorrectly. The entry should have read

0 0 * * * find /tmp -type f -exec rm {} \;

By 1:30 a.m. they had everything the way it should be and ready for production in the morning. All went fairly well in the morning. The moral? Always be careful when automating file deletions.

Other Resources

Man pages:

crontab, find, grep, rm

UNIX Hints & Hacks

ContentsIndex

Chapter 6: File Management

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

6.1 Copy Files with Permissions and Time Stamps

 

6.5 Finding Files with grep

 

6.8 Moving and Renaming Groups of Files

 

6.11 Splitting Files

6.2 Copy Files Remotely

 

6.6 Multiple grep

 

6.9 Stripping the Man Pages

 

6.12 Limit the Size of the Core

6.3 Which tmp Is a Good Temp?

 

6.7 Executing Commands Recursively with find

 

6.10 Clean Up DOS Files

 

6.13 uuencode and uudecode

6.4 Dealing with Symbolic Links

 

 

 

 

 

 

 

© Copyright Macmillan USA. All rights reserved.