Solved

Shell script to parse and delete files

Posted on 2014-12-24
9
215 Views
Last Modified: 2015-01-06
Have two dir's Dir_A and Dir_B

===Directory A has the below files

ora/spp/abc.sps
ora/sql/acbef.sql
ora/tyb/ty/file_3.ty

====Text file(list.txt) has something like (name of files in Dir_A)

abc.sps
file_3.ty

Shell Script that will delete all of the files in Dir_A except the ones in the list.txt

Parameters to the shell script

Parameter 1: Path to list.txt file with list of file names
Parameter 2: Path to top level directory(Dir_A) where the files exist.


Call to the script :  

Test_parse.sh path/to/Dir_A path/to/list.txt


The above script should delete all of the scripts in Dir_A except the ones in the list.txt and

Copy the all the Contents to Dir_B
0
Comment
Question by:gaurav sharma
  • 5
  • 4
9 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
if [ -d $1 ]; then DIR=$1
   else echo "Wrong directory"
   exit
fi
if [ -f $2 ]; then STOP=$2
   else echo "Wrong Control File"
   exit
fi
for file in $DIR/*
   do
      if ! grep -q $(basename $file) $STOP; then echo rm $file
       else echo cp $file Dir_B
      fi
   done

Please note that "Dir_B" is hard coded (not passed as an argument because you didn't request this).

Note further that I put "echo" in front of "rm" and "cp" so you can test the outcome.
Remove both "echo" statements for the "real" processing to occur.

The source directory should not contain any subdirectories to avoid error messages.
If there are such subdirs please let me know.

Finally, please note that the script does preliminary checks for existence of the source directory and the control file.
0
 

Author Comment

by:gaurav sharma
Comment Utility
The source directory does have sub-directories. Also requesting the destination to be parametrized.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
if [ -d $1 ]; then DIR=$1
   else echo "Wrong Source Directory $1"
   exit
fi
if [ -f $2 ]; then STOP=$2
   else echo "Wrong Control File $2"
   exit
fi
if [ -d $3 ]; then DEST=$3
   else echo "Wrong Destination Directory $3"
   exit
fi
for file in $(find $DIR -type f -maxdepth 1)
   do
      if ! grep -q $(basename $file) $STOP; then echo rm $file
       else echo cp $file $DEST
      fi
   done

Test_parse.sh /path/to/Dir_A /path/to/list.txt /path/to/Dir_B
0
 

Author Comment

by:gaurav sharma
Comment Utility
worked perfectly ..thanks a lot ..!!!!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:gaurav sharma
Comment Utility
Small correction is needed. Only the files in the list.txt are getting copied to Dir_B. The directory structure is not preserved. For example: if the text file has abc.txt      as one of the entry and the file abc.txt is in a directory under test1/test2/test3/abc.txt. When this file is copied to Dir_B can it be copied with the directory structure.

test1/test2/test3/abc.txt    instead of just abc.txt


Sorry about the confusion !
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
for file in $(find $DIR -type f)
   do
      if ! grep -q $(basename $file) $STOP; then echo rm $file
       else echo cp -r $file $DEST
      fi
   done
0
 

Author Comment

by:gaurav sharma
Comment Utility
Tried the above command its still just copying the files without the directory structure.

Thank you
0
 

Author Comment

by:gaurav sharma
Comment Utility
Also its not deleting files which have a  space in their name.  

eg: abc cde.xls
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
find $DIR -type f | while read file
   do
      if ! grep -q "$(basename "$file")" $STOP; then echo rm "$file"
       else echo cp -r --parents "$file" $DEST
      fi
   done
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

9 Experts available now in Live!

Get 1:1 Help Now