13

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!

Azsgy
  • 265
  • 4
    Um, do you mean cd /path/to? That will change the directory to the directory that the file file.end is in. – Alaa Ali Jul 05 '13 at 09:56
  • 1
    No, the file is variable, and I would like to enter the path of the file (drop it into the terminal) and then go to the folder it is in – Azsgy Jul 05 '13 at 10:27
  • Related: adding shopt -s autocd (default on zsh) to your ~/.bashrc, Allows you to cd into directory merely by typing the directory name. More: man bash | grep "autocd" – Pablo Bianchi Jul 01 '23 at 03:46

4 Answers4

17

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` 
guntbert
  • 13,134
9

You can not cd into a file. Here is a (command line) function that will automatically cd into a path for a given fully qualified file path:

function fcd () { [ -f "$1" ] && { cd "$(dirname "$1")"; } || { cd "$1"; } ; pwd; }
glenn jackman
  • 17,900
Rinzwind
  • 299,756
  • Did I get this right?: function fcd () { [ -f $1 ] && { cd $(/file.end $1); } || { cd $1 ; } pwd } the response is: bash: syntax error near unexpected token `pwd' it gives me an error this way :( – Azsgy Jul 05 '13 at 10:30
  • 2
    @Atsch, need a semicolon before pwd, and if you're defining a function in one line, need a semicolon after pwd. – glenn jackman Jul 05 '13 at 10:36
  • @glennjackman the function just exits without any output or change of dir :( – Azsgy Jul 05 '13 at 10:43
  • run the function, then enter echo $? -- what is the exit status? – glenn jackman Jul 05 '13 at 11:07
  • darn I should have not doubted myself. The alternative looked better to me when I found it _O- – Rinzwind Jul 05 '13 at 11:11
  • Thanks, was looking for something like this - I bump into bash: cd: Not a directory more often than I'd like to; this might help. Cheers! – sdaau Jan 18 '15 at 20:01
  • A more robust version of this function that handles multiple arguments can be found here: https://stackoverflow.com/a/59049206/401712 (cd is not commonly used with multiple arguments, but it's still good to support them) – Dario Seidl Sep 28 '21 at 08:56
2

If you append "/.." to the filename that will take you to the correct directory e.g. cd /path/to/file.end/... It works on Cygwin anyway.

brain
  • 121
  • 2
0

Use:

cd $(dirname $(realpath <path>/myfile.txt))

It unfolds the path of file.