Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-15-2017, 03:14 AM
|
#1
|
LQ Newbie
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 12
Rep: 
|
cp -R doesn't search recursively
Code:
sudo cp -R /mnt/old-hdd1/{*.jpg,*.png,*.gif,*.jpeg,*.bmp,*.tiff} /home/name/restore/pics/
I think this command tells cp to search in all sub-sequent folders of "old-hdd1/" but it never copies any pictures, even though I KNOW for certain that there are enough, especially .jpg ones that I included in the cp command.
Did I phrase the command wrongly or is something wrong with the system?
|
|
|
06-15-2017, 07:26 AM
|
#2
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,939
|
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/. \;
However before doing the -exec portion, I'd see what the find, would find, by not having the -exec term. Note you can also use the search spec "*.jp*" to find both jpg and jpeg extensions.
|
|
1 members found this post helpful.
|
06-15-2017, 07:39 AM
|
#3
|
Member
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567
|
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
actually becomes
> cp -R /mnt/old-hdd1/*.jpg /mnt/old-hdd1/*.png /mnt/old-hdd1/*.gif /mnt/old-hdd1/*.jpeg /mnt/old-hdd1/*.bmp /mnt/old-hdd1/*.tiff DESTPATH
So, the command is actually looking for pictures immediately under /mnt/old-hdd1/ instead of recursively. Normally, the "-R" argument is used when copying entire directories and any subdirectories along. When you are looking for specific files or types of files, it is better to do a search first, like what rtmistler suggested with the find command. I invite you to read the man page for the find command. It is a very powerful command and will surely be useful in the future.
|
|
1 members found this post helpful.
|
06-15-2017, 07:41 AM
|
#4
|
LQ Newbie
Registered: Jun 2017
Posts: 16
Rep: 
|
Quote:
Originally Posted by Sideroxylon
Code:
sudo cp -R /mnt/old-hdd1/{*.jpg,*.png,*.gif,*.jpeg,*.bmp,*.tiff} /home/name/restore/pics/
|
I guess the files are in some subfolders below /mnt/old-hdd1/. With the command above you only hit the files that are directly in /mnt/old-hdd1/. For instance /mnt/old-hdd1/bold/beng.jpg does not match /mnt/old-hdd1/*.jpg but is does match /mnt/old-hdd1/bold/*.jpg .
|
|
|
06-15-2017, 08:05 AM
|
#5
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,333
|
Quote:
Originally Posted by aragorn2101
I invite you to read the man page for the find command. It is a very powerful command and will surely be useful in the future.
|
Well meant no doubt, but there be dragons - the find manpage is about the most inscrutable in existence.
|
|
1 members found this post helpful.
|
06-15-2017, 09:35 AM
|
#6
|
LQ Newbie
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 12
Original Poster
Rep: 
|
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.
|
|
|
06-15-2017, 09:48 AM
|
#7
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,939
|
Quote:
Originally Posted by Sideroxylon
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.
|
I agree with your starting philosophy and in fact had that perception once myself.
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.
|
|
|
06-15-2017, 12:13 PM
|
#8
|
LQ Newbie
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 12
Original Poster
Rep: 
|
Code:
sudo find . -iname "*.jpg" -iname "*.jpeg" -iname "*.png" -iname "*.gif" -iname "*.bmp" -iname "*.tiff"
I used this one but it gives no output.
|
|
|
06-15-2017, 12:16 PM
|
#9
|
LQ Newbie
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 12
Original Poster
Rep: 
|
Quote:
Originally Posted by rtmistler
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.
|
Actually I already did search for all files on the drive including picture, that's also why I certainly know that there are pictures of these types. But it doesn't help me, because I obviously can't just copy paste the output of ls into a cp command.
|
|
|
06-15-2017, 12:44 PM
|
#10
|
LQ Newbie
Registered: Jun 2017
Posts: 16
Rep: 
|
The find-cmd as suggested by rtmistler is nontheless best way for you though a bit steep.
Quote:
Originally Posted by Sideroxylon
Code:
sudo find . -iname "*.jpg" -iname "*.jpeg" -iname "*.png" -iname "*.gif" -iname "*.bmp" -iname "*.tiff"
|
You need to put "-or" between each off your filename conditions. Otherwise it looks for files that fulfill all these conditions, e.g. files that end with .jpg and .jpeg and so forth which is a contradiction.
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/
Why and how this exactly works is another story.
Last edited by sweepnine; 06-15-2017 at 12:46 PM.
|
|
|
06-15-2017, 12:47 PM
|
#11
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by Sideroxylon
Code:
sudo find . -iname "*.jpg" -iname "*.jpeg" -iname "*.png" -iname "*.gif" -iname "*.bmp" -iname "*.tiff"
I used this one but it gives no output.
|
try it like this
Code:
find . -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.tiff" \) -exec cp {} /media/data/testPhotos \;
you got a be in the working dir to run it. or you can replace the dot . with an absolute (search) path or two or three or four just add them in series using spaces between paths.
Code:
find /usr/local /etc /usr/bin -type f -name "*.goBa"
that will cause it to traverse through all of the paths one at a time
if you need to add more file types just repeat the pattern. -o -name "*.gif" etc..
Last edited by BW-userx; 06-15-2017 at 12:56 PM.
|
|
|
06-15-2017, 12:49 PM
|
#12
|
LQ Newbie
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 12
Original Poster
Rep: 
|
Quote:
Originally Posted by sweepnine
The find-cmd as suggested by rtmistler is nontheless best way for you though a bit steep.
You need to put "-or" between each off your filename conditions. Otherwise it looks for files that fulfill all these conditions, e.g. files that end with .jpg and .jpeg and so forth which is a contradiction.
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/
Why and how this exactly works is another story.
|
I read it implies the "-and" if I don't specificy anything. I assumed a different meaning there. Also the man page says that -exec is highly "insecure" and one should use -execdir instead.
|
|
|
06-15-2017, 01:46 PM
|
#13
|
LQ Newbie
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 12
Original Poster
Rep: 
|
Code:
find . -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.tiff" \)
Now this one definitely worked like a charm! Thanks for that.
|
|
|
06-15-2017, 01:52 PM
|
#14
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by Sideroxylon
Code:
find . -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.tiff" \)
Now this one definitely worked like a charm! Thanks for that.
|
you're welcome
|
|
|
All times are GMT -5. The time now is 03:44 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|