Linux: Copy all files by extension to single dirrectory

I am trying to copy all *.tif files from ./old to the ./new. In ./old i have lots of subdirs with different files, and in ./new i need only TIF files, without folder tree. So, I had tried cp -vR ./old/*.TIF ./new and got an error:

No such file file or dirrectory "./old/*.TIF"

What I'm doing wrong?

3 Answers

Use find for this find . -name "*.TIF" -exec cp {} new \;

So find is used to find files. The command is saying find files starting from here . where the name of the file -name ends in .tif remember the double quotes for shell expansion. So to find all the tif files is simply.

find . -name "*.tif" ./2/3/3.tif ./2/2.tif ./1.tif 

We then use -exec to do something with files in this case cp found files {} to the target directory new followed by an escaped semicolon \;

0

It should be like this. You have to enter old directory:

cd old cp -R *.tif ../new 

in old directory (direct) you might not be having any .tif files.

moreover you can get a list of files and copy them using a simple script

find /old -type f | xargs grep *.tif will give you the list

you can do like

for i in `find /old -type f | xargs grep *.tif` do cp $i /new done; 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like