Dirname Command in Linux with Examples Last Updated : 16 Oct, 2024 Comments Improve Suggest changes 2 Likes Like Report dirname is a command in Linux that is used to remove the trailing forward slashes "/" from the NAME and print the remaining portion. If the argument NAME does not contain the forward slash "/" then it simply prints dot ".". In other words, we can say that the 'dirname' command is a useful tool for extracting the directory portion from a given path or filename. Syntax dirname [OPTION] NAMEWhere,NAME is the file or directory path from which the directory portion will be extracted.OPTION allows for additional functionality such as version checking or line-ending control.Basic Example: Extracting the Directory Name from a Full PathWe know that it is used to extract the directory name from a given file path.dirname /Desktop/root/bash.shdirname /Desktop/root/bash.shHere we can see that our whole path is "/Desktop/root/bash.sh", where we have an output "/Desktop/root" which is our directory name, and "bash.sh" was our file name.dirname bash.shdirname bash.shHere we can see that no path is mentioned on file name "bash.sh" is mentioned. Our output shows "." which means that we are in the current directory.Common Options available for dirname Command 1. `-z or -zero` Option in `dirname` command in LinuxThis option provides a way to end the line by a null command rather than a new line. Using this the next command will execute on the same line rather than on the next line. Syntax:dirname -zero NAME or dirname --z NAMEExample:dirname -z /Desktop/root/bash.shdirname -z /Desktop/root/bash.shAs we can see we end the line with a null command rather than a new line.2. `--help` Option in `dirname` command in Linux It displays helpful information. dirname --helpdirname --help3. `--version` Option in `dirname` command in Linux It displays version information. dirname --versiondirname --versionConclusionIn this article we discussed the `dirname` command in Linux. Which provides a simple and effective way to extract the directory portion from a given file path or name. By going through this article one can understand that `dirname` command helps in manipulating and working with the file path efficiently. Create Quiz Comment D DrRoot_ Follow 2 Improve D DrRoot_ Follow 2 Improve Article Tags : Technical Scripter Linux-Unix Technical Scripter 2018 linux-command Linux-directory-commands +1 More Explore Getting Started with LinuxIntroduction to Linux Operating System7 min readLINUX Full Form - Lovable Intellect Not Using XP2 min readDifference between Linux and Windows7 min readLinux Distributions6 min readDifference between Unix and Linux6 min readInstallation with LinuxInstallation of Arch Linux in VirtualBox4 min readFedora Linux Operating System5 min readHow to install Ubuntu on VirtualBox?6 min readHow to Install Linux Mint?3 min readInstallation of Kali Linux in Virtual Machine2 min readHow to Install Linux on Windows PowerShell Subsystem?2 min readHow to Find openSUSE Linux Version?2 min readInstallation of CentOS2 min readLinux CommandsLinux Commands15+ min readEssential Unix Commands7 min readFind Command in Linux with Examples7 min readLinux File SystemLinux File System12 min readLinux File Hierarchy Structure5 min readLinux Directory Structure6 min readLinux KernelLinux Kernel4 min readKernel in Operating System3 min readHow Linux Kernel Boots?11 min readDifference between Operating System and Kernel3 min readLinux Kernel Module Programming: Hello World Program7 min readLinux Loadable Kernel Module7 min readLoadable Kernel Module - Linux Device Driver Development4 min readLinux Networking ToolsNetwork configuration and troubleshooting commands in Linux5 min readHow to configure network interfaces in CentOS?5 min readCommand-Line Tools and Utilities For Network Management in Linux8 min readLinux - Network Monitoring Tools4 min readLinux ProcessProcesses in Linux/Unix5 min readHow to Manage Process in Linux4 min readGetting System and Process Information Using C Programming and Shell in Linux2 min readProcess states and Transitions in a UNIX Process4 min readLinux FirewallLINUX Firewall7 min readiptables command in Linux with Examples7 min readHow to Configure your Linux Firewall - 3 Methods12 min readShell Scripting & Bash ScriptingIntroduction to Linux Shell and Shell Scripting7 min readUnderstanding Terminal, Console, Shell and Kernel3 min readHow to Create a Shell Script in linux7 min readShell Scripting - Different types of Variables4 min readBash Scripting - Introduction to Bash and Bash Scripting12 min readBash Script - Define Bash Variables and its types12 min readShell Scripting - Shell Variables6 min readBash Script - Difference between Bash Script and Shell Script4 min readShell Scripting - Difference between Korn Shell and Bash shell3 min readShell Scripting - Interactive and Non-Interactive Shell3 min readShell Script to Show the Difference Between echo â$SHELLâ and echo â$SHELLâ4 min readLinux Administrator SystemWhat is Linux System Administration?6 min readBeginner's Guide to Linux System Administration5 min readHow to Monitor System Usage, Outages and Troubleshoot Linux Servers6 min readLinux - Systemd and its Components3 min readBoot Process with Systemd in Linux3 min readHow to Control Systemd Services on Remote Linux Server2 min readHow to Start, Stop and Restart Services in Linux Using systemctl Command9 min read Like