简体   繁体   English

无论文件夹深度或数量如何,复制或移动目录中的所有文件

[英]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.

相关问题 如何移动/复制目录中的很多文件(不是所有文件)? - How to move/copy a lot of files (not all files) in a directory? 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 将指定文件夹中的所有文件移到一个目录中 - Move all files in specified folder up one directory 移动和删除文件夹中所有与grep匹配的文件 - Move and delete all files matching grep in folder Shell脚本,它将当前目录中的所有可执行文件移动到单独的文件夹中 - shell script which will move all executable files in the current directory to a seperate folder 如何对目录中的所有文件执行命令,并将每个文件的输出移至新文件夹? - How do I conduct a command on all the files in a directory and move the output for each file to a new folder? 如何对目录中的所有文件进行tar压缩并将该tar移至另一个目录 - how to tar all files in a directory and move that tar to another directory 如何在列表中查找包含使用部分名称的目录和子目录中的所有文件,然后将它们复制到新文件夹 - How to find all files in a directory and subdirectories that contain using partial names within a list then copy them to a new folder 消除子文件夹,将所有文件移动到一个文件夹中 - Eliminating subfolders to move all files into one folder 从第 n 级子文件夹复制或移动到另一个目录或文件夹 - Copy or move from nth level subfolder to another directory or folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM