0

I am trying to make a simple command in Linux similar to cd.. in DOS. What I tried is to make a script that changes directory to a path, which I have to get from pwd, by removing the last folder name.

So for a path: /home/usr/Downloads/images I want to get /home/usr/Downloads.

Mat
  • 202,337
  • 40
  • 393
  • 406
Brotchu
  • 115
  • 1
  • 8
  • just put a space in between `cd` and `..`. In DOS, that is admitted, but not in unix. So `cd ..` would be enough. – Luis Colorado Mar 31 '15 at 06:03
  • Possible duplicate of [Remove part of path on Unix](https://stackoverflow.com/questions/10986794/remove-part-of-path-on-unix) – jww Apr 04 '18 at 21:05

1 Answers1

2

You can use the dirname command to do what you're asking for, it remove the last "part" from a file. If what you give it is a directory, you'll get the parent directory.

parent=$(dirname /your/path/here)

But doing a cd.. with a script is not possible - the cd would only affect the shell that the script is running in, not the shell that invoked the script.

So you have to use an alias or a function.

alias cd..='cd ..'

Or

cdp() {
  cd ..
}
Mat
  • 202,337
  • 40
  • 393
  • 406
  • i added an alias in .bashrc, though i didnt know about 'cd ..' in linux , i always used to try 'cd..' – Brotchu Mar 29 '15 at 15:21