• Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'dirname' Command In Linux

2023-12-13 - By Robert Elder

     I use the 'dirname' command to extract the leading path components from a file or directory path:

dirname /home/robert/todo.txt
/home/robert
dirname /home/robert/my-directory
/home/robert

Relative & Absolute Paths

     In this example, the 'dirname' command extracts the directory part from the relative or absolute path of the image file:

dirname robert/subdir/cat.png
robert/subdir
dirname /home/robert/cat.png
/home/robert

Works With Directories

     If the full path ends with a directory, the 'dirname' command extracts the enclosing directory:

dirname /home/robert/backups
/home/robert
dirname /home/robert/backups/
/home/robert

Extract Multiple Directories At Once

     You can pass multiple arguments to the 'dirname' command, to extract the leading path components of all arguments:

dirname /home/robert/cat.png /home/mike/dog.png /home/sally/rat.png
/home/robert
/home/mike
/home/sally

The 'dirname' Vs. 'basename' Commands

     According to the info pages, the 'basename' and 'dirname' commands are related in the following way:

info dirname
...
   Together, ‘basename’ and ‘dirname’ are designed such that if ‘ls
"$name"’ succeeds, then the command sequence ‘cd "$(dirname "$name")";
ls "$(basename "$name")"’ will, too.  This works for everything except
file names containing a trailing newline.
...

     To put the above paragraph into practice, we can say that if the following 'ls' command succeeds:

name=/home/robert/cat.png
ls "$name"

     Then these commands should also succeed:

cd "$(dirname "$name")"
ls "$(basename "$name")"

     In other words, if this command succeeds:

ls "/home/robert/cat.png"

     these commands should also succeed:

cd "$(dirname "/home/robert/cat.png")"
ls "$(basename "/home/robert/cat.png")"

     and, equivalently, these commands should too:

cd "/home/robert/"
ls "cat.png"

Works With Non-existant Files & Symlinks

     The dirname command is not affected by symlinks or whether a file actually exists or not, as illustrated by the following examples:

mkdir first-directory
mkdir first-directory/subdir/
touch first-directory/subdir/foo.txt
ln -s first-directory second-directory
ls -l
total 4
drwxrwxr-x 3 robert robert 4096 Nov 20 15:19 first-directory
lrwxrwxrwx 1 robert robert   15 Nov 20 15:19 second-directory -> first-directory
ls -l first-directory
total 4
drwxrwxr-x 2 robert robert 4096 Nov 20 15:19 subdir
ls -l first-directory/subdir
total 0
-rw-rw-r-- 1 robert robert 0 Nov 20 15:19 foo.txt
dirname first-directory/subdir/foo.txt
first-directory/subdir
dirname second-directory/subdir/foo.txt
second-directory/subdir
rm -r first-directory/subdir
dirname first-directory/subdir/foo.txt
first-directory/subdir
dirname second-directory/subdir/foo.txt
second-directory/subdir

Implementation

     The GNU Core Utils version of the 'dirname' command simply performs strings operations to print the first part of the file path.

     And that's why the 'dirname' command is my favourite Linux command.

Intro To 'stty' Command In Linux
Published 2023-10-04
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List

Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
Privacy Policy      Store Policies      Terms of Use