Matthew Orndoff

Matthew Orndoff

Front-End React Developer. I can do all things through Cold Brew 🙏

find

Life is hard. Don’t make it harder by not knowing how to find your files. Let’s use find to make our lives easier. These commands all should work largely the same between Linux and Unix.

Find by Name and Type

# Find file tolkien.html in current working directory.
find . -name tolkien.html

# Find all files under /home/ with "A" in their name, case-sensitive.
find /home/ -type f -name "*A*"

# Find all symlinks under /var/ with 'c' in their name, case-insensitive.
find /var/ -type l -iname "*c*"

# Find all .txt or .doc files.
find . -iname "*.txt" -or -iname "*.doc"

# Find all sockets with permissions set to 777, pipe all errors to /dev/null (the void)
find / -type s -perm 777 2> /dev/null

# Find all .cpp files inside dir1 and dir2. Search two directories.
find dir1 dir2 -type f -name "*.cpp"

Find by User or Group

# Find all .html files in /etc/ owned by 'ents'
find /etc/ -group ents -name "*.html"

# Find all .php files in current working directory owned by user 'ghandalf'
find . -user ghandalf -name "*.php"

# Find files in /var/ that are NOT owned by user 'ghandalf'
# These two commands are semantically equivalent.
find /var/ -not -user ghandalf
find /var/ ! -user ghandalf

# Find all files in current working directory, that are NOT owned by the group 'ents'
# Search to a max depth of 3 directories.
find . -maxdepth 3 ! -group ents

Find by Time

# Find files in homedir modified MORE than a day ago.
find ~ -mtime +1

# Find files in /var/ modified LESS than a day ago.
find /var/ -mtime -1

# Find files in /etc/ modified MORE than 60 days ago.
find /etc/ -mtime +60

# Files in current working directory created within last 24hrs.
find . -ctime -1

# Find files in home directory accessed in last 5 minutes.
find ~ -amin 5

# Find files more reently modified than rivendale.cpp in current working directory.
find . -newer rivendale.cpp

Find by Size


# Find files greater than 100Mb in ~/Downloads
find ~/Downloads -type f -size +100M

# Find tar.gz files larger than 200Mb and run 'ls -l' on them.
# {} is a stand-in for each file found, the command is terminated by \;
find ~ -type f -name *.tar.gz -size +200M -exec ls -l {} \;

# Find mp4 files under 500k and delete them.
find / -type f -name *.mp4 -size -500k -exec rm -f {} \;

# Find all empty files in home directory.
find ~ -empty

# Find files in home directory betwen 50-100 MegaBytes
find ~ -size +50M -size -100M

Find and Do Something

# Find all .log files in /var/ and delete them.
find /var/ -name "*.log" -delete

# Find all .sh shell scripts in current working directory and execute them.
find . -name '*.sh' -exec perl -c {} \;

# List filetype information for every file in current working directory.
find . -exec file {} \;

# Finds files in current dir, run ls, and sort them by the 7h column.
find . -ls | sort -k 7

# Find all gif files in current working directory, and archive them in /tmp/
find . -name "*.gif" -type f | xargs tar -cvf /tmp/`date '+%d%m%Y'_archive.tar`

# Find all files in /var/www wiht 777 permissions and change them to 644
find /var/www -type f -perm 777 -print -exec chmod 644 {} \;

# Find all empty directories and delete them.
find /tmp -type d -empty -delete

More reading

Using -xargs with find 10 Examples of find in Unix shell.

Comments