LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script for recursive delete required. (http://www.linuxquestions.org/questions/linux-newbie-8/shell-script-for-recursive-delete-required-817009/)

bikbjrt 06-29-2010 12:16 PM

Shell script for recursive delete required.
 
is there a recursive shell or Perl script to delete files with the same name as the parent folder? i wish to include the starting folder name as argument to the script.

TB0ne 06-29-2010 12:41 PM

Quote:

Originally Posted by bikbjrt (Post 4018420)
is there a recursive shell or Perl script to delete files with the same name as the parent folder? i wish to include the starting folder name as argument to the script.

No, but it should be easy to write. Lots of shell script and Perl programming tutorials on Google. Post what you've written, and where you're having problems, and we can help.

simon.sweetman 07-01-2010 12:25 AM

This script uses find to search all sub-directories from the specified directory. Any file with the same name as it's parent directory is removed.

Note all the quotes are needed to deal with directories/files with spaces in their name.

Code:

#!/bin/bash

if [ $# -ne 1 -o ! -d "$1" ]
then
    echo "Usage $0 dirname"
    exit 1
fi
 
find "$1" -type f -print | while read file
do
  dir=$(dirname "$file")
  dname=$(basename "$dir")
  fname=$(basename "$file")
  [ "$dname" = "$fname" ] && rm "$file"
done



All times are GMT -5. The time now is 07:40 PM.