cp -R doesn't search recursively
Code:
sudo cp -R /mnt/old-hdd1/{*.jpg,*.png,*.gif,*.jpeg,*.bmp,*.tiff} /home/name/restore/pics/ Did I phrase the command wrongly or is something wrong with the system? |
Hi Sideroxylon and welcome to LQ.
I haven't used that type of a command to find and copy specific file types. Instead what I do is to use a find -exec command, such as: Code:
$ sudo find /mnt/old-hdd1 -name "*.jpg" -exec cp {} /home/name/restore/pics/. \; |
Hi Sideroxylon and welcome to LQ,
The "-R" option for cp certainly means copy while accessing directories recursively, but the culprit here is the {}. If you expand your command you will understand what's going on: Code:
> cp -R /mnt/old-hdd1/{*.jpg,*.png,*.gif,*.jpeg,*.bmp,*.tiff} DESTPATH |
Quote:
|
Quote:
|
Thank you for extensive explanation, I understand now. Well, first of all I really wanted to copy the files and not only search. I guess I didn't make it clear enough since I used the word "search" in connection with the cp command. Secondly, this was my thought:
1. cp -R /one/pc/dir/ copies recursively 2. {*.png,*.etc} copies different file types 1. + 2. = 3. copies different file types recursively. But I understand now that it obviously works differently when combining these two. |
Quote:
I've learned over time that I really do wish to search in advance to know what it will find and copy, or do some other operation I'm working on. Per aragorn2101's and syg00's points. The man pages are both, "very helpful and usually 100% correct", and also "sometimes elusive or difficult to read/interpret". I learned find incrementally. First how to find a file from the current working directory of a certain pattern name (regular expression I believe applies here). Then I learned how to do the -exec portion. That's about it, find may have tons more properties, but that seems to have served me well. On the other side, I absolutely moved away from cp -R for when I did not wish to copy an entire tree unconditionally. I would use that, or the -a flag to copy an entire tree. But to locate only certain pattern filenames and do something with them, I stick with find. |
Code:
sudo find . -iname "*.jpg" -iname "*.jpeg" -iname "*.png" -iname "*.gif" -iname "*.bmp" -iname "*.tiff" |
Quote:
|
The find-cmd as suggested by rtmistler is nontheless best way for you though a bit steep.
Quote:
As next step, try the -exec part in rtmistler answer. The syntax of find is akward here but it will work. Alternatively you may pass finds output to the cp cmd as follows. Code:
find . -iname "*.jpg" -or -iname "*.jpeg" | xargs cp -t /home/name/restore/pics/ |
Quote:
Code:
find . -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.tiff" \) -exec cp {} /media/data/testPhotos \; Code:
find /usr/local /etc /usr/bin -type f -name "*.goBa" if you need to add more file types just repeat the pattern. -o -name "*.gif" etc.. |
Quote:
|
Code:
find . -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.tiff" \) |
Quote:
|
All times are GMT -5. The time now is 03:21 AM. |