Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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
share|improve this answer
1  
Using sed 's/\.[^.]*$//' as you have, will fail for (hidden) .filename and . and .. directories –  Peter.O Aug 4 '12 at 13:47

Use any of the followings ways:

out_file="${in_file##*/}"

out_file="$(basename $in_file)"

out_file="$(echo $in_file | sed 's=.*/==')"

out_file="$(echo $in_file | awk -F"/" '{ print $NF }')"

ps. You get the same string because in your statement \(.*\.\) matches to the string from the beginning until dot (/root/video.) and then you manually add .mp4 which is the same as in you original string. You should use s=.*\([^/]*\)=\1= instead.

Update:

To get the only filename without extension you can :

out_file="$(echo $in_file | sed 's=.*/==;s/\.[^.]*$/.new_ext/'

out_file="$(echo $in_file | sed 's=\([^/]*\)\.[^./]*$=\1.new_ext=')"

out_file="$(echo $in_file | awk -F"/" '{ gsub (/\.[^/.]*$/,".new_ext",$NF);print $NF }'
share|improve this answer
    
But with any of those methods I get filename with the format and I need to get only the filename and put a new format manually. –  Shixons Aug 4 '12 at 6:30
    
Ah, that makes sense. I've updated my answer. –  rush Aug 4 '12 at 6:43
    
@rush: There will be edge cases, e.g. for a file named my.file.tar.gz. –  donothingsuccessfully Aug 4 '12 at 7:02
    
Thanks, that worked :). –  Shixons Aug 4 '12 at 7:08
    
@donothingsuccessfully there was a missing dot symbol in the last sed and awk. Fixed. Thank you. –  rush Aug 4 '12 at 7:09

The easiest solution is remove everything until last appearance of /:

echo /root/video.mp4 | sed 's/.*\///'

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.