开发者

Bash: Moving multiple files into subfolders

I have a folder with a couple thousand files and I want to move t开发者_如何学Gohem 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?


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


is this work as your requirement?

mv *run*.html dir1


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜