BashItOut

Find Examples For Linux

find is a very versatile tool. It can do a lot more than just hunt for files.

If you’re new to find, checkout my post How To Use The GNU - Linux Find Command

Here’s some examples of its features that come in handy for everyday usage.


Looking for a file in the current directory

find . -name "my_file.txt"

The first argument is the root directory and find is recursive by default, so this will return a list of all files in the current directory and any of its subdirectories with the name “my_file.txt”.


Looking for all png files in an folder:

find /path/to/my/dir -iname "*.png"

If you have a filesystem based cache, it can be really handy to just purge the files that haven’t been used for a while.

All files that haven’t been touched for at least 7 days:

find /path/to/my/dir -atime +7

Sometimes you need to purge the cache but you only care about binning the PNGs.

Delete all pngs that haven’t been accessed in the last month

find /path/to/my/dir \
-iname "*.png" \
-atime +31 \
-exec rm -fv {} \;

If you’re running out of space, hunting down large files from the command line can be a pain, but not with find.

Files in your home directory that are larger than 500MB

find $HOME -size +500M

Some units that are common for the -size argument:

c : Bytes

k : Kilobytes

M : Megabytes

G : Gigabytes


Mac and Windows litter the file systems with metadata files. This might be fine in a fully homogeneous environment, but when you have may different OSs sharing network storage, these .DS_Store and Thumbs.db files can be an unsightly mess.

To remove Mac OS X and Windows messy files you could use:

find /path/to/my/dir \
-regextype posix-egrep \
-iregex ".*thumbs.db|.*\._.*|.*\.ds_store" \
-exec rm -fv {} \;

Tags: Linux

blog comments powered by Disqus

About

@MTerzza Twitter Icon

+MikeTerzza Google Plus Icon

Atom | RSS RSS Icon

Recent Posts:

Monitoring the progress of dd

RTL SDR Frequency Drift Offset

Random Seriousness with Python and Password Generation

Random Fun and the Busy Linux Geek

Ampersands & on the command line