Specific use of Linux dirname command

Specific use of Linux dirname command

01. Command Overview

dirname - strip non-directory suffixes from file names

The dirname command removes the non-directory part of the file name and displays only the contents related to the directory. The dirname command reads the specified path name, retains the last / and the characters following it, deletes the rest, and writes the result to standard output. If there are no characters after the last /, the dirname command uses the second-to-last / and ignores all characters after it. dirname and basename are commonly used within shell command substitution to specify an output file name that is slightly different from the specified input file name.

02. Command format

dirname namedirname options

03. Common options

Print NAME with the part after / removed; if NAME does not contain /, print `.' (which means the current directory).

--help 
 show help and exit --version
 Output version information and exit

04. Reference examples

4.1 What if the last file is a directory?

[deng@localhost test]$ dirname /home/deng/share/
/home/deng

4.2 If the last file is a normal file

[deng@localhost test]$ dirname /home/deng/scott_data.sql 
/home/deng

4.3 If the name does not contain /, then output.

[deng@localhost test]$ dirname dir
.
[deng@localhost test]$

4.4 Relative path case

[deng@localhost test]$ dirname dir/a
dir

4.5 When the path is the root directory

[deng@localhost test]$ dirname /
/
[deng@localhost test]$ dirname //
/
[deng@localhost test]$

This is the end of this article about the specific usage of the Linux dirname command. For more information about the Linux dirname command, please search 123WORDPRESS.COM’s previous articles or the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use python3 to get the current path and use os.path.dirname
  • The difference between node global variables __dirname and __filename
  • A brief analysis of the use of os.path.dirname(__file__) in python3
  • The meaning and application of dirname(__FILE__)
  • Introduction to the use of __dirname in Node.js
  • Detailed explanation of the difference between php getcwd and dirname(__FILE__)
  • Instructions for using the path.dirname method in node.js
  • Example analysis of usage of __FILE__, dirname and basename in PHP
  • Parsing php dirname() and __FILE__ constant application
  • php dirname(__FILE__) gets the absolute path of the current file
  • PHP dirname (_file_) explanation

<<:  Causes and solutions for slow MySQL query speed and poor performance

>>:  Implementation of react routing guard (routing interception)

Recommend

Install and use Git and GitHub on Ubuntu Linux

Introduction to Git Git is an open source version...

Summary of Common Mistakes in Web Design

In the process of designing a web page, designers...

JavaScript imitates Jingdong carousel effect

This article shares the specific code for JavaScr...

jQuery achieves full screen scrolling effect

This article example shares the specific code of ...

MySQL DATE_ADD and ADDDATE functions add a specified time interval to a date

MySQL DATE_ADD(date,INTERVAL expr type) and ADDDA...

CentOS 7.x deployment of master and slave DNS servers

1. Preparation Example: Two machines: 192.168.219...

Vue implements image dragging and sorting

This article example shares the specific code of ...

CSS method of controlling element height from bottom to top and from top to bottom

Let’s start the discussion from a common question...

Native JS object-oriented typing game

This article shares the specific code of JS objec...

Detailed explanation of Mysql self-join query example

This article describes the Mysql self-join query....

How to use axios to make network requests in React Native

In front-end development, there are many ways to ...

HTML drawing user registration page

This article shares the specific implementation c...