New answers tagged cd
2
In zsh, I often do:
cd /path/to/somefile(:h)
(h for head).
If somefile is a symlink, you can also do:
cd somefile(:A:h)
To get to the directory where the target of the symlink may be found.
The zsh equivalent of Chris' now bash-only solution would be:
cd() {
[[ ! -e $argv[-1] ]] || [[ -d $argv[-1] ]] || argv[-1]=${argv[-1]%/*}
builtin cd "$@"
...
2
If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:
mycd() { if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ; }
If you name it cd, then strange things will happen.
11
I assume you still want to retain the original functionality if you input a directory, and you are using bash.
cd() {
local file="${!#}"
if (( "$#" )) && ! [[ -d "$file" ]]; then
builtin cd "${@:1:($#-1)}" "${file%/*}"
else
builtin cd "$@"
fi
}
If you are never going to use cd's options (-P, etc), then this will ...
6
You could use dirname to strip the filename from the path, e.g.
mycd() { cd "$(dirname "$1")"; }
See man dirname.
6
I assume that this means that you want to still be in the directory after ls has run, if not, just run ls with the dir as an argument.
cl() {
cd "$@" && ls
}
foo$ mkdir bar
foo$ > bar/baz
foo$ > bar/qux
foo$ cl bar
baz qux
bar$
Top 50 recent answers are included

