Twitter

Using Linux Find Command Effectively Could Make Linux Administration Easier

Sabtu, 31 Oktober 2009 , Posted by Dion at 01.18

http://androes.files.wordpress.com/2008/08/tux-droid-linux-companion.jpg


When comes to file searching in a Linux file system, the locate command is more simple and easier to use than the find command. But, if you want to find some files with matching criteria and automatically apply actions to them, the find command will works perfectly.

As with most Linux command line utilities, the find command has a lot of option switches to fine tune its file searching abilities. Here are some find command usage by examples:

To search all the configuration files that contain IP address 172.20.11.25, by assuming there are no configuration files larger than 200KB (some files such as lynx.conf that contains over 3000 lines can be as big as 130KB).

find / -type f -size -200k -exec grep -HIils "172\.20\.11\.25" {} \;

The -exec option switch instructs find command to pass each of the matching files (-type f indicates regular file, and -size -200k indicates file size that is less than 200KB) to the {} file holder, which will be used by the grep command.

The grep command option switch

-H indicates grep will only print out the file name that matches the criteria,
-I will ignore binary files in the searching to save time,
-i ignore case-sensitive pattern searching,
-l to stop searching further within the file when the first pattern has matched,
-s to suppress errors message such as non-existent or non-readable.
To search all zero-byte files and move them to /tmp/zerobyte folder

find / -type f -size 0 -exec mv {} /tmp/zerobyte \;

You should have noticed that the position of {} file holder has changed, and it should make sense if you recall the mv command syntax. Alternative, the -empty option switch will works more efficiently, as it default to find all empty files and directory.

find /home/jeff -empty -maxdepth 1 -exec mv {} /tmp/zerobyte \;

The -maxdepth 1 option switch instructs the find command to only search at /home/jeff directory, without going further to /home/jeff/appslog, etc.

If quota mechanism is not configured for Samba file server, you might be curious to know what files have been stored and fill up 15G of free disk space! Run this find command

find /smbsp1 -size +50000k -exec -ls -lactr {} \;

to locate all the files, which are greater than 50MB, in the /smbsp1 file system. You may have to run this find command for few times, to filter with different size. If you guess that your users might be storing MP3 music files rather than RMVB movie files, it makes sense to start with -size +3000k for better search results. In addition, you could consider to fine tuning the file searching algorithm with the -name "*.mp3" or -name "*.rmvb" option switch, which will only look for files ended with MP3 or RMVB file extension.

Currently have 0 komentar:

Leave a Reply

Posting Komentar