Shell command---dirname

Source: Internet
Author: User

Description of Use

The dirname command can take the directory portion of a given path (strip non-directory suffix from file name). This command is rarely used directly from the shell command line, and I typically use it in a shell script to get the directory where the script file is located and then switch the current directory over. According to the manual page, "Print NAME with its trailing/component removed; If NAME contains no/' s, Output '. ' (meaning the current directory). "It seems that" taking the directory portion of a given path "does not accurately summarize the purpose of the dirname command. Another command under Linux is basename, which, contrary to dirname, is the part that gets the file name.

Common parameters

No.

Example of using a sample from a man page

[Email protected] ~]# Dirname/usr/bin/sort
/usr/bin
[Email protected] ~]# dirname stdio.h
.
[Email protected] ~]#

Example Two

[Email protected] ~]# Dirname/usr/bin
/usr
[Email protected] ~]# dirname/usr/bin/
/usr

Note: The output here seems a bit strange. This should take a look at the source code of the DirName command to explain.

Example three using bash code in a bash script
    1. #!/bin/sh
    2. # Jump to the directory where the script is located
    3. CD $ (dirname "$") | | exit 1
    4. # The rest of the part

I often use this notation because sometimes the script does not know which directory it is in, such as when the script is used in crontab.

Another notation is: CD ' DirName $ ' where the anti-quote · Equivalent to $ ().

Shell command---dirname

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.