find

After a successful career in the United States Navy, Chief Petty Officer David Goggins has decided that he wants to master GNU/Linux, and become a RHEL Systems Engineer. One morning, after a 100 mile run, he decides that he is going to sit down and master two components of the find command: how to delete files that are found, and how to execute commands on the files that are found. Please give him 7 examples (one for each of the boats that he carried on his back during today's 100 mile run). Assume that he never runs commands as root, and that his username is king_of_no_excuses.

Sure, here are 7 examples that Chief Petty Officer David Goggins can use to master the find command in RHEL:

  1. Delete all files with a specific extension:
    • find /path/to/search -type f -name "*.txt" -delete
    • This command finds all files with the extension .txt in the specified directory and deletes them.
  2. Delete all empty directories:
    • find /path/to/search -type d -empty -delete
    • This command finds all empty directories in the specified directory and deletes them.
  3. Delete all files older than a specified number of days:
    • find /path/to/search -type f -mtime +30 -delete
  4. Delete all files except for those with a specific pattern in the filename:
    • find /path/to/search -type f ! -name "*pattern*" -delete
    • This command finds all files in the specified directory that do not contain the specified pattern in the filename and deletes them.
  5. Execute a command on all files that match a pattern:
    • find /path/to/search -type f -name "*.txt" -exec grep "pattern" {} \;
  6. Execute a command on all files that match a pattern and create a new file with the output:
    • find /path/to/search -type f -name "*.txt" -exec grep "pattern" {} \; > output.txt
    • This command finds all files with the extension .txt in the specified directory, executes the grep command on each file to search for the specified pattern, and then creates a new file called output.txt with the search results.
  7. Execute a command on all directories that match a pattern:
    • find /path/to/search -type d -name "*pattern*" -exec ls -l {} \;

This command finds all directories in the specified directory that contain the specified pattern in the name and executes the ls -l command on each directory to list its contents.

After practicing these examples, Chief Petty Officer David Goggins should have a solid understanding of how to use the find command to delete files and execute commands on files that are found.

 

 

You should also read:

find

Rihanna is a great singer by night, and a RHEL Sysadmin at Google by day. She is teaching a group of new hires…

find

Hector is a RHEL Sysadmin at IBM. He wants to find and delete all images with a .jpg extension in /home/hectorz/images/. What command…

find

Alicia is a RHEL Sysadmin at Red Hat. Red Hat is purging any files that are in a proprietary file format from RHEL_Server_01.

find

Regina wants to search for and delete all files in /home/regina/data/ that have a .odt extention. Her computer system is a modern RHEL…