Monday, April 29, 2024
15
rated 0 times [  15] [ 0]  / answers: 1 / hits: 19591  / 1 Year ago, tue, january 17, 2023, 10:17:17

I am writing a .sh to do some work for me, but I am now at the point where I have to cd to the directory the file /path/to/file.end is in.
terminal doesn't allow



cd /path/to/file.end
bash: cd: /path/to/file.end: Not a directory


there is sadly no workaround I know of, so it would be nice if you could help!


More From » command-line

 Answers
7

Type cd $( dirname /path/to/file.end). That will take you into /path/to.



Explanation:




  • dirname returns the complete path for a file (without the filename, which you would get with basename) - i.e. dirname /etc/apt/apt.conf.d/99update-notifier returns /etc/apt/apt.conf.d

  • the expression $(anything) is replaced by the result of the command in the parentheses. So cd $( dirname /etc/apt/apt.conf.d/99update-notifier) is executed as cd /etc/apt/apt.conf.d



Another (but old and discouraged) notation for the same was



cd `dirname /path/to/file.end` 

[#30439] Wednesday, January 18, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
heathree

Total Points: 157
Total Questions: 132
Total Answers: 108

Location: Honduras
Member since Mon, Apr 5, 2021
3 Years ago
;