0

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

subslin
  • 1
  • 1
  • What did you try? Share your code please – Steephen May 09 '17 at 13:39
  • 1
    When posting to Stack Overflow you should also post your attempt at the problem. You can read more about it [here][1]. Your question will be easier and quicker to answer when we have a base script to work off of. [1]: http://stackoverflow.com/help/mcve – ShaneNal May 09 '17 at 13:43
  • I am sorry guys. I should have shared what i did. In fact all i did was try multiple solutions presented here: https://stackoverflow.com/questions/22668866/linux-bash-script-to-create-folder-and-move-files http://stackoverflow.com/questions/19633452/create-multiple-folders-using-existing-file-names-in-linux https://stackoverflow.com/questions/22668866/linux-bash-script-to-create-folder-and-move-files I also updated the first question to better explain what i was trying to do. – subslin May 13 '17 at 09:55

2 Answers2

2

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.

grundic
  • 4,641
  • 3
  • 31
  • 47
0

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
  • Yes i am sorry. In detail they 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. – subslin May 13 '17 at 09:50