LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2010, 12:16 PM   #1
bikbjrt
LQ Newbie
 
Registered: Jun 2010
Posts: 1

Rep: Reputation: 0
Question 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.
 
Old 06-29-2010, 12:41 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 25,640

Rep: Reputation: 7650Reputation: 7650Reputation: 7650Reputation: 7650Reputation: 7650Reputation: 7650Reputation: 7650Reputation: 7650Reputation: 7650Reputation: 7650Reputation: 7650
Quote:
Originally Posted by bikbjrt View Post
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.
 
Old 07-01-2010, 12:25 AM   #3
simon.sweetman
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 22
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
 
  


Reply

Tags
recursive, script, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script required to get the mail after files are uploaded to ftp siddabathuni Programming 12 10-01-2009 08:08 AM
Shell script assistance required.. TheEngineer Linux - Newbie 4 10-26-2006 06:42 AM
shell script help required syntax error sridhar11 Programming 1 10-31-2005 08:38 AM
small shell script required demigor Programming 6 08-08-2005 04:38 PM
shell script provides an error that a binary operator is required max_rsr Linux - General 1 03-12-2005 08:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration