Solved

shell script to make dirs and move files

Posted on 2000-05-15
4
205 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.

777 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