Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 ?

Thanks.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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 \;
share|improve this answer
    
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 '11 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. –  ℝaphink May 27 '11 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 '11 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 '11 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 –  ℝaphink May 27 '11 at 10:49
add comment

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.

share|improve this answer
    
Ah thanks. I had tried something like \.(avi|flv)$ without success ;-) –  ℝaphink May 27 '11 at 10:48
add comment

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)

share|improve this answer
    
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 '11 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. –  ℝaphink May 27 '11 at 7:55
    
@marc.gayle: for fred in ... defines a loop with variable fred, which is then used as $fred inside the loop. –  ℝaphink May 27 '11 at 7:57
    
yep $fred = arbitrary variable name for use in for loops... –  Rhys May 27 '11 at 8:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.