How to get only filename using sed
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
The easiest solution is remove everything until last appearance of /
:
echo /root/video.mp4 | sed 's/.*\///'
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: (First one is fixed now)
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 }'