Linux find: search files by name


Efficiently Finding and Archiving Specific File Types in Linux

In the world of Linux, the `find` command is an incredibly powerful tool, especially when you need to locate files of different types by their names. Let’s dive into a common scenario: You need to find all `*.php`, `*.js`, and `*.css` files in a folder for archiving, regardless of the case sensitivity of the file names.

Locating the Files

Firstly, navigate to the target directory:

cd /home/taras/public_html

Then, execute the following `find` command:

find . -type f \( -iname '*.php' -o -iname '*.js' -o -iname '*.css' \) -print > /home/taras/list-to-archive.txt

This command breaks down as follows:

  • `find .` starts the search in the current directory.
  • `-type f` looks for files (not directories).
  • `\( -iname ‘*.php’ -o -iname ‘*.js’ -o -iname ‘*.css’ \)` searches for files ending in `.php`, `.js`, or `.css`, using `-iname` for case-insensitive matching.
  • `-print` outputs the found file names.
  • `> /home/taras/list-to-archive.txt` redirects the output to a file.

Archiving the Files

The file `/home/taras/list-to-archive.txt` now contains a list of the files you’ve found. To archive them, use the `tar` utility:

tar -cpjf /home/taras/archive.tar.bz2 -T /home/taras/list-to-archive.txt

Case-Sensitive Search

If you prefer a case-sensitive search, simply replace `-iname` with `-name` in the find command.


Using the `find` command in Linux for locating specific file types and archiving them can greatly simplify file management tasks. Whether you’re dealing with case-sensitive or case-insensitive requirements, this method is both efficient and effective.