Scripts
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb Site ManagementScripts

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 24th, 2006, 04:02 PM
dotancohen's Avatar
dotancohen dotancohen is offline
המבין יבין
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2001
Location: Haifa
Posts: 2,011 dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 7 m 49 sec
Reputation Power: 1086
Send a message via ICQ to dotancohen Send a message via AIM to dotancohen Send a message via MSN to dotancohen Send a message via Yahoo to dotancohen
Checking for duplicate file names

First off, this is a repost of a post of mine in the linux forum yesterday. I didn't realize that there is a seperate scripts forum. Sorry for the noise.

I need to check a bunch of recursive directories for duplicate file names. I plan on moving them all into one large directory, and don't want to overwrite a file by accident. Is there a way I can check this in bash? I don't know any bash, but I know enough C to print out Hello World. Better yet, is ther a linux command that does this?

Thanks, all.
__________________
. . . What is Firefox?
. . . . . . What is Linux?
. . . . . . . . . . . What is Love?

Reply With Quote
  #2  
Old April 24th, 2006, 07:30 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 814 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 8 m 11 sec
Reputation Power: 243
Interesting problem - without putting too much thought into it (i.e., this is probably a crap solution!) how about something like:

Code:
find /base/dir/path -type f -exec ls -1 {} \; > /a/temp/file

for f in `cat /a/temp/file`
do
  fn=`basename $f`
  echo "$fn:$f" >> /another/temp/file
done
sort -k1 -t: -u /another/temp.file > /yet/another/tempfile
diff /another/temp/file /yet/another/tempfile


Basically - get a list of files (with full (relative) path and save it.
Process that file and get the filename alone and put that, and the full path into another file, which you then sort on the 1st field, asking for the output to be unique (i.e., no duplicates). Diff thte two files and any differences SHOULD be any duplicate filenames. That, at least, is the theory!
Comments on this post
dotancohen agrees: thanks

Reply With Quote
  #3  
Old April 24th, 2006, 07:37 PM
dotancohen's Avatar
dotancohen dotancohen is offline
המבין יבין
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2001
Location: Haifa
Posts: 2,011 dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 7 m 49 sec
Reputation Power: 1086
Send a message via ICQ to dotancohen Send a message via AIM to dotancohen Send a message via MSN to dotancohen Send a message via Yahoo to dotancohen
Thanks. I can follow it a bit, but I've a long way to go before I write something like that. I'll whip up a test directory and try it. Thank you.

Reply With Quote
  #4  
Old April 24th, 2006, 07:47 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 814 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 8 m 11 sec
Reputation Power: 243
*blinks at the rapid reply* Wow, I didn;t see you looking over my shoulder .... and .. hey get OUT of my bedroom!!!!!

Good luck with it, let me know if you have any issues - apart from the temp file names I tried to get the syntax as correct as possible but memory is not my ... er, what was I saying ...? You may need to 'quote' the colon in the sort command - sort -k2 -t\:

Edit: I made the assumptions:
a) you have a songle base dir under which all the subdir and files are found. If there is not an easy or obvious base dir you can repeat the directory search: find /fred/charlie/files /george/bill/files- type f ....
b) that no file names will have a space character in them.

Last edited by Ehlanna : April 24th, 2006 at 07:50 PM. Reason: additional info

Reply With Quote
  #5  
Old April 24th, 2006, 08:33 PM
dotancohen's Avatar
dotancohen dotancohen is offline
המבין יבין
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2001
Location: Haifa
Posts: 2,011 dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 7 m 49 sec
Reputation Power: 1086
Send a message via ICQ to dotancohen Send a message via AIM to dotancohen Send a message via MSN to dotancohen Send a message via Yahoo to dotancohen
Quote:
Originally Posted by Ehlanna
that no file names will have a space character in them.


Oh, that's a bad assumption. Some of those files are wife-named and eliminating spaces disrupts the delicate linux-wife balance I work so hard to maintain. What would I have to quote in order to make this spouse-proof? (only I will run it, but the spaces must stay )

Reply With Quote
  #6  
Old April 25th, 2006, 12:12 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 814 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 8 m 11 sec
Reputation Power: 243
Ok a bit more awake now ...

Let's take a gander at this and try to remember *shudder* spaces in names ...

Code:
# Set up some temp files
TF1=/tmp/filelist.txt
TF2=/tmp/filesort.txt
TF3=/tmp/filediff.txt

# List files, extract the name from the path, sort and output to file
find /base/dir -type f -exec ls -1 {} \; | awk -F\/ '{printf("\"%s\"\n",$(NF))}' | sort > $TF1
# Sort, removing duplicates
sort -u $TF1 > $TF2
# compare files, any diffs are duplicates
diff $TF1 $TF2 | grep "^< " | cut -c3- > $TF3
#We now have a list of any duplicate filenames, 'framed' in double quotes
Comments on this post
dotancohen agrees: I wish that I had tons or rep to pour on you...

Reply With Quote
  #7  
Old April 25th, 2006, 01:54 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 814 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 8 m 11 sec
Reputation Power: 243
Quote:
Originally Posted by dotancohen
tons or rep


LMAO, I hope you meant "of" there!!! If not, it depends on what the tons are of ...

More serioulsy, I just hope it works - it is a little cack-handed, and I am sure theer are more elegant ways to go: probably chucking an xargs -I {} in somewhere.

Reply With Quote
  #8  
Old April 25th, 2006, 02:34 PM
dotancohen's Avatar
dotancohen dotancohen is offline
המבין יבין
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2001
Location: Haifa
Posts: 2,011 dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level)dotancohen User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 7 m 49 sec
Reputation Power: 1086
Send a message via ICQ to dotancohen Send a message via AIM to dotancohen Send a message via MSN to dotancohen Send a message via Yahoo to dotancohen
Oh yeah, my fingers don't always say what I'd like them too. Well, neither does my mouth for that matter...

I'm going to have to play with this a little later. The more that I want to do something, the more attention I must give to other matters...

But thank you very much. I hate asking people to do my work for me, but bash scripting is just something that I can't seem to find the time to learn properly. Know that I do learn from your example and that I am not so lazy!

Thank you.

Reply With Quote
  #9  
Old April 25th, 2006, 05:09 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 814 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 8 m 11 sec
Reputation Power: 243
I am pleased to hear that you, and I hope others, are learning - I think I must have the genes of a teacher lurking somewhere and I enjoy passing on knowledge (as well as acquiring it).

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb Site ManagementScripts > Checking for duplicate file names


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |