Skip to content

How to search files on CLI

Stop using locate on command line interface.

– locate uses a prebuilt database and can be updated only by root
– locate will fail to name files that were created since the last database update
– locate is insecure
– locate is not flexible.

Start using find. ‘man find’ is a great place to start. With find you can search files or directories by name, creation date, modification time, extension, size, owner, group, case sensitive and insensitive, path, readable, permissions, type and operatos. Also, you can use actions such as: delete, exec, print …

Basic examples:

# Find a file in a current working directory
find . -name example.txt

# Find a file in case insensitive (Example.txt, example.txt, example.TXT)
find . -iname example.txt

# Find on a specified dir example
find /path -name example.txt

# Find on multiple dirs
find /path /usr /etc -name example.txt

# Find for all files with a certain extension
find /path -name "*.c"

# Find by type
find /path -type d # for dirs
find /path -type f # for files

# Find by permission (example - with chmod 777)
find /path -perm 0777

# Find by size
find /path -size +100M               # a file with more than 100MB
find /path -size +100M -size -300M   # a file with more than 100MB but less than 300MB

# Find by modification time
find /path -mtime 1   # last 24 hours
find /path -mtime -3  # last 3 days

# Find by creation time
find /path -ctime +7  # more than 7 days ago
find /path -ctime -7  # less than 7 days ago
find /path -ctime 7   # exactly 7 days ago

# Find by accesed time
find /path -atime +7  # more than 7 days ago
find /path -atime -7  # less than 7 days ago
find /path -atime 7   # exactly 7 days ago

# Find by group (gid)
find /path -gid 5002

# Find by owner (username)
find /home -user www-data

# Find all empty
find /path -type f -empty # files
find /path -type d -empty # dirs
Published inFreeBSDLinuxUnix
© 2019 - Marian Amza. All rights reserved.
Powered by Wordpress & Debian