Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to find all files that have the same filename (excluding the file extension) that occur 3 times. I also need the full path to the file.

What I have currently is

#get file without extension
alias lse="ls -1R | sed -e 's/\.[a-zA-Z]*$//'"
#print out the current dir and get files occuring 3 times
lse | sed "s;^;`pwd`/;"  | sort | uniq -c | grep " 3 "

This runs howver pwd prints the folder I ran the command from not the path to the file.

So I tried find

find . -type f | sed "s#^.#$(pwd)#" | sort | uniq -c

This runs but includes the file extension. When I try to add sed -e 's/\.[a-zA-Z]*$//'" I get errors as I am not sure how to combine the two sed commands and I cannot seem to pipe a second time to sed?

so what I am trying to do is

find . -type f | sed "s#^.#$(pwd)#" | sed -e 's/\.[a-zA-Z]*$//'"| sort | uniq -c | grep " 3 "

but this does not run.

share|improve this question
up vote 2 down vote accepted

what's the extra " in the second sed command doing there?

 /tmp/test $ touch foo.bar foo.baz foo.foo
 /tmp/test $ find . -type f | sed "s#^.#$(pwd)#" | sed -e 's/\.[a-zA-Z]*$//'| sort | uniq -c | grep " 3 "
 3 /tmp/test/foo
share|improve this answer
    
thanks, I cannot belive I did not notice that – Daveo Apr 11 '12 at 5:48
1  
Note that grep " 3 " can cause issues with files like ` 3 .txt` (space before and after 3). You can replace it with awk '{if ($1 == 3) {print}}' (but check what the awk default separator for your system is) – lupincho Apr 11 '12 at 5:50

Alternate but similar way using some other concepts:

find . -type f -printf '%f\n' | sed 's/\.[[:alpha:]]*$//' |\
sort | uniq -c | awk '$1==3'

Uses:

  • find's -printf to directly strip the leading directories.
  • the [:alpha:] character class to define letters. This emulates your example, but does not take into account file endings with numbers (e.g. .mp3). Change to [:alnum:] to match letters AND numbers. Look for more info on character classes to see other possibilities.
  • awk to only print the lines with count 3 (easily changed to count>=3 which seems more logical). Since print is the default action, this can be accomplished using only awk '$1==3'.
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .