Author Topic: Sorting with a bash script  (Read 941 times)

Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Sorting with a bash script
« on: June 14, 2012, 08:54:14 PM »
Hello, long time!

So my membership to a Joomla template club is nearing it's time to run out, I have long sense abandon Joomla for Drupal. However, I started downloading ALL the templates they ever made for the icons and in case anyone I know ever needs a template.


Cutting to the chase, I have a folder full of files like this: rt_camber.zip and rt_camber_j15.tgz (_j15 are 1.5 versions). With each version are varies additional files like rt_camber-extensions, I would like to sort them all into folders like this:

Camber
-2.5
--rt_camber-extensions.zip
--rt_camber-extensions.zip
-1.5
--rt_camber_j15.tgz
--rt_camber_j15-extensions.zip


Sure I can copy past them all into the correct folders, but I think this is a both a good learning experience and good to know how to do this. Right now I have about 16 different templates, with 2 different version (2.5 and 1.5 each) and I haven't even downloaded half of them yet.

~Thanks for any help!

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
Re: Sorting with a bash script
« Reply #1 on: June 15, 2012, 12:26:53 AM »
Only 1.5 and 2.5 versions? And all files start with rt_? And no files like rt_camber.lol_j15.tgz?
« Last Edit: June 15, 2012, 12:29:27 AM by sir_herrbatka »

Offline djohnston

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 6227
  • I don't do Windows
Re: Sorting with a bash script
« Reply #2 on: June 15, 2012, 12:30:10 AM »
This will create extension name folders and move each file, by its extension, to the appropriately named folder. You will have to supply the file extensions you want sorted. I used gz and bz2.

#!/bin/sh
EXTS="gz bz2"
for ext in $EXTS
do
    mkdir $ext
    mv *.${ext} $ext
done


Bare metal                           VBox
AMD Athlon 7750 Dual-Core    Single core
4GiB RAM                              1GiB RAM
nVidia GeForce FX 5200          64MB video
LXDE 32bit                            KDE 64bit

Registered Linux User #416378

Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: Sorting with a bash script
« Reply #3 on: June 15, 2012, 10:37:58 AM »
This will create extension name folders and move each file, by its extension, to the appropriately named folder. You will have to supply the file extensions you want sorted. I used gz and bz2.

#!/bin/sh
EXTS="gz bz2"
for ext in $EXTS
do
    mkdir $ext
    mv *.${ext} $ext
done




The problem is only the regular template in Joomla 1.5 is tgz, all the others are zip. So it will only sort out the Joomla 1.5 template, not all the additional stuff like extensions. Is there anyway to have it rad the file name? So anything containing "j15" can be moved to dir j15 and everything else to dir j25? I would still need to sort the files into folders with the name of their templates, but it's 1 step closer.

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
Re: Sorting with a bash script
« Reply #4 on: June 15, 2012, 10:55:41 AM »
mkdir j15
mkdir j25

for file in *j15*; do mv $file ./j15; done
mv * ./j25

more fancy variant would need some kind of if statment checking if the file name has j15 in it's name.

PS
Something like this

for file in *; do
test=`echo * | grep j15`
if [[ "$test" = " " ]]; then mv $file ./j25
else mv $file ./15
fi
« Last Edit: June 16, 2012, 07:30:19 AM by sir_herrbatka »

Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: Sorting with a bash script
« Reply #5 on: June 18, 2012, 02:50:28 PM »
mkdir j15
mkdir j25

for file in *j15*; do mv $file ./j15; done
mv * ./j25

more fancy variant would need some kind of if statment checking if the file name has j15 in it's name.

PS
Something like this

for file in *; do
test=`echo * | grep j15`
if [[ "$test" = " " ]]; then mv $file ./j25
else mv $file ./15
fi


I figured I could use grep to sort by version, but I'm still trying to figure out how to do it for each template name without manually entering each one. I haven't really played around with it yet, because I'm busy. Is there a way I can specifically grab one part of a name? All tempalte files begin with rt_thetempaltename, so if I can identify this part in the script I could sort this way.

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
Re: Sorting with a bash script
« Reply #6 on: June 19, 2012, 12:39:46 AM »
Quote
Only 1.5 and 2.5 versions? And all files start with rt_? And no files like rt_camber.lol_j15.tgz?

;-)

We will do it this way... If there are files named in different manner It won't work.

for file in *; do
test=`echo $file | cut -d '_' -f 2-2 | cut -d '-' -f 1-1`
mkdir -p ./$test
mv $file ./$test/$file
done
« Last Edit: June 19, 2012, 07:42:06 AM by sir_herrbatka »

Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: Sorting with a bash script
« Reply #7 on: June 20, 2012, 05:12:04 PM »
Quote
Only 1.5 and 2.5 versions? And all files start with rt_? And no files like rt_camber.lol_j15.tgz?

;-)

We will do it this way... If there are files named in different manner It won't work.

for file in *; do
test=`echo $file | cut -d '_' -f 2-2 | cut -d '-' -f 1-1`
mkdir -p ./$test
mv $file ./$test/$file
done

I still haven't had time to test this, but do you mind explaining what that dose? I know I'm a pain, but I want to make sure I learn form this. Thanks!

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
Re: Sorting with a bash script
« Reply #8 on: June 21, 2012, 03:05:57 AM »
sure

We start loop with for. This time on all files so for file in *; do
now it's time to get "camber" part so echo $file gets file from our for loop, then we do some cutting.

cut cut's part of text. -d set's delimiter -f fields we want to get so for example...

echo lol.bash.cut
lol.bash.cut

echo lol.bash.cut | cut -d '.' -f -1
lol

echo lol.bash.cut | cut -d '.' -f 2-3
bash.cut

echo rt_camber-extensions.zip | cut -d '_' -f 2-2 | cut -d '-' -f 1-1
camber
echo rt_camber_j15.zip | cut -d '_' -f 2-2 | cut -d '-' -f 1-1
camber
echo rt_camber_j15-extensions.zip | cut -d '_' -f 2-2 | cut -d '-' -f 1-1
camber

first we cut with delimiter _ then with -

We get camber, and create a new string called test by test=`echo $file | cut -d '_' -f 2-2 | cut -d '-' -f 1-1`

now we create dir with mkdir -p. -p option forces to create directory only if it's not there.

mkdir -p ./$test should create in this example directory ./camber if it not exist
mv $file moves file that is proceeded  to correct dir.

done closes loop

So
Create loop for all files by using *
  check the name
  create correct directory if it does not exist
  move file to correct directory
  next plz!
awesome!








Offline djohnston

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 6227
  • I don't do Windows
Re: Sorting with a bash script
« Reply #9 on: June 21, 2012, 03:12:20 AM »

  next plz!
awesome!


 ;D  Very detailed and informative explanation.

Bare metal                           VBox
AMD Athlon 7750 Dual-Core    Single core
4GiB RAM                              1GiB RAM
nVidia GeForce FX 5200          64MB video
LXDE 32bit                            KDE 64bit

Registered Linux User #416378

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
Re: Sorting with a bash script
« Reply #10 on: June 21, 2012, 03:14:43 AM »
It explain how for loop works ;-)

Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: Sorting with a bash script
« Reply #11 on: July 01, 2012, 03:25:16 PM »
sure

We start loop with for. This time on all files so for file in *; do
now it's time to get "camber" part so echo $file gets file from our for loop, then we do some cutting.

cut cut's part of text. -d set's delimiter -f fields we want to get so for example...

echo lol.bash.cut
lol.bash.cut

echo lol.bash.cut | cut -d '.' -f -1
lol

echo lol.bash.cut | cut -d '.' -f 2-3
bash.cut

echo rt_camber-extensions.zip | cut -d '_' -f 2-2 | cut -d '-' -f 1-1
camber
echo rt_camber_j15.zip | cut -d '_' -f 2-2 | cut -d '-' -f 1-1
camber
echo rt_camber_j15-extensions.zip | cut -d '_' -f 2-2 | cut -d '-' -f 1-1
camber

first we cut with delimiter _ then with -

We get camber, and create a new string called test by test=`echo $file | cut -d '_' -f 2-2 | cut -d '-' -f 1-1`

now we create dir with mkdir -p. -p option forces to create directory only if it's not there.

mkdir -p ./$test should create in this example directory ./camber if it not exist
mv $file moves file that is proceeded  to correct dir.

done closes loop

So
Create loop for all files by using *
  check the name
  create correct directory if it does not exist
  move file to correct directory
  next plz!
awesome!









Very well done, this will be going into my collection of tutorials for sure!

Offline sir_herrbatka

  • Full Member
  • ***
  • Posts: 238
Re: Sorting with a bash script
« Reply #12 on: July 02, 2012, 01:16:32 AM »
HTH