简体   繁体   English

将多个文件移动到多个目录

[英]Move multiple files to multiple to multiple directories

I have 5 files called file1.txt, file2.txt ... file5.txt and I would like to move each one into a respective directory called dir1, dir2 ... dir5. 我有5个名为file1.txt,file2.txt ... file5.txt的文件,我想将每个文件移动到一个名为dir1,dir2 ... dir5的相应目录中。

So file1.txt is moved into dir1, file2.txt is moved into dir2 and so on. 所以file1.txt移动到dir1,file2.txt移动到dir2,依此类推。

Is there a way to do this in one line at the command line, using mv and xargs perhaps? 有没有办法在命令行中使用mvxargs在一行中执行此操作?

I'm only suggesting xargs because I quite like this answer provided by Robert Gamble to a question asking how to copy one file to multiple directories. 我只是建议xargs ,因为我很喜欢这个答案由罗伯特宝洁提供的一个问题问如何一个文件复制到多个目录。

echo dir1 dir2 dir3 | xargs -n 1 cp file1

I would personally prefer a solution that relies on a for loop, eg: 我个人更喜欢依赖于for循环的解决方案,例如:

for n in {1..5}; do echo mv -- "file$n.txt" "dir$n/"; done
#                   ^^^^ remove that

This can be done with xargs but I find the solution to be less elegant: 这可以用xargs完成,但我发现解决方案不那么优雅:

seq 1 5 | xargs -n1 -I{} echo mv -- "file{}.txt" "dir{}/"
#                        ^^^^ remove that

你可以做的另一种方法,如果它不一定是连续整数的列表,并且dir*不一定已经存在。

for f in *.txt; do mkdir dir${f: -5:1}; mv $f dir${f: -5:1}/; done

使用GNU Parallel你会这样:

parallel mv {} {=s/file/dir/=} ::: *.txt

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

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