我有从.txt到10.txt的10个文件和5个文件夹a b c d e
我想以这种方式移动我的文件:
1.txt 2.txt to a
3.txt 4.txt to b
..
..
9.txt 10.txt to e
我如何实现这一目标?
答案 0 :(得分:0)
这是执行此操作的命令。
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等等。