繁体   English   中英

Shell Script,将每 2 个文件移动到每个目录

[英]Shell Script, move each 2 files to each directory

我有 10 个文件从 1.txt 到 10.txt 和 5 个文件夹 abcde

我想以这种方式移动我的文件:

1.txt 2.txt to a
3.txt 4.txt to b
..
..
9.txt 10.txt to e

我怎样才能做到这一点?

这是执行此操作的命令。

count=0    # initialize the counter
declare -a files=(*.txt)     # put all the txt files in an array
# just print directories, and strip the unneeded /. in the returned string
declare -a dirs=($(ls -d */.|sed 's;/.;;')

# run through the array, moving each file
while [[ $count -le ${#files[@]} ]]; do
    mv ${files[$count]} ${dirs[$(($count/2))]}  
    ((++count))    # very important to increment the counters
done

难以阅读的部分是获取每个文件,然后将其移动到具有该索引一半的目录。 由于 bash 数学四舍五入并忽略余数,这将为我们提供索引 0、0、1、1 等。

暂无
暂无

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

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