简体   繁体   中英

Copy or move all files in a directory regardles of folder depth or number

Lets say i have a folder named Pictures and I want to move or copy all files out of this folder.

However I also want to move and harvest all of the files who are in sub folders so: Pictures/1.png Pictures/yolo/2.png Pictures/yolo/swag/sand/3.png Pictures/extra/fire/4.png

I want to move or copy all these files to another folder like results so I get: results/1.png results/2.png results/3.png results/4.png

Only I have no idea in advance what sub folders will be in the Pictures folder.

How can I accomplish this in bash/shell scripts ? I also appreciate making it file type neutral so any files are harvested from their directories (not only .png like in my example) and I have no idea what the file name will be (I only used 1...4 because i did not have any idea how to name them).

You can simply copy all files and subdirectories along with their contents using cp's recursive option:

cp -pr <source_path>/* <destination_path>/

But, moving them recursively is a bit tricky, you will need to create tar files of the subdirectories and move them and then untar the tar files in destination path. As this is a complex process, as a workaround, you can copy the files/directories recursively and then delete the files from original path.

cp -pr <source_path>/* <destination_path>/ && rm -rf <source_path>/*

You can do it like this:

find /absolute/path/to/Pictures -type f -name '*.png' -exec mv -i {} /absolute/path/to/results  \;

Another option is to use xargs

find /absolute/path/to/Pictures -name '*.png' | xargs -I files mv files /absolute/path/to/results

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM