Practical Uses of the find Command in Linux

Hey! Today, I took a deep dive into the find command and thought I’d share my notes in case it’s helpful to anyone.

Find Files by Extension

The find command is super handy for locating files based on specific criteria. If you need to find all .txt files within the Desktop directory, this command will do the trick:

find Desktop -name '*.txt'

This scans the Desktop directory for any file name indicated by the * wildcard with a .txt extension and returns their paths.

Exclude Specific Results with grep

Sometimes, you might want to exclude certain files from the search results. You can chain commands together using the | (pipe) symbol. For example, if you want to exclude files with “testing” in the name, you’d use:

find Desktop -name '*.txt' | grep -v testing

This is how it breaks down:

  • find Desktop -name '*.txt': Finds all .txt files in the Desktop directory.
  • | grep -v testing: Filters out any files with “testing” in the name.

Example Output:

You can see when I search for all txt files in the Desktop directory below, I’m getting two files test.txt and testing.txt:

~ » find Desktop -name '*.txt'
Desktop/testing.txt
Desktop/test.txt
~ » find Desktop -name '*.txt' | grep -v testing 
Desktop/test.tx

In the second search, the -v flag in grep ensures that any lines containing the word “testing” are not included in the output.

Find Files by Name and Type

You can also find specific files by name and filter by type. If you need to find a file named Documents that’s specifically a regular file, you’d go with:

find . -name Documents -type f

This searches the current directory . for files named Documents that are of type f (regular file). In addition to regular files (f), you can also use other types like directories (d), symbolic links (l), or block devices (b). For example, to find directories named “Documents,” you would use -type d.

Change File Permissions

One of the cool things you can do with find is execute commands on the files it locates. For example, if you want to change the permissions of all files in the Desktop directory to 600, use this:

find Desktop/ -type f -exec chmod 600 {} +

Here’s what’s happening:

  • find Desktop/ -type f: Finds all files in the Desktop directory.
  • -exec chmod 600 {} +: Changes permissions for each found file to 600, which restricts read and write permissions to the file owner only.

Save Results to a File

If you need to save the list of files found by your search to a text file, redirect the output like this:

find . -name '*.js' > results.txt

This command finds all .css files in the current directory and saves the list into results.txt.

Find Files by Size and Execute a Command

The find command can also be used to locate files by size. For instance, if you want to find all files that are exactly 1033 bytes in size and then execute the file command on them, you’d use:

find . -type f -size 1033c -exec file {} +

Here’s what this does:

  • find . -type f -size 1033c: Finds all regular files in the current directory that are exactly 1033 bytes (1033c specifies the size in bytes).
  • -exec file {} +: Executes the file command on each of these files, which tells you the file type.

Find Files by User and Size

If you need to find files owned by a specific user and of a specific size, you can combine these criteria. For example, to find files owned by the admin user and exactly 26 bytes in size across the entire file system, use:

find / -type f -user admin -size 26c

This command will:

  • find / -type f -user admin: Search the entire file system (/) for files owned by the user admin.
  • -size 26c: Filter the results to include only files that are exactly 26 bytes.

Find Files Newer Than a Reference File

The find command can also be used to locate files that have been modified more recently than a specific reference file. This is useful when you want to identify files that have been updated after a certain point in time. Here’s how you can do it:

find . -type f -newer reference_file

Here’s what this does:

  • find . -type f: Finds all regular files in the current directory and its subdirectories.
  • -newer reference_file: Filters the results to include only files that have a more recent modification time than the reference_file.

This command is especially helpful when you need to track changes made after a particular file was last modified. If reference_file is a symbolic link, and the -H or -L options are in effect, the modification time of the file it points to will be used.

That’s all my notes for today! Hopefully, this helps anyone else trying to get more comfortable with the find command.

Extra resources:


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *