Bash: Moving multiple files into subfolders

一曲冷凌霜 提交于 2019-12-11 10:24:36

问题


I have a folder with a couple thousand files and I want to move them into subfolders according to a string in the filename. The files all have a structure like

something-run1_001.txt

something-run22_1243.txt

So I tried the following script in order to move all files with "run1" in it into a subfolder r1 and all "run22" files in a subfolder r22 (and so on) but it does no work that way and I get a message "File X is the same as file X".

#!bin/bash

for i in {1..39}
do
foldername=r$i
#echo "$foldername"
mkdir $foldername
find . -type f -name "*run$i_*" | xargs -i mv {} $foldername/ 
done

How to solve this?


回答1:


for i in {1..39}
do
  mkdir -p r${i}/
  mv *run${i}_* r${i}/
done



回答2:


is this work as your requirement?

mv *run*.html dir1




回答3:


If you still run into the "too many arguments" trap you can pipe find into a while loop

#!/bin/bash -u
find . -maxdepth 1 -name '*-run*_*.txt' |
{
    while read FNAME
    do
        N=${FNAME##*-run}
        N=${N%_*}
        DIR=r$N
        test -d $DIR || mkdir $DIR
        mv $FNAME $DIR/.
    done
}


来源:https://stackoverflow.com/questions/5882316/bash-moving-multiple-files-into-subfolders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!