Ask Your Question

How can I extract the directory name from a given path in bash?

asked 2022-06-18 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-12-11 06:00:00 +0000

huitzilopochtli gravatar image

You can use the dirname command in bash to extract the directory name from a given path.


$ dirname /path/to/file


$ dirname /home/user/Documents/example.txt

This command will return the directory name from the given path "/home/user/Documents/example.txt". In this case, the output will be "/home/user/Documents".

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2022-06-18 11:00:00 +0000

Seen: 20 times

Last updated: Dec 11 '22