LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   separate out files based on name (https://www.linuxquestions.org/questions/programming-9/separate-out-files-based-on-name-872372/)

anon091 04-01-2011 08:05 AM

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?

colucix 04-01-2011 08:09 AM

Are they all in the Pictures main directory or are they placed in sub-directories?

anon091 04-01-2011 08:14 AM

sub-directories, and lots of them

colucix 04-01-2011 08:20 AM

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.

kurumi 04-01-2011 08:21 AM

Code:

mv *a.jpg /destination

Nominal Animal 04-01-2011 08:24 AM

Quote:

Originally Posted by rjo98 (Post 4310668)
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/{}' ';'


anon091 04-01-2011 08:26 AM

cool, thanks guys. I'll try out Nominal's suggestions, I never would have been able to come up with that myself haha.

anon091 04-08-2011 01:21 PM

I tried the folder structure one, and it says "missing argument to '-exec' "

colucix 04-08-2011 03:38 PM

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.

anon091 04-08-2011 03:41 PM

Thanks. How do I get it to run all those lines at once, put them in a .sh file then run that?

anon091 04-08-2011 03:43 PM

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...

Ramurd 04-08-2011 03:44 PM

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.

colucix 04-08-2011 03:47 PM

Quote:

Originally Posted by rjo98 (Post 4318409)
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.

colucix 04-08-2011 03:50 PM

Quote:

Originally Posted by rjo98 (Post 4318411)
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.

anon091 04-08-2011 03:57 PM

getting somewhere with ramurd's suggestion...

now i get

mkdir: invalid option -- i


All times are GMT -5. The time now is 07:24 AM.