?
Solved

shell script to make dirs and move files

Posted on 2000-05-15
4
Medium Priority
?
212 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
[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
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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.
Suggested Courses

765 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