Thursday, May 2, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2315  / 3 Years ago, sat, july 31, 2021, 11:56:39

I have a folder composed of M several sub-folders, each of them containing some text files and N images (*.png)



This is what the tree looks like:



/parent/
/sub-folder1/
/data1.dat
/data2.dat
/image1.png
/image2.png
...
/imageN.png
/sub-folder2/
/data1.dat
/data2.dat
/image1.png
/image2.png
...
/imageN.png
...
/sub-folderM/
/data1.dat
/data2.dat
/image1.png
/image2.png
...
/imageN.png


notice that all images in each sub-folder are named the same (ie: image1.png, ..., imageN.png)



What I need is to move only the images into a new parent folder (say parent2), while replicating the sub-folder structure. After the moving is done the new parent folder should look like this:



/parent2/
/sub-folder1/
/image1.png
/image2.png
...
/imageN.png
/sub-folder2/
/image1.png
/image2.png
...
/imageN.png
...
/sub-folderM/
/image1.png
/image2.png
...
/imageN.png


(ie: only images and respecting the same sub-folders structure)



and the original parent folder should look like:



/parent/
/sub-folder1/
/data1.dat
/data2.dat
/sub-folder2/
/data1.dat
/data2.dat
...
/sub-folderM/
/data1.dat
/data2.dat


(ie: images moved out)



I've seen some examples of scripts that can move all filed into a new folder (Shell script to move all files from subfolders to parent folder) or some that can move only images (Script to move pictures) but I haven't found one that would do so while respecting the sub-folders tree.


More From » command-line

 Answers
1

You can try using rsync:



rsync -av --include="*/"  --include='*.png' --exclude='*' parent1 parent2


this creates directory parent2 and copies all files with .png extension with subdirectory structure to it.

explanation




  • -v verbose to see whats copied

  • -a archive mode (copy subdirectories with same ownership, permissions etc.)

  • --include '*/' --include='*.png' include .png ending files first part is to create subdirectories

  • --exclude='*' exclude all other files
    for more info see rsync man page


[#26006] Sunday, August 1, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rialhirt

Total Points: 422
Total Questions: 113
Total Answers: 120

Location: France
Member since Sun, May 15, 2022
2 Years ago
;