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 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 $( ).
Example 2:
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:
Output:
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:
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:
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. Download and install DiskInternals Linux Reader on your computer.
- 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.
- 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.
- 4. Copy the Linux files to a Windows partition and edit the content if you wish. That’s all.
Related articles
- About a bash date command
- How to Access Linux Ext2 or Ext3 on Windows
- How to Access Ext4 from Windows
- How to Mount Ext4 on Windows for Free
- Mount Linux Drive on Windows for Free
- Bash Script: All You Need to Know
- An Algorithm: How to Create Bash While Loop
- Linux Shell: What You Need to Know at First
- 5 Basic Shell Script Examples for Your First Script
- Bash: How to Check if the File Does Not Exist
- Bash Time Command on Linux
- How to use bash get script directory in Linux
- How to Check Bash String Equality
- How to Use Linux Wait Command
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Bash Script SSH: How to Use It
- Bash: A Script For User Input
- Linux ZSH: the basic you need to know
- Basic of Grep in Linux Shell Script
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Bash: how to split strings in Linux
- Linux: Sudo in Bash Scripts
- Hello World Script in Bash
- A Linux bin/bash Shell
- Linux: New Line in Shell Script
- Linux: Bash String Ends With
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: $0 in a Shell Script
- Linux: A Bash Startup Script
- Linux: Write a Shell Script
- Shell Script: Replace String in File
- A crontab service shell script
- Bash and sh: is there any difference
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- Linux: A Bash Linter
- A Bash Nested Loop
- A Bash Test Command
- Use a Shell Script to Send an Email
- A Bash Status of Last Command
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- About Bash Language
- Bash for Loop in One Line
- AWK in a Bash Script
- Learn about useful bash scripts
- Learn about systemd startup script
- About chaining bash commands
- Learn about a bash error code
- Using a bash tee command
- About a bash export command
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash UNTIL loop
- Learn about C shell script
- Learn about Korn shell scripting
- Using /usr/bin/env command
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- The disk you inserted was not readable by this computer error
- Learn about SFTP in bash scripting
- Learn to run Perl script in Linux
- Examples of using the Expect
- Copy command in shell scripts
- Shell script usage
- Install Oracle Database
- Here is everything you need to know about Bash for Windows
- Guide in Pictures
- How to Use Shell Script Sleep Command
- A Bash Multiline Command




