Linux Reader
Access files and folders on Ext, UFS, HFS, ReiserFS, or APFS file systems from Windows

Pew Pew madafakas!

Access files and folders on Ext, UFS, HFS, ReiserFS, or APFS file systems from Windows
Last updated: Jul 29, 2021

What is a Bash Dirname Command

Dirname is short for “directory name”. If you’re writing a bash script, this command is quite necessary, and here’s everything you should know about dirname.

Here you will find out:

  • when you can use a bash multiline command
  • how to print a multiline command
  • when DiskInternals can help you

Are you ready? Let's read!

What is a bash dirname command?

Dirname is a built-in command on Linux and Unix-like OSes; it is used to identify paths in shell scripts. Simply, dirname will remove the last part of a filename, after the last forward-slash (/), and print the remaining command as the name of the directory where that file is saved.

The syntax and options of dirname

The syntax of dirname is:

The syntax of dirname

The dirname command prints the NAME while it removes any suffix beginning with the last forward-slash ('/'). In a situation where there’s no slash (/), dirname will output a single period (.). Below is an example.

dirname offers some options, which are -z, --zero, --help, and --version.

The –z and –zero options separate the printed commands with NULL rather than initiating a newline.

The –help dirname option displays the help info and exits, while –version will print the version information and exits.

Examples of using bash dirname

Example 1:

In this example, let’s try to use dirname to extract the path info from a variable “pathnamevar” and save the result using the parameter expansion $( ).

Use dirname to extract the path

Example 2:

The directory tree of a file name

From the examples above, you can see that the dirname command simply outputs the directory tree of a file name.

The other ways to use dirname in a shell script

Dirname can handle multiple files; all you need do is to pass the absolute filenames one after another.

Example:

Handle multiple files with dirname

Output:

The output of multiple files

However, if the filename is also the absolute name…

If the string is simply the filename, dirname will print a (.) as the current directory. To return the absolute path where the script is saved, you will have to change the script’s directory, get the dirname, and then save the value to a variable before echoing it.

Example:

To return the absolute path change the script's directory

If the script above is saved as /home/dopi/bin/test1.sh, whether you run it as /home/dopi/bin/test1.sh or bin/test1.sh, it will echo:

The echo of a shell script that used dirname

Make it possible to open Linux files from Windows

Using DiskInternals Linux Reader software, you can easily open Linux files on Windows computers. This free software is especially designed to help users on dual-boot and virtual machines. DiskInternals Linux Reader features an intuitive user interface and is very easy to use.

Instructions for reading Linux files

  1. 1. Download and install DiskInternals Linux Reader on your computer.
  1. 2. Launch Linux Reader on your Windows computer; it will scan and look out for all Linux-compatible partitions, then display them on the interface. Double-click on the partition you want to open to view the files inside.
  1. 3. You will be presented with all the files saved inside the Linux partition, including media files, documents, etc. Right-click on any file and select “Preview in new window” to open a file in the Linux partition.
  1. 4. Copy the Linux files to a Windows partition and edit the content if you wish. That’s all.

Related articles

FREE DOWNLOADVer 4.18, WinUpgrade to PROFrom $29.95

Please rate this article.
51 reviews