Using find and exec command on Ubuntu
On Ubuntu the find command is a very powerful tool used for searching files and directories based on various criteria such as name, size, or timestamp. When combined with exec, it becomes even more versatile, allowing users to perform perform actions on the files found, such as deleting, moving, or executing commands.
The -exec option allows you to execute a command on each file that matches the criteria.
Example 1: Find temp files and delete them
find /path/to/search -name "*.tmp" -exec rm {} \
- {} is a placeholder for the found file.
- \; signifies the end of the -exec command.
Example 2: Find temp files and delte them with xargs
The xargs command is often used with find to handle large numbers of files more efficiently.
Deleting all .tmp files using xargs:
find /path/to/search -name "*.tmp" -print0 | xargs -0 rm
–print0 prints the full file name on the standard output, followed by a null character (instead of the newline character).
–0 option in xargs tells it to expect input items to be terminated by a null character.
On this link you can find how to remove files older than x days.