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 examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe