Link to home
Start Free TrialLog in
Avatar of jamoraquai
jamoraquai

asked on

Moving directories for archiving purposes

Hi experts!

I'm quite new to unix and its shell scripting. Anyway, I need to create an sh script that
moves directories and its file contents to the archive directory.

Example:

Before archive:

SourceFolder
  Subfolder1
     revAfolder
     revBfolder
     revCfolder
 Subfolder2
     revAfolder
     revBfolder

ArchiveFolder

After archive:

SourceFolder
  Subfolder1
     revCfolder
 Subfolder2
     revBfolder

ArchiveFolder
   Subfolder1
     revAfolder
     revBfolder
   Subfolder2
     revAfolder
 
Notice that after archiving, the directories for old revisions are moved to the
archive folder.

Hope anyone can help me with this. =D
Avatar of yuzh
yuzh

you can do:

cd /path-to/Subfolder1
mv Subfolder[12] /path-to/ArchiveFolder

man mv
to learn more details

ASKER CERTIFIED SOLUTION
Avatar of Gns
Gns

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here's a tested version

#!/bin/sh

SOURCEDIR=/path/to/source
ARCHIVEDIR=/path/to/archive

for dir in $SOURCEDIR/Subfolder*
do
  for subdir in `ls -rd $dir/rev*|sed 1d`
  do
    [ -d $ARCHIVEDIR/$dir ] || mkdir $ARCHIVEDIR/$dir
    mv $subdir $ARCHIVEDIR/$dir
  done
done
Avatar of jamoraquai

ASKER

Thanks for the quick response guys. will try these scripts out! =)
Hi Glen,

I thinks i should use the `ls -t` instead of `ls -rt` because I need to exclude the latest folder from being moved to
the archive folder. Am I correct? just a confirmation. I noticed that this block
       if [ "x$M" == "x" ]
        then
          M="$j"
        else
          mv $j $ARCHIVE/$i
        fi
is excluding the first folder from being moved or archived and when you use the `ls -rt` the first folder is the oldest folder.
therefore excluding the old folder from being archived instead of the latest being excluded.

Thanks!
I thought you were going off the subdirs, alphabetically.

Anyhow, easy to change to date based:

#!/bin/sh

SOURCEDIR=/path/to/source
ARCHIVEDIR=/path/to/archive

for dir in $SOURCEDIR/Subfolder*
do
  for subdir in `ls -rtd $dir/rev*|sed 1d`
  do
    [ -d $ARCHIVEDIR/$dir ] || mkdir $ARCHIVEDIR/$dir
    mv $subdir $ARCHIVEDIR/$dir
  done
done
Sorry for the change. anyway, I found out that there were different names for revision directories so i had to rely on the last modified field.
I'm currently testing Glenn's script and I'll try yours Tintin if Glenn's does not work. Thanks man!
> I thinks i should use the `ls -t` instead of `ls -rt` because I need to exclude the latest folder from being moved to
> the archive folder. Am I correct? just a confirmation. I noticed that this block
>       if [ "x$M" == "x" ]
>        then
>          M="$j"
>        else
>          mv $j $ARCHIVE/$i
>        fi
> is excluding the first folder from being moved or archived and when you use the `ls -rt` the first folder is the oldest
> folder.
> therefore excluding the old folder from being archived instead of the latest being excluded.
Do the ls with -lt and -lrt and you'll see if the way I do it is the right way to do it;-).On many many unices the ls -t will list from oldest to newest, so to have a safe way (that aren't susceptible to arbitrary limits) of excluding the first directory (and note that I go to some pains to ensure that it really is a directory)... Well, having said that, one can note that Solaris implementation of ls (of course) does it the other way around... So in that case you would have to remove the "r" as you suggest.

-- Glenn
Oh, and there is the complete difference between Tintins suggestion and mine... I'll only move directories (with arbitrary names), while his will move any file or directory named rev<something>... But that was perhaps obvious:-).

-- Glenn
Thanks Glenn for all the help! =D Appreciate it man. Thank you also Tintin!
Hope to hear from you guys again. Ciao!
> "On many many unices the ls -t "
On not so many... Must have been smoking something interresting there:-).

-- Glenn