I have a parent directory with thousands of directories within sub-directories and so on. In some
of the sub-directories I have few image files.
I want to copy (rsync
is preferred) all of the bmp
and jpg
files from the sub-directories to a single new directory without any sub-directories. But at the same time I want to append the name of the LAST directory where the file comes from to the copied file.
├── example1
│ ├── fdfd
│ │ ├── db_files
│ │ │ ├── asdasd.zip
│ │ │ ├── dfdfsta.zip
│ │ │ ├── csf_4545.zip
│ │ │ ├── 45455.zip
│ │ │ ├── 4544.zip
│ │ │ ├── 45545.zip
│ │ │ ├── gfdfgd.zip
│ │ │ ├── retert.zip
│ │ │ ├── vcxvxc.zip
│ │ │ ├── vcxcvcx.zip
│ │ │ ├── asdr3.zip
│ │ │ ├── tetst.zip
│ │ │ ├── testeh.bmp
│ │ │ └── testst43.zip
│ │ ├── TEST_FILE.bmp
│ │ └── hfexport.csv
│ └── testFOLDER2018-05
│ ├── Databasef.txt
│ ├── Folder_Backup_2014-01-05.7z
│ ├── full_Export.pdf
│ ├── f-to-7000.csv
│ ├── f-to-505.csv
│ ├── f-to-600.csv
│ ├── f-to-960.bmp
│ ├── g-to-1000.jpg
│ └── 7000.csv
├── example3
│ ├── img_copy
│ │ ├── automation
│ │ │ ├── f.sh
│ │ │ ├── fff.sh
│ │ │ └── test.bat
│ │ ├── all.bmp
│ │ ├── h23.txt
│ │ ├── old2_copy.jpg
In the example above, the copied files will be as follows:
db_files_testeh.bmp
fdfd_TEST_FILE.bmp
testFOLDER2018-05_f-to-960.bmp
testFOLDER2018-05_g-to-1000.jpg
img_copy_all.bmp
img_copy_old2_copy.jpg
How can I achieve this?
I use the following commands to copy only bmp
and jpg
files to a new directory.
find -regex '.*\.\(bmp\|jpg\)' | tee /home/tmp/matches.txt
while read -r LINE; do rsync -vh --append-verify --chmod=ugo=rwX --progress --stats "$LINE" /mnt/storage3/backups/NEW_DIR/ONLY_IMAGES; done < /home/tmp/matches.txt