Linux Shell extract file name and directory name

Source: Internet
Author: User
${} 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.