[英]Shell Script, move each 2 files to each directory
I have 10 files from 1.txt to 10.txt and 5 folders abcde我有 10 个文件从 1.txt 到 10.txt 和 5 个文件夹 abcde
I want to move my files in this way :我想以这种方式移动我的文件:
1.txt 2.txt to a
3.txt 4.txt to b
..
..
9.txt 10.txt to e
How Can I achieve this?我怎样才能做到这一点?
Here is the commands to do it.这是执行此操作的命令。
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
The hard to read part is taking each file, and then moving it to the directory with the half that index.难以阅读的部分是获取每个文件,然后将其移动到具有该索引一半的目录。 Since bash math rounds down and ignores remainders, this will give us index 0, 0, 1, 1, and so forth.
由于 bash 数学四舍五入并忽略余数,这将为我们提供索引 0、0、1、1 等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.