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. Credit: Alan Levine 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. Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe