Finding files in Linux

Finding Files using the locate command

If you want to find files fast just by the name of the files, part of the name or some regular expression - my favorite one is the locate command.

First you need to install the mloacte package:

$ sudo dnf -y install mlocate

Then create the database:

$ sudo updatedb

Find files by name

$ locate which

The above will find all the files that include which in their names.

Find the exact file name

$ locate "*/which"

Ignoring case

$ locate -i authors

Note: The locate command uses it’s database to search for the files so in case it wasn’t run for some time it may not have accurate results. Just make sure to run the updatedb command before doing the search.

Using the find command

The find command is a very powerful. You can not only find files but also act on the results (e.g. Deleting those files).

Find a file in the current directory

$ find -name somefile.txt

Find all the empty files

$ find / -empty

Using Regular expression

$ find / -name *.mp4

Output to a file

find / -name .mp4 -fprint outputfile

Ignoring case

$ find / -iname '*.TXT'

Find files that are just 2 levels deep

$ find ./mydir -maxdepth 2 -name "*.mp3"

Find files by file type

$ find -name which -type f

The above will find all the files named ‘which’.

Find supports other types as well: f: regular file d: directory l: symbolic link c: character devices b: block devices

Invert match

$ find ./mydir -not -name "*.mp3"

The ‘-not’ cab be replaced by ‘!’

Using Or
Find all the mp3 and mp4 files in the current directory

$ find -name '*.mp3' -o -name '*.mp4'

Search in multiple directories

$ find /dir1 /dir2 -type f -name "*.txt"

Finding files with permission 0755

find / -maxdepth 2 -perm 0755 2>/dev/null

We ignore the permission denied errors by directing the standard error to /dev/null

Finding file that are readable

$ find /somedir -maxdepth 3 -perm /a=r

Find files by size

Find all the files with size greater that 1G

$ sudo find / -size +1G

Ignore matches Ignore all the ‘.git’ directories

$ find -type d -path '.git' -prune -o -print

Putting files in a Tar archive

$ find . -type f -name "*.mp3" | xargs tar rvf songs.tar

Acting upon results

The powerfull aspect of the find command is acting upon the results. We’ll see some examples:

Show the top 10 largest files in a directory

$ find . -type f -exec ls -sh {} \; | sort -n -r | head -10

The “{}” is used as a placeholder for the files that find matches. The “\;” is used so that find knows where the command ends.

Remove files (Be careful!)

$ find /home/user -type f -name '*.tar.gz' -size +1G -exec rm -f {} \;

Find all the log files that contain the Error string

find . -type f -name "*.log" -exec grep -l Error {} \;    

We used ‘-l’ to get just the file names and not the lines that contain them

Copy one file to multiple directories

$ find dir1 dir2 dir3 dir4 -type d -exec cp somefile.txt {} \;



Leave a Comment