Pre-defined Actions in the Find Command in Linux
The find
command in Linux not only allows users to search for files and
directories but also to perform predefined actions on the results. These actions
can be anything from simply printing the file names to executing complex
commands. Understanding these actions is essential for anyone who wants to
automate their file management tasks effectively.
Here is a table listing the available predefined actions for the find
command,
along with a description of what each action does:
Action | Description |
---|---|
-print | Displays the path of the found item. This is the default action if no other action is specified. |
-print0 | Similar to -print , but the output is terminated by a null character. This is useful when dealing with unusual filenames. |
-exec | Executes a command on the found files. This is followed by a command string, terminated by ; or + . |
-execdir | Similar to -exec , but the specified command is run from the subdirectory containing the matched file. |
-ok | Like -exec , but prompts the user for confirmation before executing the command. |
-okdir | Like -execdir , but prompts the user for confirmation before executing the command. |
-delete | Deletes the found files or directories. Use with caution. |
-ls | Lists the current file in ls -dils format on standard output. |
-fls | Like -ls , but writes the output to the given file list. |
-prune | If the file is a directory, do not descend into it. If you're looking for directories, this will stop find from going into the found directories. |
-quit | Exits immediately. No further files are processed after a file is found that matches the criteria. |
Examples of Using Pre-defined Actions with the Find Command
Below are examples illustrating how you can use these predefined actions on an Ubuntu system:
Example 1: Using -print
to display found items
find /var/log -name "*.log" -print
This command lists all .log
files in the /var/log
directory.
Example 2: Combining -print0
with xargs
to handle filenames with spaces
find / -type f -name "*.conf" -print0 | xargs -0 grep "ServerName"
This command finds .conf
files and uses grep
to search for the term "
ServerName" within them, handling filenames that contain spaces or other unusual
characters.
Example 3: Using -exec
to execute a command
find /tmp -type f -name "*.tmp" -exec rm {} \;
This will find and delete all .tmp
files in the /tmp
directory.
Example 4: Using -execdir
for more security
find /home -type f -name "*.sh" -execdir chmod u+x {} \;
This command will locate all .sh
files and make them executable. It changes
the permissions only within the directory where the file is located, which is
more secure than -exec
.
Example 5: Using -ok
to prompt before execution
find / -type f -name "config.php" -ok cp {} {}.backup \;
Before copying each config.php
file to config.php.backup
, this command will
prompt the user for confirmation.
Example 6: Using -delete
to remove empty directories
find /tmp -type d -empty -delete
This command finds and deletes all empty directories within /tmp
.
Example 7: Listing found files with -ls
find /etc -type f -name "hosts" -ls
This will list the hosts
file located in /etc
with details similar
to ls -l
.
Example 8: Using -prune
to exclude directories
find / -name ".git" -prune -o -name "*.js" -print
This will search for .js
files but will not descend into directories
named .git
.
Example 9: Stopping the search with -quit
find / -type f -name "fstab" -print -quit
This command will print the path of the fstab
file and then exit without
searching for more instances.
When using these predefined actions, it's important to be aware of their impact,
especially with actions like -delete
that can remove files and directories.
Always double-check the command and, if necessary, run a trial with -print
before executing
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.