Solved

shell script to make dirs and move files

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.:

746 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

11 Experts available now in Live!

Get 1:1 Help Now