Select Page

When you’re working directly on a Linux server it’s sometimes helpful to be able to find all files that end in a certain way (e.g. everything that ends with .old). Fortunately Linux provides the solution in the form of the find command.

An example might be if you’ve been keeping copies of edited files by adding a ‘.old’ extension to a copy of the original. You really don’t want all these obsolete files hanging around on your server so here’s a handy command to help you find them all.

find . -type f -name '*.old'

Let’s break this command down so you understand what’s going on:
find . will search in the current directory.
-type f indicates that we’re only interested in actual files (not directories)
-name ‘*.old’ tells the find command that we’re looking for all files that end in .old.

Searching for text within those files

Okay, what if you want to search for the word ‘users’ in all the .sql files on the server?

That’s easy to do – first we need to ask the server for all of the .sql files, then we pass the result into the grep command with a little help from xargs:

find . -type f -name '*.sql' | xargs grep 'users'

Again, let’s break that down so you can see what’s going on:

find . will search in the current directory.
-type f indicates that we’re only interested in actual files (not directories)
-name ‘*.sql’ indicates we’re searching for .sql files
| indicates that we want to feed the output of the command into the command that follows
xargs converts the list of filenames from find into individual filenames for the grep command
grep ‘users’ indicates we are looking for the word users within the filename provided by xargs.

Further information

If you want to know about the individual commands, find, xargs, and grep, here’s some links to the man pages online: