Using Linux Find

Published in Software
August 25, 2024
3 min read
Using Linux Find

The find command is one of the most useful out there, it finds things. Along with other common command line programs that are built-in to Linux such as grep, find allows you to search many files a lot quicker than you’d typically be able to with a graphical user interface (GUI).

Searching for a specific File

The basic way to use “find” would be something like this…

find / -name "filename.txt"

So, for example, it’s been a few years since I looked at my CV and I’ve rearranged the SSDs since then so I couldn’t actually find a CV of mine recently. I knew the filename of my CV began with my name, so I did a search like this where the ”*” character is a wildcard…

find / -name "neil-ludlow*"

To silence permission errors use…

2>/dev/null

… and to actually search the places where you might get permission errors use sudo.

To find my CV I could also search like this, -iname is a case-insensitive name, and I’m using more wildcards and specifying the filetype…

find / -iname "*neil*ludlow*.pdf" 2>/dev/null

In this case, I had no idea where my CVs were, so I had to search everything with the / location.

If I knew roughly where the file was, or which directory it was in, and I knew that it was a PDF, I could do something like this, for example…

find ~/Documents/ -iname "*neil*ludlow*.pdf" 2>/dev/null

Listing the contents of a Directory

The universal list command which works on most terminals (Linux, Mac, Windows) is…

ls

This gives a list of filenames that spread across the terminal in columns.

If I want a single list, i.e. one column, with slightly more data such as date and filesize, I can use this on Linux…

ls -al

Because I use the Ubuntu flavour of Linux, I can simplify ls -al to…

ll

What this will show is the contents of a directory, including any other directories that may be inside it.

To actually list the contents of a directory, and see the contents of the subdirectories in the same list, I can use find

find ~/Documents/ -ls

I could also do something like this using the ls or ll commands, but it looks different on the screen, grouping the different subdirectories instead of presenting one long list…

ls ~/Documents/ -Ral
ll ~/Documents/ -R

Searching the Contents of Files

By combining find with grep we can search based on the contents of the file…

find . -iname "*.mdx" -exec grep -Hi communication {} \;

. in find is searching the current directory. -H in grep means print the filename for each match. -i in grep means to ignore case, i.e. case-insensitive.

When I do this search in the directory where I keep the posts for this website, I currently get four posts returned. The search gives one line per occurrence of the word “communication” inside each file, so 12 lines in total. I could use something like this in a bash script to rank each post based on the number of times a word appeared in the content.

Find by type

Going back to my search for my CV. If I had no idea where the CV was, I’d use the / location. In that case, the search results may be returning a lot of directories as well as files.

To specify that I only want to see files, I could do this…

find / -iname "*neil*ludlow*.pdf" -type f 2>/dev/null
  • f = file
  • d = directory
  • L or l = alias (or symlink)
  • c = character devices
  • b = block devices
  • p = named pipe (FIFO)
  • s = socket

Maxdepth

The -maxdepth flag limits the amount of recurring levels find will go into. Searching for just the top-level directories in your home directory would be…

find ~/ -type d -maxdepth 1

Find by filesize

Exactly 1024 bytes…

find /tmp -type f -size 1024c

Less than 1MB, the ”-” prefix…

find /tmp -type f -size -1M

Greater than 2MB, the ”+” prefix…

find /tmp -type f -size +2M
  • b = 512-byte blocks (default)
  • c = bytes
  • w = two-byte words
  • k = Kilobytes
  • M = Megabytes
  • G = Gigabytes

Find empty files

find / -type f -empty

Find files by age

Go back one day…

find ~/ -mtime 1

-daystart measures from the beginning of today, rather than from 24 hours ago. So, for all the files in your home directory that were modified yesterday…

find ~/ -mtime 1 -daystart

Or, something like this might be more useful, list all the PNGs that were modified yesterday…

find ~/ -daystart -type f -mtime 1 -iname "*.png" -ls

Find files by Permissions

Find the files with a permission of “644”…

find ~/ -maxdepth 2 -perm 644 -ls

See, also, these work slightly differently because of the ”/” and ”-” prefixes…

find . -perm /444
find . -perm -664

Find files by Owner

Find abd list files with my username…

find ~/ -maxdepth 1 -type f -user neil -ls

Changing Ownership with Find

“www-data” is the user for a webserver. To change files owned by the webserver and to assign them to another user you can do something like this…

find / -user www-data -type f -exec chown someoneelse {} \;

Find and Delete

Use with caution, best to list the files first to check before running again and deleting…

find /var/log/ -name `*.temp` -delete

The Find Command Documentation

As with all the main build-in Linux commands, there is help available on the command line…

find --help

There is also Findutils documentation online, including the manual GNU Findutils manual.

Previous Article
Why
Next Article
Choices