Solved

shell script to make dirs and move files

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
sed -- search an replace multiple pattern 11 101
Java core in Solaris 10 1 312
Virtualizing TAPE on dual VIOS 3 117
SCO 5.0.7 on HP Proliant DL380 G5 will not shutdown properly. 6 54
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…
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

733 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