Americas

  • United States
sandra_henrystocker
Unix Dweeb

Using the Linux find command with caution

How-To
Oct 16, 20173 mins
LinuxUbuntu

To make sure files aren't removed accidentally when running the Linux find command, use the -ok command. It will ask for permission before removing any files.

A friend recently reminded me of a useful option that can add a little caution to the commands that I run with the Linux find command. It’s called -ok and it works like the -exec option except for one important difference — it makes the find command ask for permission before taking the specified action.

Here’s an example. If you were looking for files that you intended to remove from the system using find, you might run a command like this:

$ find . -name runme -exec rm {} ;

Anywhere within the current directory and its subdirectories, any files named “runme” would be summarily removed — provided, of course, you have permission to remove them. Use the -ok command instead, and you’ll see something like this. The find command will ask for approval before removing the files. Answering y for “yes” would allow the find command to go ahead and remove the files one by one.

$ find . -name runme -ok rm {} ;
 ?

The -exedir command is also an option

Another option that can be used to modify the behavior of the find command and potentially make it more controllable is the -execdir command. Where -exec runs whatever command is specified, -execdir runs the specified command from the directory in which the located file resides rather than from the directory in which the find command is run. Here’s an example of how it works:

$ pwd
/home/shs
$ find . -name runme -execdir pwd ;
/home/shs/bin
$ find . -name runme -execdir ls ;
ls  rm  runme

So far, so good. One important thing to keep in mind, however, is that the -execdir option will also run commands from the directories in which the located files reside. If you run the command shown below and the directory contains a file named “ls”, it will run that file and it will run it even if the file does not have execute permissions set. Using -exec or -execdir is similar to running a command by sourcing it.

$ find . -name runme -execdir ls ;
Running the /home/shs/bin/ls file
$ find . -name runme -execdir rm {} ;
This is an imposter rm command
$ ls -l bin
total 12
-r-x------ 1 shs shs 25 Oct 13 18:12 ls
-rwxr-x--- 1 shs shs 36 Oct 13 18:29 rm
-rw-rw-r-- 1 shs shs 28 Oct 13 18:55 runme
$ cat bin/ls
echo Running the $0 file
$ cat bin/rm
echo This is an imposter rm command

The -okdir option also asks for permission

To be more cautious, you can use the -okdir option. Like -ok, this option will prompt for permission to run the command.

$ find . -name runme -okdir rm {} ;
 ?

You can also be careful to specify the commands you want to run with full paths to avoid any problems with imposter commands like those shown above.

$ find . -name runme -execdir /bin/rm {} ;

The find command has a lot of options besides the default print. Some can make your file searching more precise, but a little caution is always a good idea.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author