LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2011, 08:05 AM   #1
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Rep: Reputation: 49
separate out files based on name


I have a folder named Pictures that contains a bunch of .jpg files. My problem is that they all have randomly numbered names, then there is a duplicate of the file that is random numbers then the letter a right before the .jpg.

for example, there would be 123.jpg and 123a.jpg, where 123a.jpg is just a resized version of 123.

What i'd like to do but have NO clue how to, is to have a script or something go through my Pictures folder, then copy the ones that end in a.jpg to a folder called Resized, and ones that dont have that to a folder called Originals. That way my Pictures folder will be in tact, and i'll have copies of them all separated out.

I have to do this all through the CLI on a machine, maybe I dont even need a script and can just do it with a slick command?
 
Old 04-01-2011, 08:09 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Are they all in the Pictures main directory or are they placed in sub-directories?
 
Old 04-01-2011, 08:14 AM   #3
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
sub-directories, and lots of them
 
Old 04-01-2011, 08:20 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hence you need a command to search recursively inside the Pictures folder. You can try find, using the -regex option to match file names with numbers and w/ or w/o the trailing a. The -exec action might serve to move them. On the other hand, if you want to preserve the original directory structure inside Resized and Originals, you may want to write a more elaborate script.
 
1 members found this post helpful.
Old 04-01-2011, 08:21 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
mv *a.jpg /destination
 
Old 04-01-2011, 08:24 AM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by rjo98 View Post
copy the ones that end in a.jpg to a folder called Resized, and ones that dont have that to a folder called Originals.
If you want them copied to the folder and not duplicate the folder structure,
Code:
find Pictures/ -name '*a.jpg' -exec cp -vi -- '{}' Resized/ ';'
find Pictures/ -name '*[^a].jpg' -exec cp -vi -- '{}' Originals/ ';'
If you want to duplicate the folder structure too, then
Code:
cd Pictures/
find ./ -type d -exec mkdir -p -- '../Resized/{}' '../Originals/{}' ';'
find ./ -name '*a.jpg' -exec cp -vi -- '{}' '../Resized/{}' ';'
find ./ -name '*[^a].jpg' -exec cp -vi -- '{}' '../Originals/{}' ';'

Last edited by Nominal Animal; 04-08-2011 at 06:38 PM. Reason: Fixed the folder structure duplication commands.
 
2 members found this post helpful.
Old 04-01-2011, 08:26 AM   #7
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
cool, thanks guys. I'll try out Nominal's suggestions, I never would have been able to come up with that myself haha.
 
Old 04-08-2011, 01:21 PM   #8
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
I tried the folder structure one, and it says "missing argument to '-exec' "
 
Old 04-08-2011, 03:38 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
At this point keep it simple and do a loop to achieve your task step-by-step and to have a better control on what happens. For example, suppose you have the following directories:
Code:
/home/rjo98/Originals
/home/rjo98/Pictures
/home/rjo98/Resized
first move the re-sized pictures:
Code:
while read src
do
  dst=${src/Pictures/Resized}
  echo mkdir -p $(dirname $dst)
  echo mv $src $dst
done < <(find /home/rjo98/Pictures -name \*a.jpg)
then the original ones:
Code:
while read src
do
  dst=${src/Pictures/Originals}
  echo mkdir -p $(dirname $dst)
  echo mv $src $dst
done < <(find /home/rjo98/Pictures -name \*.jpg)
The echo statements are for testing purposes. Once you've verified the resulting commands are what you're looking for, remove the echo and run again. Please, note that the find command in the first loop searches the *a.jpg files, the second one searches all the remaining jpg. Hope this helps.
 
Old 04-08-2011, 03:41 PM   #10
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
Thanks. How do I get it to run all those lines at once, put them in a .sh file then run that?
 
Old 04-08-2011, 03:43 PM   #11
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
also, i was hoping to preserve the Pictures folder by just doing copies, in case something screwed up, but i guess that's why we echo first...
 
Old 04-08-2011, 03:44 PM   #12
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Quote:
I tried the folder structure one, and it says "missing argument to '-exec' "
I think that is because the && are caught by the shell and thus not to find; try escaping them ( replace them to \&\& )

Edit: the thread lived, and had to put in the quote to show what I was referring to.

Last edited by Ramurd; 04-08-2011 at 03:46 PM.
 
Old 04-08-2011, 03:47 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by rjo98 View Post
Thanks. How do I get it to run all those lines at once, put them in a .sh file then run that?
Just copy/paste them on the command line, modify the path in the find command according to the actual path of the Pictures directory and press enter. Beware of not removing the echo statements before testing.
 
Old 04-08-2011, 03:50 PM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by rjo98 View Post
also, i was hoping to preserve the Pictures folder by just doing copies, in case something screwed up, but i guess that's why we echo first...
That's right. Anyway, you might substitute the mv command with cp, since the syntax in this case is the same.
 
Old 04-08-2011, 03:57 PM   #15
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
getting somewhere with ramurd's suggestion...

now i get

mkdir: invalid option -- i
 
  


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
how to link two separate files nagananda Linux - Newbie 3 07-30-2009 04:05 AM
Parsing input into separate files bboyz Linux - Newbie 7 05-07-2008 02:08 AM
Encoding separate audio channels to separate files omnio Linux - Software 0 06-01-2007 07:46 AM
Looking for a tool to auto crop and separate images in to separate files.. mlsfit Linux - Software 2 08-06-2006 03:13 PM
Fortran and separate module files Neruocomp Programming 4 04-14-2005 08:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:40 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
Open Source Consulting | Domain Registration