[英]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. 我检查了
cp
并find
命令,但无法正常运行。
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: 然后将以下条件层叠应用于找到的每个项目:
-name '*.txt'
will filter out only files that have the .txt
extension -name '*.txt'
将仅过滤扩展名为.txt
文件 -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
结尾的目录) -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. 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: 然后应用以下内容:
-type d
this time filters out only directories -type d
这次仅过滤出目录 -empty
filters out only those that are empty -empty
仅过滤那些空的 -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.