Redhat – How to find all files of a specific type in RHEL on the command line

findredhat

I am looking for files of type .flv, .avi, etc

I would like to see a list of all the files (on my server) of those file types (I don't mind doing it one by one, i.e. seeing all the files of .flv, then all the files of .avi).

Then, once I identify the ones I want to move, how do I copy several files that are scattered all over the filesystem to a specific folder?

Best Answer

There are several ways to search for files on GNU/Linux systems. The two main ways are locate and find:

  • locate uses a database of files known in the whole system to find documents. It is very useful but requires to keep this database up-to-date (with updatedb), which can take a long time;
  • find searches for files in a given directory. It is usually slower than locate (it has no persistent database) but is more fine-tuned.

So, if you need to find all files on your system that match your criteria, you can use locate:

$ locate --regex "avi|flv" | grep '\.\(avi\|flv\)$'

whereas if you're searching in a specific directory and want to make sure to have no cache delay effect, you can use find:

$ find /path/to/your/directory -regex '.*\.\(avi\|flv\)'

Now, to copy these files to a specific folder:

$ locate --regex "avi|flv" | grep '\.\(avi\|flv\)$' | xargs cp /path/to/specific/folder

or

$ find /path/to/your/directory -regex '.*\.\(avi\|flv\)' -exec cp {} /path/to/specific/folder \;