Sed – How to Get Only Filename Using Sed

filenamessed

How can I get only the filename using sed? I've this

out_file=$(echo $in_file|sed "s/\(.*\.\).*/\1mp4/g")

But I get the path too /root/video.mp4, and I want only video.mp4.

Best Answer

basename from the GNU coreutils can help you doing this job:

$ basename /root/video.mp4
video.mp4

If you already know the extension of the file, you can invoke basename using the syntax basename NAME [SUFFIX] in order to remove it:

$ basename /root/video.mp4 .mp4
video

Or another option would be cutting everything after the last dot using sed:

$ basename /root/video.old.mp4 | sed 's/\.[^.]*$//'
video.old
Related Question