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
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 $( ).
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.
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.
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.