Shell Script To Delete Files In The First Directory Which Are Similarly Named In The Second Directory
Posted in Decision Making » File-management
A shell script to compare files in two directory and delete files in the first directory which are similarly named in the second directory. Following script uses for loop to compare files in a each directory.
#!/bin/bash # Write a shell script that accepts two directory names as arguments and # deletes those files in the first directory which are similarly named in # the second directory. SRC="$1" DST="$2" if [ $# -ne 2 ] then echo "$(basename $0) dir1 dir2" exit 1 fi if [ ! -d $SRC ] then echo "Directory $SRC does not exists!" exit 2 fi if [ ! -d $DST ] then echo "Directory $DST does not exists!" exit 2 fi for f in $DST/* do #echo Processing $f if [ -f $f ] then tFile="$SRC/$(basename $f)" if [ -f $tFile ] then echo -n "Deleting $tFile..." /bin/rm $tFile [ $? -eq 0 ] && echo "done" || echo "failed" fi fi done
Download - Email this to a friend - Printable version
Is your site working? Monitor Your Web Site 24/7. Get SMS alerts on server downtime! Free 30-day trial including 20 SMS!
Related Other Helpful Shell Scripts:
- Shell script to find the number of files present in the current directory without using WC command
- Shell Script To Count Number Of Files In Each Subdirectories
- Shell script to read 3 numbers and find the greaters of the three
- Shell Script to find out whether file has read, write and execute permission
- Shell script to copy all files recursively and upload them to remote FTP server
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: directory names, directory structure, exit status, for command, for loop, if command, loop method, search directory, shell loops ~ Last updated on: July 15, 2008

