LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 06-15-2017, 02:14 AM   #1
Sideroxylon
LQ Newbie
 
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 10

Rep: Reputation: Disabled
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?
 
Old 06-15-2017, 06:26 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,050
Blog Entries: 13

Rep: Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498
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.
Old 06-15-2017, 06:39 AM   #3
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 552

Rep: Reputation: 270Reputation: 270Reputation: 270
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.
Old 06-15-2017, 06:41 AM   #4
sweepnine
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
Quote:
Originally Posted by Sideroxylon View Post
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 .
 
Old 06-15-2017, 07:05 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,134

Rep: Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927
Quote:
Originally Posted by aragorn2101 View Post
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.
Old 06-15-2017, 08:35 AM   #6
Sideroxylon
LQ Newbie
 
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 10

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-15-2017, 08:48 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,050
Blog Entries: 13

Rep: Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498Reputation: 3498
Quote:
Originally Posted by Sideroxylon View Post
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.
 
Old 06-15-2017, 11:13 AM   #8
Sideroxylon
LQ Newbie
 
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 10

Original Poster
Rep: Reputation: Disabled
Code:
sudo find . -iname "*.jpg" -iname "*.jpeg" -iname "*.png" -iname "*.gif" -iname "*.bmp" -iname "*.tiff"
I used this one but it gives no output.
 
Old 06-15-2017, 11:16 AM   #9
Sideroxylon
LQ Newbie
 
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
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.
 
Old 06-15-2017, 11:44 AM   #10
sweepnine
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
The find-cmd as suggested by rtmistler is nontheless best way for you though a bit steep.

Quote:
Originally Posted by Sideroxylon View Post
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 11:46 AM.
 
Old 06-15-2017, 11:47 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,140

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Quote:
Originally Posted by Sideroxylon View Post
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 11:56 AM.
 
Old 06-15-2017, 11:49 AM   #12
Sideroxylon
LQ Newbie
 
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sweepnine View Post
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.
 
Old 06-15-2017, 12:46 PM   #13
Sideroxylon
LQ Newbie
 
Registered: Jun 2017
Distribution: Debian 10 Buster
Posts: 10

Original Poster
Rep: Reputation: Disabled
Code:
find . -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.tiff" \)
Now this one definitely worked like a charm! Thanks for that.
 
Old 06-15-2017, 12:52 PM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,140

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Quote:
Originally Posted by Sideroxylon View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search recursively by exif date? rioguia Programming 6 02-21-2010 05:49 AM
How to search through all files in a folder, recursively... MJBoa Linux - Software 13 09-14-2008 11:25 PM
Search a string recursively under one directory say_hi_ravi Programming 6 07-09-2008 04:47 AM
recursively search basak Linux - Software 1 08-13-2006 10:25 AM
To recursively search with grep grautu Slackware 5 11-21-2005 02:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration