Use dirname in Linux

Source: Internet
Author: User
In Linux, use dirname-general Linux technology-Linux programming and kernel information. The following is a detailed description. In Linux, the pwd command and getcwd () function are commonly used to obtain directory paths. However, sometimes it is not easy to match common requirements. The problem comes from lightning. He wrote a program and handed it to the customer in a folder which contains several applications and files. The customer can put the folder in any directory, then the customer uses the absolute path to run a program in the folder:/program/a/B/c/d/e/folder/fgh. sh, run fgh in the customer. sh this program uses other files in the same folder as it:/program/a/B/c/d/e/folder/1.txt.

In this way, you need to obtain the absolute path of its directory in the fgh. sh program:/program/a/B/c/d/e/folder. The pwd command can only get the current directory, that is, the directory where the program is running, but cannot get the directory where the program is located. Dirname exactly implements this function. You can use dirname $0 to parse the absolute path/program/a/B/c/d/e/folder.

Later I thought about it. The dirname command is not very common and hard to remember. In general, you can also make judgments in the program. If the customer uses an absolute path to run the program, you can get the parameter, it is a long string of things/program/a/B/c/d/e/folder/fgh. sh, and then parse the desired path name from this parameter:

Filename =/program/a/B/c/d/e/folder/fgh. sh
Filedirectory =$ {filename % /*.*}

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.