[英]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.假设我有一个名为 Pictures 的文件夹,我想从该文件夹中移动或复制所有文件。
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但是,我也想移动和收集子文件夹中的所有文件: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我想将所有这些文件移动或复制到另一个文件夹,如结果,所以我得到: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 ?如何在 bash/shell 脚本中完成此操作? 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).
我也很欣赏使其文件类型中性,以便从它们的目录中收集任何文件(不仅仅是像我的示例中那样的 .png),而且我不知道文件名是什么(我只使用了 1...4,因为我没有知道如何命名它们)。
You can simply copy
all files and subdirectories along with their contents using cp's recursive
option:您可以使用 cp 的
recursive
选项简单地copy
所有文件和子目录及其内容:
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.但是,递归
moving
它们有点棘手,您需要创建子目录的tar
文件并移动它们,然后untar
目标路径中的 tar 文件。 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
另一种选择是使用
xargs
find /absolute/path/to/Pictures -name '*.png' | xargs -I files mv files /absolute/path/to/results
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.