Avatar of gaurav sharma
gaurav sharma

asked on 

Shell script to parse and delete files

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
LinuxShell Scripting

Avatar of undefined
Last Comment
woolmilkporc
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

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.
Avatar of gaurav sharma
gaurav sharma

ASKER

The source directory does have sub-directories. Also requesting the destination to be parametrized.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of gaurav sharma

ASKER

worked perfectly ..thanks a lot ..!!!!
Avatar of gaurav sharma

ASKER

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 !
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

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
Avatar of gaurav sharma

ASKER

Tried the above command its still just copying the files without the directory structure.

Thank you
Avatar of gaurav sharma

ASKER

Also its not deleting files which have a  space in their name.  

eg: abc cde.xls
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

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
Linux
Linux

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo