GIT – ‘find’ – search for files in a directory hierarchy, we have some tips with ‘Searching For Files with Find’ and ‘Find Large Files Fast’

1. Searching For Files with Find

I’ll start with some quick examples for the “copy and paste” crowd, which I’m usually a part of.

Search the entire file system for files named .ini.

find / -name 'php.ini'

Search everything under the /etc directory for files named nginx.conf.

find /etc -name 'nginx.conf'

Search the entire file system for file names ending in .cnf.

find / -name '*.cnf'

If your like me when I was starting out you’ve probably fumbled with the findcommand before. Like many powerful command line tools you need to learn a few basics before you can start harnessing any of its powers.

The two things you need to know about find are how to specify the search path and file name to look for. The search path is the first parameter and it defaults to searching the directory your currently in. It’s a bit counter intuitive since these days most tools search everything by default and then provide filters. To specify the entire filesystem use “find /”.

The second most used flag is “-name”. This allows you to specify the file name you’re looking for. Be sure to include the actual file name in quotes or you’ll get unexpected results. You can use the * symbol for wildcard matches.

I’ll mention one more option that you’ll find helpful when your searching for files and that’s the “-user” flag. Specifying a user filters search results to only show files owned by that user. The “-user” flag doesn’t require quotes.

Find all php files owned by the web .

find / -user www-data -name '*.php'

2.Find Large Files Fast

We’ve all been there, your 10GB VPS server is running out of space or that 3GB virtual machine your using as a dev server is bursting at the seems and you need to free up some space quickly.

Use these searches to find the largest files on your server and start clearing up space for more 1′s and 0′s.

Find files over 1GB

find / -type f -size +1000000k -exec ls -lh {} \;

Find files over 100MB

find / -type f -size +100000k -exec ls -lh {} \;

Find files over 10MB

find / -type f -size +10000k -exec ls -lh {} \;

The first part is the find command we’ve gone over before except this time we’re using the “-size” flag to find files over different sizes measured in kilobytes.

The last bit on the end starting with “-exec” allows us to specify a command we want to execute on each file we find. Here we’re executing the “ls -lh” command to include all the information we’re used to seeing when listing the contents of a directory. The h towards the end is especially helpful as it prints out the size of each file in a human readable format i.e. Megs and Gigs.

Print Friendly

Comments

comments

Bài viết liên quan