LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   hopefully simple 'cp' question (https://www.linuxquestions.org/questions/linux-newbie-8/hopefully-simple-cp-question-897481/)

frumpus 08-15-2011 04:25 PM

hopefully simple 'cp' question
 
I want to copy all files with the extension .afm from:

/usr/local/texlive/2011/texmf-dist/fonts/afm

...into a single directory. The problem is that they are split into several levels of subdirectories under that directory. There are TONS of subdirectories and levels in that /afm directory and I don't want to have to drill down into each one to copy the files out. Surely there is a simple way to do this but I am not finding it.

I have tried:

cp *.afm /home/username/afm-files

but it doesn't find any afm files to copy as it only looks in the top level directory.

can anyone tell me what I'm missing here?

frumpus 08-15-2011 04:33 PM

Oh I have also tried cp -r which I thought would check the subdirectories but no luck there either.

This is the error I get.

Code:

username@username [/usr/local/texlive/2011/texmf-dist/fonts/afm]
$ cp -r *.afm /home/username/afm-fonts
cp: cannot stat `*.afm': No such file or directory


repo 08-15-2011 04:42 PM

You can use the find command
Code:

find startdirectory -iname "filetype" -exec cp {} target_dir \;
Kind regards

TobiSGD 08-15-2011 04:44 PM

This is a perfect case to use the find command:
Code:

find /usr/local/texlive/2011/texmf-dist/fonts/afm -name "*.afm" -exec cp '{}' /home/username/afm-files \;
This will search in the directory /usr/local/texlive/2011/texmf-dist/fonts/afm recursively for files that match the name *.afm and pass the filenames to the cp command, which will copy the files to /home/username/afm-files.
For more info have a look at
Code:

man find

frumpus 08-15-2011 04:49 PM

I will give that a try, thanks!

TobiSGD 08-15-2011 04:55 PM

You have to escape the curly brackets, that can simply be done with single quotes '{}'. You also forget the ; at the end of the command, this has also to be escaped, you can do this with single quotes also ';', or you use the backslash \;.
For matching the filename you also need the wildcard *.
Code:

find /usr/local/texlive/2011/texmf-dist/fonts/afm -iname "*.afm" -exec cp '{}' /home/username/afm-fonts \;

frumpus 08-15-2011 05:00 PM

Thanks guys, 'find' was exactly what I needed!


All times are GMT -5. The time now is 10:44 PM.