问题
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