Download your favorite Linux distribution at LQ ISO.
Go Back > Blogs > loadedmind
User Name


Rate this Entry

Fun with the find command

Posted 01-05-2012 at 10:37 AM by loadedmind
Updated 05-31-2017 at 07:23 AM by loadedmind

Over the years, I've found some pretty useful find recipes to get the job done in a quick hurry. I thought I'd provide examples of what I needed done and how find helped me do that. I'm sure a quick google search will get you similar findings and more, but perhaps my particular examples will assist should you find yourself in a similar predicament.

**Disclaimer** Before I provide some examples, it's worth mentioning that you should always test this within an environment you don't care what happens to so it won't matter if you happen to destroy files or, at the very worst, render your system unusable. Consider yourself warned. **Disclaimer**

Most of you probably understand the find command enough to prevent me from explaining its usage, but here's a primer:

find / -type d -iname "*settings*"

This tells your system to search from the root directory up find / (in other words, the whole system) ; you're only interested in directories -type d ; you want a case-insensitive search -iname ; and you want find to match only those directories that contain the word settings in their name "*settings*"

Now let's build on that. You've found the settings directory, let's say it was in /var/www/html/drupal/settings.

Regarding web development, suppose you need to make modifications to all php files in the settings directory, but you want to make sure you have a backup before you start editing them.

First, we'll make sure we're in that directory:

cd /var/www/html/drupal/settings



Now issue the find command that will create a backup of all php files with the suffix .bak

find . -iname "*.php" -exec cp -v {} {}.bak \;

Verify your actions:

ls -la

Now let's say you just want to move all of those backed up files out of that directory and into a directory one level up called backup_files:

find . -iname "*.bak" -exec mv -i {} ../backup_files \;

Verify files in current directory and that the backup files have been moved out of it:

ls -la

Now verify the backup files exist in the backup_files directory:

ls ../backup_files

As you may have guessed, the uses for -exec are practically infinite.

Now on to a different find scenario. The following command should work on other *nix-based systems, but for this example, I was on a Mac which runs a derivative of Darwin BSD. I had a directory on the Mac which was called scripts, but I couldn't recall where I put it. For those of you familiar with Macs, you'll know that, at the top level of the filesystem, you have the following directories:
Developer (if you installed it, of course)
opt (for those of you that use fink and/or Mac Ports)

I knew find could potentially match files in those directories and I really didn't want find to waste its time traversing them because I was confident my scripts directory wasn't in any of those directories. This find command will skip looking through those directories for the scripts directory:

sudo find / -type d \( ! -name Developer \) -o \( ! -name Applications \) -o \( ! -name opt \) -o \( ! -name Library \) -name scripts -print

To break it down, find will start from the root filesystem / searching for a directory -type d but will not traverse Developer directory and those that follow using the -o switch to add others it shouldn't look in for each instance.

You may want to add directories to this command that may contain data from an external source, such as an NFS-mounted directory, a flash drive, external hard drive, etc. Simply substitute or add using the above example.

Hope this was as beneficial to you as it was to me. For the longer commands, you can, of course, create aliases if you should need to run the same kind of find command in the future.

Perhaps I'll write another blog regarding the use of aliases in the near future.
Views 859 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 02:57 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration