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
===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
ASKER
The source directory does have sub-directories. Also requesting the destination to be parametrized.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
worked perfectly ..thanks a lot ..!!!!
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 !
test1/test2/test3/abc.txt instead of just abc.txt
Sorry about the confusion !
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
do
if ! grep -q $(basename $file) $STOP; then echo rm $file
else echo cp -r $file $DEST
fi
done
ASKER
Tried the above command its still just copying the files without the directory structure.
Thank you
Thank you
ASKER
Also its not deleting files which have a space in their name.
eg: abc cde.xls
eg: abc cde.xls
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
do
if ! grep -q "$(basename "$file")" $STOP; then echo rm "$file"
else echo cp -r --parents "$file" $DEST
fi
done
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.