Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bash script help needed 1

Status
Not open for further replies.

thedaver

IS-IT--Management
Joined
Jul 12, 2001
Messages
2,741
Location
US
I want to delete the first instance of a duplicated filename...

I use this bash scriptlet to produce a list of files that have duplicate file names

find ./ | sed s#.*/##g | sort | grep mp3 | uniq -d > duplicate_files

The file "duplicate_files" contains the list.

I need to read this file, perform another "find -iname [filename]" and then delete the first record (probably using "head -1") - which should delete the first found instance of a duplicated file.

Help appreciated.

 
Something like this?

Code:
cat names | while read name
do 
    print rm $( find ~ -name $file -print | head -1 )
done

And that is *not* a useless use of a cat...
 
OK, with this code originating the list,
Code:
find ./ | sed s#.*/##g | sort | grep txt | uniq -d  > duplicate_files

and this code processing the list

Code:
cat duplicate_files | while read name
do
# Remove the echo on the next line
    echo rm -f  \"$( find -iname "$name" -print | tail -1 )\"
done

I was able to delete the last found instance of a duplicate file.

BIG WARNING!!! This will keep killing instances of the found files until you have none if you keep running the second script.

Thanks Eric!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top