Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This command:

find ./ -name "mybinaryrpm-1.0.24-1.1.i586.rpm"

gives the output:

./DirectoryName/SubDirectory/mybinaryrpm-1.0.24-1.1.i586.rpm

but I only want the output:

mybinaryrpm-1.0.24-1.1.i586.rpm

How can I get this?


What I am really trying to do:

  1. Read the input file that contains a line separated list of binary file names
  2. Search for files with corresponding names within current directory recursively
  3. If more than one match is found, then consider only one search result and copy the file to the corresponding destination directory.

Current code:

while read line; do
    binaryRpm=$(find ./ -name "$line" -exec basename {} \;)
    echo " [ Read From File: $line ] <==> [ Find Command Searched: $binaryRpm ]"
    ## If found then copy the binary file to dest dir
    ## TODO: If Find Command finds more than One File with the
    ## Same Name, then ONLY ONE FILE MUST be Compared and copied to
    ## the Release Directory
    if [ "$line" != "$binaryRpm" ]; then
        echo "---------- Binary File Searching---------- "
        continue
    else
        echo " !!!!!! Binary File Found ==> $binaryRpm !!!!!! "
    fi
done < "$FILENAME"
share|improve this question
3  
You want to extract exactly the same string you give an input? –  Matteo May 5 '12 at 10:43
    
-1 So tired of misleading questions... –  Daniel Beck May 12 '12 at 14:54

3 Answers 3

Well, your example does not really make sense since you, as noted in a comment, want to get the same output as you give as input. If you know the input, you already know the output.

But, in general, use find's -printf function:

find . -name "mybinaryrpm-1.0.24-1.1.i586.rpm" -printf '%f\n'

From man find, section -printf:

%f    File's name with any leading directories removed (only the last element).

This is only available for GNU find, but the GNU tools are the standard on Linux which you asked for. It is also installable on more or less every relevant platform.


A minimal code example that does what it seems like you are really trying to do:

#!/bin/sh
FILENAME="/path/to/file/with/filenames"

while read line; do
    binaryRpm=$(find -name "${line}" -type f | head -1)
    if [ -z "${binaryRpm}" ]; then
        echo "No matches found for \"${line}\"."
    else
        echo "Match for \"${line}\" found at \"${binaryRpm}\""
    fi
done < "${FILENAME}"

Comments on my above example:

  • head -1 will only keep the first match.
  • [ -z ] checks if the variable is empty (it will be empty if find doesn't find anything above).

Comments on your script:

  • You don't need to check if the filename matches the line since find will only return those that match. I guess you use it to discard multiple matches, but head is a better tool for that.
  • You don't need the continue statement as it stands, since the script will jump out of the if clause and iterate the for loop directly afterwards anyway.
share|improve this answer
    
I'm really very pleased to receive your remarks and answer,Sir. Sincere Thank You^^ –  sree May 12 '12 at 5:08
    
[ Additional Question ] ** But the above find command searches and prints more than one file having same file name. I need only One File from the Find Results, to compare the result from find command with that of file name reads from the File. You precious comments and answers are really much appreciable... Thanking you –  sree May 12 '12 at 5:14
    
@sree: You need to update your question with information on what you really are trying to do. It is very inefficient for us to need to keep guessing, and it sounds very likely that there is a better way than using find like this. –  Daniel Andersson May 12 '12 at 7:14
    
Steps that I'm trying to follow –  sree May 12 '12 at 8:35
    
Steps that I'm trying to follow, 1. Input File has list of binary files names 2. Read file line by line for list of binary file names 3. Search for the read binary file name within the current directory or all the directories 4. Compare the Searched file with that of the binary file name in the file 5. If Searched result contains more than one then consider only one Search result and copy the file to the corresponding destination directory. –  sree May 12 '12 at 8:45

Try this command:

find . -name "mybinaryrpm-1.0.24-1.1.i586.rpm" -exec basename {} \;
share|improve this answer
    
I'm really very pleased to receive your remarks and answer,Sir. Sincere Thank You^^ –  sree May 12 '12 at 5:11
    
[ Additional Question ] ** But the above find command searches and prints more than one file having same file name. I need only One File from the Find Results, to compare the result from find command with that of file name reads from the File. You precious comments and answers are really much appreciable... Thanking you –  sree May 12 '12 at 5:15

To answer you question, you could try something like that:

find ./ -name "mybinaryrpm-1.0.24-1.1.i586.rpm" | sed 's|^\..*/\(.*\)|\1|g'

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.