14

I'm new to sed, and need to grab just the filename from the output of find. I need to have find output the whole path for another part of my script, but I want to just print the filename without the path. I also need to match starting from the beginning of the line, not from the end. In english, I want to match, the first group of characters ending with ".txt" not containing a "/". Here's my attempt that doesn't work:

ryan@fizz:~$ find /home/ryan/Desktop/test/ -type f -name \*.txt
/home/ryan/Desktop/test/two.txt
/home/ryan/Desktop/test/one.txt
ryan@fizz:~$ find /home/ryan/Desktop/test/ -type f -name \*.txt | sed s:^.*/[^*.txt]::g
esktop/test/two.txt
ne.txt

Here's the output I want:

two.txt
one.txt

Ok, so the solutions offered answered my original question, but I guess I asked it wrong. I don't want to kill the rest of the line past the file suffix i'm searching for. So, to be more clear, if the following:

bash$ new_mp3s=\`find mp3Dir -type f -name \*.mp3\` && cp -rfv $new_mp3s dest
 `/mp3Dir/one.mp3' -> `/dest/one.mp3'
 `/mp3Dir/two.mp3' -> `/dest/two.mp3'

What I want is:

bash$ new_mp3s=\`find mp3Dir -type f -name \*.mp3\` && cp -rfv $new_mp3s dest | sed ???
 `one.mp3' -> `/dest'
 `two.mp3' -> `/dest'

Sorry for the confusion. My original question just covered the first part of what I'm trying to do.

2nd edit: here's what I've come up with:

DEST=/tmp && cp -rfv `find /mp3Dir -type f -name \*.mp3` $DEST | sed -e 's:[^\`].*/::' -e "s:$: -> $DEST:"

This isn't quite what I want though. Instead of setting the destination directory as a shell variable, I would like to change the first sed operation so it only changes the cp output before the "->" on each line, so that I still have the 2nd part of the cp output to operate on with another '-e'.

3rd edit: I haven't figured this out using only sed regex's yet, but the following does the job using Perl:

cp -rfv `find /mp3Dir -type f -name \*.mp3` /tmp | perl -pe "s:.*/(.*.mp3).*\`(.*/).*.mp3\'$:\$1 -> \$2:"

I'd like to do it in sed though.

6 Answers 6

17

Something like this should do the trick:

find yourdir -type f -name \*.txt | sed 's/.*\///'

or, slightly clearer,

find yourdir -type f -name \*.txt | sed 's:.*/::'
17

Why don't you use basename instead?

find /mydir | xargs -I{} basename {}
2
  • 1
    And its friend, dirname. Would be great if you showed a working example that uses the details provided in the question. Jul 25, 2009 at 2:32
  • i'd never heard of basename, and I'm glad i know about it now, but I was hoping for some sed regex help.
    – ryan_m
    Jul 25, 2009 at 4:47
10

No need external tools if using GNU find

find /path -name "*.txt" -printf "%f\n"
1
  • How did I not know about this?!? This is an incredibly handy feature to know. Thank you! Isolate directories with find /path -name "*.txt" -printf "%h\n" Jul 14, 2016 at 5:50
4

I landed on the question based on the title: using sed to grab filename from fullpath. So, using sed, the following is what worked for me...

FILENAME=$(echo $FULLPATH | sed -n 's/^\(.*\/\)*\(.*\)/\2/p')

The first group captures any directories from the path. This is discarded.

The second group capture is the text following the last slash (/). This is returned.

Examples:

echo "/test/file.txt" | sed -n 's/^\(.*\/\)*\(.*\)/\2/p'
file.txt

echo "/test/asd/asd/entrypoint.sh" | sed -n 's/^\(.*\/\)*\(.*\)/\2/p'
entrypoint.sh

echo "/test/asd/asd/default.json" | sed -n 's/^\(.*\/\)*\(.*\)/\2/p'
default.json
1
  • I too landed based on the title and this answer helped me :) Jul 7, 2022 at 9:00
2
find /mydir | awk -F'/' '{print $NF}'
-2
    path="parentdir2/parentdir1/parentdir0/dir/FileName"
    name=${path##/*}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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