简体   繁体   中英

Bash script that sorts files into folder with same name

So i've been trying to pull this together for sometime. Used different methods, and also found some partial answers, but none of the answers i found here worked or only answered part of the problem, or similar problems..

I have a folder multiple files with this structure:

nameXX.tar.gz
md5nameXX.txt
debugnameXX.txt

nameYY.tar.gz
md5nameYY.txt
debugnameYY.txt

Etc...

And so on, as you can see the only portion of the file names that changes is the XX, YY etc

What i need is to create a script that will move all files that match the XX, YY etc.. into a folder with the same name.

So for example i would like to move all the,

nameXX.tar.gz ,
md5nameXX.txt, 
debugnameXX.txt
All those would move into a folder named nameXX

In detail those are backup snapshots. Each backup snapshot creates 3 files. buckup-TIMESTAMP.tar.gz ; md5backup-TIMESTAMP.tXT; debugbackup-TIMESTAMP.txt. All i need is a script that moves all 3 files in the same folder named backup-TIMESTAMP. In resume the YY or XX represent different timestamps.

Thanks for your help

I didn't made complex check, but at least this would let you to start from something:

for name in `find . -name "*.tar.gz" | xargs sh -c 'basename $1 .tar.gz' dummy`
do 
    echo "Processing $name" 
    mkdir $name 
    find . -name "$name*" -type f -exec mv {} $name \; 
done

This code assumes you have files .tar.gz, creates folder by it's name and moves all files starting from to that folder.

Not really enough information to work off here, is XX and YY the same every time? if so you could probably do each one in a one liner mv command

mv *YY.* /folderYY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM