Solved

Moving directories for archiving purposes

Posted on 2004-09-14
11
326 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:jamoraquai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 12052172
you can do:

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

man mv
to learn more details

0
 
LVL 20

Accepted Solution

by:
Gns earned 150 total points
ID: 12053783
I think jamoraquai is looking for something a bit less ... "pathbound"... and a bit more flexible Greg. Something that archives the "rev*" directories so that only the newest (lexically or ... modification-time-wise) remain. So perhaps something like
#!/bin/sh
SOURCE=/path/to/SourceFolder
ARCHIVE=/path/to/ArchiveFolder

cd $SOURCE
for i in *
do
  if [ -d $i ]
  then
    cd $i
    if [ -d $ARCHIVE/$i ]
    then
      :
    else
      mkdir $ARCHIVE/$i
    fi
    M=""
########### use the next line if you want it lexicaly
#    for j in `ls -r`
########### else use the next to look at last modification time
    for j in `ls -rt`
    do
      if [ -d $j ]
      then
        if [ "x$M" == "x" ]
        then
          M="$j"
        else
          mv $j $ARCHIVE/$i
        fi
      fi
    done
  fi
done
# End of script

Caveat is that I've not tested the above, and it checks virtually no errors... But the gist of it should be good:-).

-- Glenn
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12059619
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jamoraquai
ID: 12060574
Thanks for the quick response guys. will try these scripts out! =)
0
 

Author Comment

by:jamoraquai
ID: 12060889
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!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12060907
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
0
 

Author Comment

by:jamoraquai
ID: 12060941
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!
0
 
LVL 20

Expert Comment

by:Gns
ID: 12062240
> 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
0
 
LVL 20

Expert Comment

by:Gns
ID: 12062259
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
0
 

Author Comment

by:jamoraquai
ID: 12062550
Thanks Glenn for all the help! =D Appreciate it man. Thank you also Tintin!
Hope to hear from you guys again. Ciao!
0
 
LVL 20

Expert Comment

by:Gns
ID: 12062635
> "On many many unices the ls -t "
On not so many... Must have been smoking something interresting there:-).

-- Glenn
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question