简体   繁体   English

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

[英]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.

相关问题 如果没有,请执行Shell脚本mkdir和mv文件 - Shell Script mkdir if not and mv files to each day Shell脚本 - 查找今天修改的文件,创建目录并将其移动到那里 - Shell script - Find files modified today, create directory, and move them there 每次在Linux上运行Shell脚本时都会创建一个新目录 - creating a new directory each time a shell script is run on linux 目录中每个文件的Linux Shell脚本获取文件名并执行程序 - Linux Shell Script For Each File in a Directory Grab the filename and execute a program Shell脚本-将文件移动到文件夹 - Shell script - move files into folder 创建 Unix shell 脚本以将非空文件从源目录移动到目标目录并为其添加时间戳 - Create Unix shell script to move non empty files from Source directory to Target directory and add timestamp to them 每次我更改bashrc目录时列出所有文件的脚本? - Script to list all files each time I change directory for bashrc? 在同一目录中移动多个文件,而无需每次都输入目录名(Linux) - Move multiple files in same directory without typing directory name each time (Linux) 将文件从一个目录移动到另一个目录,并添加到新目录中的每个文件名 - Move files from one dir to another and add to each files name in the new directory Shell脚本,它将当前目录中的所有可执行文件移动到单独的文件夹中 - shell script which will move all executable files in the current directory to a seperate folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM