Linux Commands - Return directory name using dirname

by
Jeremy Canfield |
Updated: December 20 2023
| Linux Commands articles
The dirname command returned the full directory name, for example.
~]# dirname /home/john.doe/Documents/foo.txt
/home/john.doe
You can pass is multiple paths.
~]$ dirname /home/john.doe/Documents/foo.txt /home/jane.doe/Documents/bar.txt
/home/john.doe
/home/jane.doe
And here is an example in a bash shell script.
#!/bin/bash
files=(/home/john.doe/Documents/foo.txt /home/john.doe/Documents/bar.txt /home/jane.doe/Documents/foo.txt /home/jane.doe/Documents/bar.txt)
for file in ${files[@]}; do
dirname $file
done
Did you find this article helpful?
If so, consider buying me a coffee over at