${} for reading, extracting and replacing strings, you can use ${} to extract the string 1, extract the file name [[email protected] log]# Var=/dir1/dir2/file.txt[[email protected] log]# Echo ${VAR##*/}FILE.TXT2, extract suffix [[email protected] log]# echo ${var##*.} TXT3, extract the file name without suffix, in two steps [[email protected] log]# Tmp=${var##*/}[[email protected] log]# echo $tmpfile. Txt[[email protected] log]# echo ${tmp%.*}file4, extract directory [[email protected] log]# echo ${VAR%/*}/DIR1/DIR2
Use the file directory's proprietary commands basename and dirname1, extract file names, note: basename is a command that uses $ () instead of ${}[[email protected] log]# echo $ (basename $var) FILE.TXT2, extract filename without suffix [[email protected] log]# echo $ (basename $var. txt) file3, extract directory [[email protected] log]# dirname $var/ Dir1/dir2[[email protected] log]# echo $ (dirname $var)/dir1/dir2
Linux Shell extract file name and directory name