Solved

shell script to make dirs and move files

Posted on 2000-05-15
4
203 Views
Last Modified: 2010-04-21
I have a little program that generates different reports based on dates....and each file is tagged with a date and it stores the files in different directories.....

eg it generates these files...

attendance-02-03-2000.txt
attendance-03-04-2000.txt
attendance-04-05-2000.txt

student-marks-02-03-2000.txt
student-marks-04-05-2000.txt

etc...

and puts the files to corresponding directories....
eg all attendance records in a directory called attandance and student marks in a directory called student-marks...

you can assume that there will be any number of directories with different names....

I need a little shell script...that goes to each directory...and checks the date tag of each file....then creates a directory if it does not exist in the root level (ie top) directory...and moves the file with that date tag to the date directory....

in the example above we should get 3 date directories...

02-03-2000

03-04-2000

04-05-2000


then in the directory
02-03-2000 we should move these file
attendance-02-03-2000.txt
student-marks-02-03-2000.txt


in directory 03-04-2000 we should move
attendance-03-04-2000.txt

and in directory 04-05-2000 we should move
attendance-04-05-2000.txt
student-marks-04-05-2000.txt


any ideas/suggestions....
thanks
0
Comment
Question by:mabo
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 50 total points
ID: 2812822
I believe what follows will do what you want. Put it into a file and make that file executable. To use it cd to the directory containing the collections of directories which contain the files and execute the script on each directory.

#!/bin/sh
#
# For the directory specified as the argument, look for file names like
# *-dd-dd-dddd*. For each such file found move that file to a directory
# named dd-dd-dddd at in the current directory (creating the dd-dd-dddd
# directory if needed).
#

if [ $# -ne 1 ]; then
  echo "Usage: $0 directory"
  exit 1
elif [ ! -d $1 ]; then
  echo "Usage: $0 directory"
  exit 1
fi

cd $1

for file in *; do
  if [ -f $file ]; then
    DATE=`echo $file | sed -e "s/.*-\([0-9]\+-[0-9]\+-[0-9]\+\).*/\1/"`
    if [ ! -d ../$DATE ]; then
      mkdir ../$DATE
    fi
    mv $file ../$DATE
  fi
done
0
 

Author Comment

by:mabo
ID: 2813815
Thanks for your response...

But is there a way to make "cd" to each directory "automatic"...ie. it should automatically search all directories...ie I don't want to "execute the script on each directory"....I want this process to be automatic as well.....

any suggestions....
0
 
LVL 12

Expert Comment

by:geotiger
ID: 2814770
Perl script might help.

#!/usr/local/bin/perl
$dir = "/your/root/dir"; # dir contains all the files

opendir DIR, $dir or die "Could not open dir - $dir:$!\n";
@a = grep !/^\.\.?$/, readdir DIR;
close DIR
foreach (@a) {
  if ($_ =~ /.*-(\d+-\d+-\d+)\.txt/) {
      $D{$1} .= " $_";
  }
}
chdir $dir;
foreach $i (sort keys %D) {
    mkdir $i;
    foreach $j (split / /, $D{$i}) {
        next if (!$j);
        link   $j $i/$j;  # copy the file to dir $i
        unlink $j;     # remove file $j
    }
}

0
 
LVL 40

Expert Comment

by:jlevie
ID: 2814896
Yes, but there'd need to be some way to recognize the directories that are to be considered as sources as opposed to those that are repositories of the collated data. In your question it sounds like there could be a number of them and the only thing that I could assume was that the would not have names of the form dd-dd-dddd. For instance if I know that all of the source directories would start with an alpha character, I could easily modify the script to process those. Or in a bourne/bash/ksh shell I could do:

for dir in [a-z]*; do do-dir $dir; done

I considered putting something like that in the script, but thought that it wasn't necessarily a valid assumption.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now