2

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?

3 Answers 3

3

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 \;
5
  • I am now running updatedb and the cursor is just blinking. How can I know when it is done ? Should I open a new terminal window and do something else ? The last time I tried a large operation, the cursor just stayed blinking forever - until I quit it after about 6 hours, by that time I was pretty certain it had finished. There another way I can know ?
    – marc.gayle
    May 27, 2011 at 7:46
  • One of the fondamentals of Unix commands is that they don't output anything unless necessary, so it is normal for a command to stay blinking while it's working, unless it's running in verbose mode. You can use the --verbose flag to udpatedb if you want to see all it's doing, but beware that it will make the process even longer, since printing of all files will limit the processing speed.
    – raphink
    May 27, 2011 at 7:54
  • Ahh...thanks....I don't mind that, at least I will know when it's done than when it's not.
    – marc.gayle
    May 27, 2011 at 8:03
  • Is there any flag that I can pass that can tell me how much has been completed? In a %, for instance ?
    – marc.gayle
    May 27, 2011 at 8:28
  • @marc: Not that I know of. What you can do is combine --verbose with a pipe to pv to see the throughput bandwidth (how fast it's being processed): updatedb --verbose | pv
    – raphink
    May 27, 2011 at 10:49
1

Expanding on Raphnik's comment, you can save yourself a pipe through grep by puting into the regex part of locate or find commands: '\.avi$|\.flv$' -- it will match files with .avi and .flv extensions.

1
  • Ah thanks. I had tried something like \.(avi|flv)$ without success ;-)
    – raphink
    May 27, 2011 at 10:48
0

Probably more elegant ways to do this with find -exec or xargs but here's a quick hack:

updatedb 

then

for fred in `locate *.flv | awk ' { print $1 } '` do;
mv "$fred" /my/dest/folder/
done

(etc)

4
  • Forgive the silly question, but is all of that code ? as in...for fred ? That seems a bit strange to me, but I am a complete Linux n00b, so I could be misunderstanding. Or am I supposed to substitute something else for fred ?
    – marc.gayle
    May 27, 2011 at 7:45
  • locate only outputs file names, so I don't see the point in the awk pipe. Also, for cuts on spaces, so files with spaces will fail with that loop. Also, this could all be done with xargs instead of a loop, especially since mv can take more than 2 arguments without a problem. Then, the question was not to move the files but to copy them. Using this command will result in losing the original files. Finally, locate doesn't match on whole filenames unless you specifiy it, so *.flv is not a best match.
    – raphink
    May 27, 2011 at 7:55
  • @marc.gayle: for fred in ... defines a loop with variable fred, which is then used as $fred inside the loop.
    – raphink
    May 27, 2011 at 7:57
  • yep $fred = arbitrary variable name for use in for loops...
    – Rhys
    May 27, 2011 at 8:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .