1

I have a list of jpeg files in a directory like this (below is just a sample, like the structure is the same but this there are many fo0# sub-folders, not only fo01 and fo02, where each of these subfolders has many .jpeg images, not only two):

.
`-- main
    |-- ccc
    |   |-- fo01
    |   |   |-- ccc_fo01_c_000.jpeg
    |   |   `-- ccc_fo01_c_001.jpeg
    |   `-- fo02
    |       |-- ccc_fo02_c_000.jpeg
    |       `-- ccc_fo02_c_001.jpeg
    |-- ddd
    |   |-- fo01
    |   |   |-- ddd_fo01_d_000.jpeg
    |   |   `-- ddd_fo01_d_001.jpeg
    |   `-- fo02
    |       |-- ddd_fo02_d_000.jpeg
    |       `-- ddd_fo02_d_001.jpeg
    `-- lll
        |-- fo01
        |   |-- lll_fo01_l_000.jpeg
        |   `-- lll_fo01_l_001.jpeg
        `-- fo02
            |-- lll_fo02_l_000.jpeg
            `-- lll_fo02_l_001.jpeg

What I want to do is to loop on the main directory on each of ccc, ddd and lll, so that in the each iteration of the loop the jpeg images of one of the subfolders will be copied to the parent location. So in the above example, in the first iteration of the loop this will happens:

  • ccc_fo01_c_000.jpeg and ccc_fo01_c_001.jpeg will be copied(not moved) from ccc/fo01 to ccc
  • ddd_fo01_d_000.jpeg and ddd_fo01_d_001.jpeg will be copied(not moved) from ddd/fo01 to ddd
  • lll_fo01_l_000.jpeg and lll_fo01_l_001.jpeg will be copied(not moved) from lll/fo01 to lll

and in the end of the first iteration, those copied jpegs (in ccc ddd lll) will be deleted from ccc ddd lll so that as if I didn't make the copying.

Same with every iteration on the subfolders. So this means that in the above example, the copying then deleting process will happen two times as there are two subfloders fo01 and fo02. But in my case there are more subfolders. So if anyone could please advise how this can be done.

1

1 Answer 1

4

Here is a suggestion on how to do it:

First you iterate over the folders, inside every folder you search and find the files and copy them to its parent folder.

#!/bin/bash

#Iterate over the folders    
for folder in main/*
do
    #Copy the files into the folder
    find $folder -name "*.jpeg" -type f -exec cp {} $folder \; -print
done

#After its done: delete the originals
find main -mindepth 3 -name "*.jpeg" -type f -delete

I just wrote a quick find to delete the files at the end. You could also do this from within the for loop.

6
  • I've tried it but it's not working
    – Tak
    Mar 13, 2015 at 7:25
  • Could you give me a hint as to what went wrong ? You have to run it from the base folder (the one above main).
    – stalet
    Mar 13, 2015 at 7:27
  • this is what happens bash: /main/ccc: Is a directory find: ``/etc/cups/ssl': Permission denied find: /etc/ssl/private': Permission denied then it kept sending this error message in almost every image in my machine `cp: cannot create regular file /home/9278015b02fa780418ef3a5791572af9.jpeg': Permission denied
    – Tak
    Mar 13, 2015 at 7:32
  • That makes no sense, the for loop uses main/*. Are you sure you didnt add a space separator or something in between the for folder in main/*. This error could have been caused if you had like for folder in main /* with a space in between.
    – stalet
    Mar 13, 2015 at 7:38
  • this is what I did location= /media/a/main/ccc then for folder in $location/*
    – Tak
    Mar 13, 2015 at 7:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .