简体   繁体   English

Linux递归复制文件到其父文件夹

[英]Linux recursive copy files to its parent folder

I want to copy recursively files to its parent folder for a specific file extension. 我想将递归文件复制到其父文件夹以获取特定的文件扩展名。 For example: 例如:

./folderA/folder1/*.txt to ./folderA/*.txt  
./folderB/folder2/*.txt to ./folderB/*.txt

etc. 等等

I checked cp and find commands but couldn't get it working. 我检查了cpfind命令,但无法正常运行。

I suspect that while you say copy , you actually mean to move the files up to their respective parent directories. 我怀疑当您说copy时 ,您实际上是在文件移动到其各自的父目录中。 It can be done easily using find : 使用find可以轻松完成:

$ find . -name '*.txt' -type f -execdir mv -n '{}' ../ \;

The above command recurses into the current directory . 上面的命令递归到当前目录. and then applies the following cascade of conditionals to each item found: 然后将以下条件层叠应用于找到的每个项目:

  1. -name '*.txt' will filter out only files that have the .txt extension -name '*.txt'将仅过滤扩展名为.txt文件
  2. -type f will filter out only regular files (eg, not directories that – for whatever reason – happen to have a name ending in .txt ) -type f将仅过滤掉常规文件(例如,不过滤由于某种原因而碰巧以.txt结尾的目录)
  3. -execdir mv -n '{}' ../ \\; executes the command mv -n '{}' ../ in the containing directory where the {} is a placeholder for the matched file's name and the single quotes are needed to stop the shell from interpreting the curly braces. 在包含目录中执行命令mv -n '{}' ../ 其中{}是匹配文件名的占位符,并且需要单引号以阻止外壳程序解释花括号。 The ; ; terminates the command and again has to be escaped from the shell interpreting it. 终止命令,并且必须再次从解释它的shell中转义。

I have passed the -n flag to the mv program to avoid accidentally overwriting an existing file. 我已将-n标志传递给mv程序,以避免意外覆盖现有文件。

The above command will transform the following file system tree 上面的命令将转换以下文件系统树

dir1/
    dir11/
        file3.txt
        file4.txt
    dir12/
    file2.txt
dir2/
    dir21/
        file6.dat
    dir22/
        dir221/
        dir221/file8.txt
        file7.txt
    file5.txt
dir3/
    file9.dat
file1.txt

into this one: 进入这个:

dir1/
    dir11/
    dir12/
    file3.txt
    file4.txt
dir2/
    dir21/
        file6.dat
    dir22/
        dir221/
        file8.txt
    file7.txt
dir3/
    file9.dat
file2.txt
file5.txt

To get rid of the empty directories, run 要摆脱空目录,请运行

$ find . -type d -empty -delete

Again, this command will traverse the current directory . 同样,此命令将遍历当前目录. and then apply the following: 然后应用以下内容:

  1. -type d this time filters out only directories -type d这次仅过滤出目录
  2. -empty filters out only those that are empty -empty仅过滤那些空的
  3. -delete deletes them. -delete删除它们。

Fine print: -execdir is not specified by POSIX, though major implementations (at least the GNU and BSD one) support it. 精细打印: POSIX未指定-execdir ,尽管主要实现(至少GNU和BSD都支持)。 If you need strict POSIX compliance, you'll have to make do with the less safe -exec which would need additional thought to be applied correctly in this case. 如果您需要严格遵守POSIX,则必须使用不太安全的-exec ,在这种情况下,还需要考虑其他一些才能正确应用。

Finally, please try your commands in a test directory with dummy files, not your actual data. 最后,请在包含伪文件而不是实际数据的测试目录中尝试您的命令。 Especially with the -delete option of find , you can loose all your data quicker than you might imaging. 特别是使用find-delete选项,您可以比想象的更快地释放所有数据。 Read the man page and, if that is not enough, the reference manual of find . 阅读手册页 ,如果还不够,请阅读find参考手册 Never blindly copy shell commands from random strangers posted on the internet if you don't understand them. 如果您不理解,则不要盲目复制互联网上发布的随机陌生人的shell命令。

$cp ./folderA/folder1/*.txt ./folderA

试试这个命令

Run something like this from the root(ish) directory: 从根目录运行以下内容:

#! /bin/bash
BASE_DIR=./

new_dir() {
LOC_DIR=`pwd`
for i in "${LOC_DIR}"/*; do
    [[ -f "${i}" ]] && cp "${i}" ../
    [[ -d "${i}" ]] && cd "${i}" && new_dir
cd ..
done
return 0
}

new_dir

This will search each directory. 这将搜索每个目录。 When a file is encountered, it copies the file up a directory. 遇到文件时,它将文件复制到目录中。 When a directory is found, it will move down into the directory and start the process over again. 找到目录后,它将下移到该目录中,然后重新开始该过程。 I think it'll work for you. 我认为它将为您工作。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linux - 如何从每个文件夹中递归复制 N 个文件并保持相同的文件夹结构 - Linux - How to copy recursive from each folder N files and keep same folder structure linux如何将不同文件夹下的同名文件批量复制重命名到单独的文件夹,文件重命名为父文件夹? - How do batch copy and rename files with same names under different folders to a separate folder with files renamed to their parent folder in linux? 复制Linux中不包含某些文件夹的文件 - Copy files excluding some folder in linux linux递归复制指定的文件不起作用 - linux recursive copy specified files doesn't work 在 Unix/Linux 中将特定文件从一个文件夹复制到另一个文件夹 - Copy specific files from one folder to another in Unix/Linux 如何在父目录linux的子目录中查找和复制文件 - how to find and copy files in a sub directory from parent directory linux 如何将特定文件复制到特定文件夹linux bash - How to copy specific files into specific folder linux bash 从Linux中包含文件夹名称的多个文件夹复制文件 - Copy files from multiple folders with including folder name in Linux 复制给定列表中目标文件夹中存在的Linux文件 - copy files in linux which are existing at destination folder from the given list 如何在Linux中根据名称在具有相应名称的文件夹中移动文件? - How to move files in Linux based on its name in a folder with a corresponding name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM