LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 09-10-2009, 04:59 PM   #16
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49

ok, so i do need the backslash before the *.jpg, i think i got it.

now if right below my root level of where all the folders were at, my first level of subfolders all started with numbers, then had some random text, then a year, is there any way to do this for say all folders directly beneath that root level (and those folders subsequent subfolders) that start with 04 then have some text then 2009?
 
Old 09-10-2009, 04:59 PM   #17
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
thanks
 
Old 09-10-2009, 05:06 PM   #18
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
'04*2009/*.jpg' I guess, if I understand what you meant.
 
Old 09-10-2009, 05:14 PM   #19
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Sorry to complicate things a little bit, but if some of the filenames contain whitespace, you want use nulls between filenames in the result.

find . -iname "*.jpg" -print0 | xargs -0 cp -t /usr/name/pictures/

Be sure you read the `xargs' man page. There are 3 options that deal with limiting the number of arguments (filenames) that xargs receives. If you have thousands of files or hundreds of very long filenames, you could have bash run out of memory.
 
Old 09-10-2009, 05:44 PM   #20
karlatLQ
Member
 
Registered: Sep 2009
Posts: 67

Rep: Reputation: 19
Move up one directory, and it should work:

Quote:
find . -iname *.jpg -print


The escape character does help as does the '{}' instead of {}.
 
Old 09-10-2009, 07:16 PM   #21
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by i92guboj View Post
'04*2009/*.jpg' I guess, if I understand what you meant.
link under my main folder, i would have the following subfolders as an example
04birthday2009
05birthday2009
04kids2009

and i'd only want to do it for jpg's under the first and third folder since they start with 04 then have 2009 in them. i dont care what the subfolder names are below those.


and now i'm confused again, not surprised, by the last two posts. so am i doing it wrong then? none of my file names should have spaces in them, but i'd rather have this account for them i suppose, in case i use this for something in the future.
 
Old 09-10-2009, 10:53 PM   #22
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Without escaping the asterisk, it is expanded in the shell before the command is run. That isn't what you want at all.
 
Old 09-11-2009, 05:36 AM   #23
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by rjo98 View Post
and now i'm confused again, not surprised, by the last two posts. so am i doing it wrong then? none of my file names should have spaces in them, but i'd rather have this account for them i suppose, in case i use this for something in the future.
If you are talking about the -print0/-0, yes, you should use those as well, they never hurt. I should have used them in first place.

My examples will work almost always for a reason, even with spaces: because xargs is using -i and the {} are correctly quoted. However, even then, there's *one* corner case where they will not work, and the null termination will.

It's the rare case when a name of the file has a carry return on it (yes, in linux you can do that).

Code:
$ touch a.jpg b.jpg "foo bar.jpg" "moo
cow.jpg"
$ ls
a.jpg  b.jpg  foo bar.jpg  moo?cow.jpg
$ find . -iname "*.jpg" | xargs -i ls -l '{}'
ls: cannot access ./moo: No such file or directory
ls: cannot access cow.jpg: No such file or directory
-rw-r--r-- 1 i92guboj i92guboj 0 Sep 11 12:33 ./b.jpg
-rw-r--r-- 1 i92guboj i92guboj 0 Sep 11 12:33 ./a.jpg
-rw-r--r-- 1 i92guboj i92guboj 0 Sep 11 12:33 ./foo bar.jpg
$ find . -iname "*.jpg" -print0 | xargs -0 ls -l     
-rw-r--r-- 1 i92guboj i92guboj 0 Sep 11 12:33 ./a.jpg
-rw-r--r-- 1 i92guboj i92guboj 0 Sep 11 12:33 ./b.jpg
-rw-r--r-- 1 i92guboj i92guboj 0 Sep 11 12:33 ./foo bar.jpg
-rw-r--r-- 1 i92guboj i92guboj 0 Sep 11 12:33 ./moo?cow.jpg
 
Old 09-11-2009, 07:36 AM   #24
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
so I should really be doing the following then?

find . -type f -iname \*.jpg -print0 | xargs -0 -i mv '{}' /new/path
 
Old 09-11-2009, 07:49 AM   #25
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
That seems ok
 
Old 09-11-2009, 07:51 AM   #26
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
and the {} is ok? or should it be '{}' ?
 
Old 09-11-2009, 08:59 AM   #27
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
so if i want to do it for a group of folders like i said in a couple posts ago, i would put this?? is that the right slash or do i need single quotes around the one part?

find . -type f -iname 04*2009\*.jpg -print0 | xargs -0 -i mv '{}' /new/path
 
Old 09-11-2009, 09:31 AM   #28
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by rjo98 View Post
so if i want to do it for a group of folders like i said in a couple posts ago, i would put this?? is that the right slash or do i need single quotes around the one part?

find . -type f -iname 04*2009\*.jpg -print0 | xargs -0 -i mv '{}' /new/path
You need to escape both asterisks with slashes. If in doubt, just use single quotes around the whole thing:

Code:
-iname '04*2009*.jpg'
# or
-iname 04\*2009\*.jpg
 
Old 09-11-2009, 09:43 AM   #29
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
the quotes make more sense to me than slashes, so just to summarize since this is a real long thread.

To move only .jpg .JPG etc files beneath where i'm running the command from to /new/path

find . -type f -iname \*.jpg -print0 | xargs -0 -i mv '{}' /new/path

to only do it in folders (and all their subfolders) who are one level beneath where i'm running this command from, only when the folder names (one level down from where i'm running the command from) begin with 04 then have some text then end in 2009

find . -type f -iname '04*2009*.jpg' -print0 | xargs -0 -i mv '{}' /new/path


it almost looks like this last one is just going to get the jpg's that have 04 then some text then 2009 then some text then .jpg or .JPG etc in the name, rather than looking at the top folder they are in's name.
 
Old 09-11-2009, 10:57 AM   #30
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Yeah, I just tried to do the find part from that second statement. it doesn't work right, it looks in folders with the wrong name.


say i have /photos
then
/photos/04birthday2009
/photos/05birthday2009
/photos/04kids2009
Then under each one of those three are 10 subfolders with random names

What I want to be able to do is move them based on the 04 and the 2009 in that 2nd level directory structure. So i want to somehow use this command to move only jpg's under (whether in that folder or levels down from it) the two 04*2009 named folders.

hope that explanation is clearer.
 
  


Reply



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
copy recursively across directories stefaandk *BSD 1 11-09-2008 10:01 PM
how to copy images recursively via bash mindfriction Programming 6 12-13-2007 10:25 PM
Using cp to copy directory permissions recursively? SirTristan Linux - Newbie 3 11-25-2007 08:36 AM
trying to recursively copy jpegs DJOtaku Linux - General 10 08-26-2005 01:09 PM
recursively copy php.ini to www directories osio Programming 3 07-01-2005 04:24 AM

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

All times are GMT -5. The time now is 08:43 PM.

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
Open Source Consulting | Domain Registration