Make cd automatically ls

Clash Royale CLAN TAG#URR8PPP
I find that I often do the following:
%> cd bla/bla
%> ls
I would like it that whenever I cd into a directory it automatically does an ls.
I fiddled with my .bashrc for a while, but couldn't figure out how to make it happen.
bash command-line cd-command
add a comment |
I find that I often do the following:
%> cd bla/bla
%> ls
I would like it that whenever I cd into a directory it automatically does an ls.
I fiddled with my .bashrc for a while, but couldn't figure out how to make it happen.
bash command-line cd-command
add a comment |
I find that I often do the following:
%> cd bla/bla
%> ls
I would like it that whenever I cd into a directory it automatically does an ls.
I fiddled with my .bashrc for a while, but couldn't figure out how to make it happen.
bash command-line cd-command
I find that I often do the following:
%> cd bla/bla
%> ls
I would like it that whenever I cd into a directory it automatically does an ls.
I fiddled with my .bashrc for a while, but couldn't figure out how to make it happen.
bash command-line cd-command
bash command-line cd-command
edited May 8 '13 at 8:30
Kevdog777
2,097123259
2,097123259
asked Sep 9 '11 at 17:56
RobKohrRobKohr
289134
289134
add a comment |
add a comment |
12 Answers
12
active
oldest
votes
You can do this with a function:
$ cdls() cd "$@" && ls;
The && means 'cd to a directory, and if successful (e.g. the directory exists), run ls'. Using the && operator is better then using a semicolon ; operator in between the two commands, as with cd "$@" ; ls; . This second command will run ls regardless if the cd worked or not. If the cd failed, ls will print the contents of your current directory, which will be confusing for the user. As a best practice, use && and not ;.
$ cdls /var/log
CDIS.custom fsck_hfs.log monthly.out system.log
$ pwd
/var/log
In general, it is a bad practice to rename a command which already exists, especially for a commonly called command like cd. Instead, create a new command with a different name. If you overwrite cd with a function or alias which is also named cd, what would happen when you enter a directory with 100,000 files? There are many utilities which use cd, and they may get confused by this unusual behavior. If you use a shared account (Such as root when you are working with other system administrators), it can be very dangerous to replace an existing command because the environment is different from what people expect.
That command really change directory? From bash's man page: "There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used"
– enzotib
Sep 9 '11 at 18:58
@enzotib : Yes, this really does change directory, at least for me. I updated my answer to show the output ofpwd. Not sure if this is a best practice, but it is commonly done. See tldp.org/LDP/abs/html/aliases.html for some examples.
– Stefan Lasiewski
Sep 9 '11 at 19:05
2
First: it does not work here. Second: in that page they use variables, not positional parameters. Third: ABS is a common source of bad practices.
– enzotib
Sep 9 '11 at 19:07
Ok fine, I added a function also. Maybe ABS is full of bad practices (Some people say this about shell scripting, in general), but at least they are advanced bad practices.
– Stefan Lasiewski
Sep 9 '11 at 19:10
1
The alias works for me on Snow Leopard but not on CentOS5 or CentOS6. I updated my answer to use a function only. No aliases.
– Stefan Lasiewski
Sep 10 '11 at 0:37
|
show 4 more comments
I have this in my .bashrc, and it works fine.
function cd
builtin cd "$@" && ls -F
Earlier in my .bashrc I have: [ -z "$PS1" ] && return, and everything after that line only applies to interactive sessions, so this doesn't affect how cd behaves in scripts.
What exactly does[ -z "$PS1" ] && returndo?
– syntagma
Jan 21 '15 at 14:34
2
[ -z "$PS1" ]checks if the$PS(interactive prompt variable) is "zero length" (-z). If it is zero length, this means it has not been set, so Bash must not be running in interactive mode. The&& returnpart exits from sourcing.bashrcat this point, under these conditions.
– frabjous
Jan 21 '15 at 17:42
1
Another way to check for interactivity is to look foriin"$-":case "$-" in *i*) ;; *) return ;; esac.
– Kusalananda
Jan 9 '17 at 19:38
@Kusalananda & frabjous: Are there cases where one should be used instead of the other?
– Swivel
Sep 30 '17 at 19:14
1
@Swivel ThePS1variable may be unset or empty and the shell may still be interactive (but without a prompt). I would check$-to make sure.
– Kusalananda
Sep 30 '17 at 19:24
|
show 6 more comments
off-topic, since the question is tagged /bash, but as some questions are closed as duplicate of this one that don't mention bash:
With zsh:
chpwd() ls
The chpwd() function is called by zsh whenever the current directory changes (by way of cd, pushd, popd...). tcsh has a similar feature and is probably where zsh got it from.
add a comment |
Why not add an alias to your .bashrc file?
Something like:
alias cdls='cd "$@" && ls'
@don_crissti A funtion and an alias are different things. So why not?
– Jodka Lemon
Dec 16 '15 at 14:09
add a comment |
The common solution of creating alias for cd command is not perfect because there are other commands which can change your current directory like popd or even running a script with cd command in it.
It is better to use $PROMPT_COMMAND Bash hook which executes a command before returning a prompt.
The command (a function in our case) will execute ls only if directory has changed to reduce screen noise. Code for .bashrc:
#each console has its own file to save PWD
PrevDir=$(tty)
PrevDir=/tmp/prev-dir$PrevDir////-
#don't ls when shell launched
echo $PWD > $PrevDir
LsAfterCd() sed 1d
echo $PWD > $PrevDir
PROMPT_COMMAND=LsAfterCd
add a comment |
In bash you cannot recur to aliases for action that require parameter. For this there are functions. So put in your ~/.bashrc the following
mycd()
cd "$1"
ls
4
cd "$1" && lswould be better.
– Gilles
Sep 9 '11 at 23:39
To allow a parameter for the ls command, I use function mycd builtin cd $1 && ls $2 . Now you can call the command e.g. mycd .. -la
– Christian Schulzendorff
Feb 19 '16 at 9:20
@ChristianSchulzendorff: better to use the quotes:function mycd builtin cd "$1" && ls "$2".
– enzotib
Feb 19 '16 at 16:58
Does not work, I just tried it. The file was empty, but after adding your code, nothing changed.
– Black
Jun 25 '18 at 14:20
add a comment |
THIS ONE IS THE ONLY POSIX COMPATIBLE ONE-LINE VERSION THAT YOU CAN USE BY JUST TYPING: "cd yourdir"!!!:
Copy this:
altercd() cd() unset -f cd ; cd $*; ls ; altercd; ; altercd
Now you just can do!!!:
cd /
(files listed)
cd /home
(files listed)
etc...
add a comment |
Place the below code in the .profile and it works.
Tested on HP-Unix box.
cdl()
if [ "$#" = 0 ]; then
cd ~ && ls -ltr
elif [ -d "$@" ]; then
cd "$@" && ls -ltr
else
echo "$@" directory not found!!!
fi
#SET YOUR ALIAS TO CD
alias cd="cdl"
add a comment |
Even more handy - with ability to go back in history:
function cd()
if [ -d "$@" ]; then
echo -n "Stack: "
pushd "$@"
ls
else
builtin cd "$@"
fi
function popd()
builtin popd "$@" && ls
When you change directory a line with: Stack: (current_dir) (previous_dir) ... will be shown, then ls output. To go back in dirs history just pop this command: popd.
I added else so you'll see an error when trying to go to a wrong directory.
if you just do cd - it will bring you to your last dir that you were in.
– Ian
Nov 13 '17 at 12:25
add a comment |
I think it's good to enable ls's options in this way as cd takes no option.
cdls()
cd $$# && ls $@:0:$#-1
Umm...cddoes take options.
– Kusalananda
Jan 11 at 19:20
add a comment |
Here's what I find useful (on Debian 9):
c()
cd "$@"
&& ls --color=always -C
This gives me truncated output with an ellipsis in case there are too many items in that directory so that the console stays clean:
$ c data/git/buildroot/package/
4th lua-markdown
a10disp lua-messagepack
acl lua-msgpack-native
acpica luaossl
acpid lua-periphery
[...]
$ ls -1 | wc --lines
1977
add a comment |
alias cd='builtin cd $1 && ls -l && builtin cd $1'
This add nothing that the other answers have not already covered.
– jasonwryan
Jul 17 '16 at 3:58
add a comment |
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do this with a function:
$ cdls() cd "$@" && ls;
The && means 'cd to a directory, and if successful (e.g. the directory exists), run ls'. Using the && operator is better then using a semicolon ; operator in between the two commands, as with cd "$@" ; ls; . This second command will run ls regardless if the cd worked or not. If the cd failed, ls will print the contents of your current directory, which will be confusing for the user. As a best practice, use && and not ;.
$ cdls /var/log
CDIS.custom fsck_hfs.log monthly.out system.log
$ pwd
/var/log
In general, it is a bad practice to rename a command which already exists, especially for a commonly called command like cd. Instead, create a new command with a different name. If you overwrite cd with a function or alias which is also named cd, what would happen when you enter a directory with 100,000 files? There are many utilities which use cd, and they may get confused by this unusual behavior. If you use a shared account (Such as root when you are working with other system administrators), it can be very dangerous to replace an existing command because the environment is different from what people expect.
That command really change directory? From bash's man page: "There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used"
– enzotib
Sep 9 '11 at 18:58
@enzotib : Yes, this really does change directory, at least for me. I updated my answer to show the output ofpwd. Not sure if this is a best practice, but it is commonly done. See tldp.org/LDP/abs/html/aliases.html for some examples.
– Stefan Lasiewski
Sep 9 '11 at 19:05
2
First: it does not work here. Second: in that page they use variables, not positional parameters. Third: ABS is a common source of bad practices.
– enzotib
Sep 9 '11 at 19:07
Ok fine, I added a function also. Maybe ABS is full of bad practices (Some people say this about shell scripting, in general), but at least they are advanced bad practices.
– Stefan Lasiewski
Sep 9 '11 at 19:10
1
The alias works for me on Snow Leopard but not on CentOS5 or CentOS6. I updated my answer to use a function only. No aliases.
– Stefan Lasiewski
Sep 10 '11 at 0:37
|
show 4 more comments
You can do this with a function:
$ cdls() cd "$@" && ls;
The && means 'cd to a directory, and if successful (e.g. the directory exists), run ls'. Using the && operator is better then using a semicolon ; operator in between the two commands, as with cd "$@" ; ls; . This second command will run ls regardless if the cd worked or not. If the cd failed, ls will print the contents of your current directory, which will be confusing for the user. As a best practice, use && and not ;.
$ cdls /var/log
CDIS.custom fsck_hfs.log monthly.out system.log
$ pwd
/var/log
In general, it is a bad practice to rename a command which already exists, especially for a commonly called command like cd. Instead, create a new command with a different name. If you overwrite cd with a function or alias which is also named cd, what would happen when you enter a directory with 100,000 files? There are many utilities which use cd, and they may get confused by this unusual behavior. If you use a shared account (Such as root when you are working with other system administrators), it can be very dangerous to replace an existing command because the environment is different from what people expect.
That command really change directory? From bash's man page: "There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used"
– enzotib
Sep 9 '11 at 18:58
@enzotib : Yes, this really does change directory, at least for me. I updated my answer to show the output ofpwd. Not sure if this is a best practice, but it is commonly done. See tldp.org/LDP/abs/html/aliases.html for some examples.
– Stefan Lasiewski
Sep 9 '11 at 19:05
2
First: it does not work here. Second: in that page they use variables, not positional parameters. Third: ABS is a common source of bad practices.
– enzotib
Sep 9 '11 at 19:07
Ok fine, I added a function also. Maybe ABS is full of bad practices (Some people say this about shell scripting, in general), but at least they are advanced bad practices.
– Stefan Lasiewski
Sep 9 '11 at 19:10
1
The alias works for me on Snow Leopard but not on CentOS5 or CentOS6. I updated my answer to use a function only. No aliases.
– Stefan Lasiewski
Sep 10 '11 at 0:37
|
show 4 more comments
You can do this with a function:
$ cdls() cd "$@" && ls;
The && means 'cd to a directory, and if successful (e.g. the directory exists), run ls'. Using the && operator is better then using a semicolon ; operator in between the two commands, as with cd "$@" ; ls; . This second command will run ls regardless if the cd worked or not. If the cd failed, ls will print the contents of your current directory, which will be confusing for the user. As a best practice, use && and not ;.
$ cdls /var/log
CDIS.custom fsck_hfs.log monthly.out system.log
$ pwd
/var/log
In general, it is a bad practice to rename a command which already exists, especially for a commonly called command like cd. Instead, create a new command with a different name. If you overwrite cd with a function or alias which is also named cd, what would happen when you enter a directory with 100,000 files? There are many utilities which use cd, and they may get confused by this unusual behavior. If you use a shared account (Such as root when you are working with other system administrators), it can be very dangerous to replace an existing command because the environment is different from what people expect.
You can do this with a function:
$ cdls() cd "$@" && ls;
The && means 'cd to a directory, and if successful (e.g. the directory exists), run ls'. Using the && operator is better then using a semicolon ; operator in between the two commands, as with cd "$@" ; ls; . This second command will run ls regardless if the cd worked or not. If the cd failed, ls will print the contents of your current directory, which will be confusing for the user. As a best practice, use && and not ;.
$ cdls /var/log
CDIS.custom fsck_hfs.log monthly.out system.log
$ pwd
/var/log
In general, it is a bad practice to rename a command which already exists, especially for a commonly called command like cd. Instead, create a new command with a different name. If you overwrite cd with a function or alias which is also named cd, what would happen when you enter a directory with 100,000 files? There are many utilities which use cd, and they may get confused by this unusual behavior. If you use a shared account (Such as root when you are working with other system administrators), it can be very dangerous to replace an existing command because the environment is different from what people expect.
edited Nov 30 '15 at 18:28
answered Sep 9 '11 at 18:03
Stefan LasiewskiStefan Lasiewski
8,789196178
8,789196178
That command really change directory? From bash's man page: "There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used"
– enzotib
Sep 9 '11 at 18:58
@enzotib : Yes, this really does change directory, at least for me. I updated my answer to show the output ofpwd. Not sure if this is a best practice, but it is commonly done. See tldp.org/LDP/abs/html/aliases.html for some examples.
– Stefan Lasiewski
Sep 9 '11 at 19:05
2
First: it does not work here. Second: in that page they use variables, not positional parameters. Third: ABS is a common source of bad practices.
– enzotib
Sep 9 '11 at 19:07
Ok fine, I added a function also. Maybe ABS is full of bad practices (Some people say this about shell scripting, in general), but at least they are advanced bad practices.
– Stefan Lasiewski
Sep 9 '11 at 19:10
1
The alias works for me on Snow Leopard but not on CentOS5 or CentOS6. I updated my answer to use a function only. No aliases.
– Stefan Lasiewski
Sep 10 '11 at 0:37
|
show 4 more comments
That command really change directory? From bash's man page: "There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used"
– enzotib
Sep 9 '11 at 18:58
@enzotib : Yes, this really does change directory, at least for me. I updated my answer to show the output ofpwd. Not sure if this is a best practice, but it is commonly done. See tldp.org/LDP/abs/html/aliases.html for some examples.
– Stefan Lasiewski
Sep 9 '11 at 19:05
2
First: it does not work here. Second: in that page they use variables, not positional parameters. Third: ABS is a common source of bad practices.
– enzotib
Sep 9 '11 at 19:07
Ok fine, I added a function also. Maybe ABS is full of bad practices (Some people say this about shell scripting, in general), but at least they are advanced bad practices.
– Stefan Lasiewski
Sep 9 '11 at 19:10
1
The alias works for me on Snow Leopard but not on CentOS5 or CentOS6. I updated my answer to use a function only. No aliases.
– Stefan Lasiewski
Sep 10 '11 at 0:37
That command really change directory? From bash's man page: "There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used"
– enzotib
Sep 9 '11 at 18:58
That command really change directory? From bash's man page: "There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used"
– enzotib
Sep 9 '11 at 18:58
@enzotib : Yes, this really does change directory, at least for me. I updated my answer to show the output of
pwd. Not sure if this is a best practice, but it is commonly done. See tldp.org/LDP/abs/html/aliases.html for some examples.– Stefan Lasiewski
Sep 9 '11 at 19:05
@enzotib : Yes, this really does change directory, at least for me. I updated my answer to show the output of
pwd. Not sure if this is a best practice, but it is commonly done. See tldp.org/LDP/abs/html/aliases.html for some examples.– Stefan Lasiewski
Sep 9 '11 at 19:05
2
2
First: it does not work here. Second: in that page they use variables, not positional parameters. Third: ABS is a common source of bad practices.
– enzotib
Sep 9 '11 at 19:07
First: it does not work here. Second: in that page they use variables, not positional parameters. Third: ABS is a common source of bad practices.
– enzotib
Sep 9 '11 at 19:07
Ok fine, I added a function also. Maybe ABS is full of bad practices (Some people say this about shell scripting, in general), but at least they are advanced bad practices.
– Stefan Lasiewski
Sep 9 '11 at 19:10
Ok fine, I added a function also. Maybe ABS is full of bad practices (Some people say this about shell scripting, in general), but at least they are advanced bad practices.
– Stefan Lasiewski
Sep 9 '11 at 19:10
1
1
The alias works for me on Snow Leopard but not on CentOS5 or CentOS6. I updated my answer to use a function only. No aliases.
– Stefan Lasiewski
Sep 10 '11 at 0:37
The alias works for me on Snow Leopard but not on CentOS5 or CentOS6. I updated my answer to use a function only. No aliases.
– Stefan Lasiewski
Sep 10 '11 at 0:37
|
show 4 more comments
I have this in my .bashrc, and it works fine.
function cd
builtin cd "$@" && ls -F
Earlier in my .bashrc I have: [ -z "$PS1" ] && return, and everything after that line only applies to interactive sessions, so this doesn't affect how cd behaves in scripts.
What exactly does[ -z "$PS1" ] && returndo?
– syntagma
Jan 21 '15 at 14:34
2
[ -z "$PS1" ]checks if the$PS(interactive prompt variable) is "zero length" (-z). If it is zero length, this means it has not been set, so Bash must not be running in interactive mode. The&& returnpart exits from sourcing.bashrcat this point, under these conditions.
– frabjous
Jan 21 '15 at 17:42
1
Another way to check for interactivity is to look foriin"$-":case "$-" in *i*) ;; *) return ;; esac.
– Kusalananda
Jan 9 '17 at 19:38
@Kusalananda & frabjous: Are there cases where one should be used instead of the other?
– Swivel
Sep 30 '17 at 19:14
1
@Swivel ThePS1variable may be unset or empty and the shell may still be interactive (but without a prompt). I would check$-to make sure.
– Kusalananda
Sep 30 '17 at 19:24
|
show 6 more comments
I have this in my .bashrc, and it works fine.
function cd
builtin cd "$@" && ls -F
Earlier in my .bashrc I have: [ -z "$PS1" ] && return, and everything after that line only applies to interactive sessions, so this doesn't affect how cd behaves in scripts.
What exactly does[ -z "$PS1" ] && returndo?
– syntagma
Jan 21 '15 at 14:34
2
[ -z "$PS1" ]checks if the$PS(interactive prompt variable) is "zero length" (-z). If it is zero length, this means it has not been set, so Bash must not be running in interactive mode. The&& returnpart exits from sourcing.bashrcat this point, under these conditions.
– frabjous
Jan 21 '15 at 17:42
1
Another way to check for interactivity is to look foriin"$-":case "$-" in *i*) ;; *) return ;; esac.
– Kusalananda
Jan 9 '17 at 19:38
@Kusalananda & frabjous: Are there cases where one should be used instead of the other?
– Swivel
Sep 30 '17 at 19:14
1
@Swivel ThePS1variable may be unset or empty and the shell may still be interactive (but without a prompt). I would check$-to make sure.
– Kusalananda
Sep 30 '17 at 19:24
|
show 6 more comments
I have this in my .bashrc, and it works fine.
function cd
builtin cd "$@" && ls -F
Earlier in my .bashrc I have: [ -z "$PS1" ] && return, and everything after that line only applies to interactive sessions, so this doesn't affect how cd behaves in scripts.
I have this in my .bashrc, and it works fine.
function cd
builtin cd "$@" && ls -F
Earlier in my .bashrc I have: [ -z "$PS1" ] && return, and everything after that line only applies to interactive sessions, so this doesn't affect how cd behaves in scripts.
answered Sep 9 '11 at 21:13
frabjousfrabjous
4,3371825
4,3371825
What exactly does[ -z "$PS1" ] && returndo?
– syntagma
Jan 21 '15 at 14:34
2
[ -z "$PS1" ]checks if the$PS(interactive prompt variable) is "zero length" (-z). If it is zero length, this means it has not been set, so Bash must not be running in interactive mode. The&& returnpart exits from sourcing.bashrcat this point, under these conditions.
– frabjous
Jan 21 '15 at 17:42
1
Another way to check for interactivity is to look foriin"$-":case "$-" in *i*) ;; *) return ;; esac.
– Kusalananda
Jan 9 '17 at 19:38
@Kusalananda & frabjous: Are there cases where one should be used instead of the other?
– Swivel
Sep 30 '17 at 19:14
1
@Swivel ThePS1variable may be unset or empty and the shell may still be interactive (but without a prompt). I would check$-to make sure.
– Kusalananda
Sep 30 '17 at 19:24
|
show 6 more comments
What exactly does[ -z "$PS1" ] && returndo?
– syntagma
Jan 21 '15 at 14:34
2
[ -z "$PS1" ]checks if the$PS(interactive prompt variable) is "zero length" (-z). If it is zero length, this means it has not been set, so Bash must not be running in interactive mode. The&& returnpart exits from sourcing.bashrcat this point, under these conditions.
– frabjous
Jan 21 '15 at 17:42
1
Another way to check for interactivity is to look foriin"$-":case "$-" in *i*) ;; *) return ;; esac.
– Kusalananda
Jan 9 '17 at 19:38
@Kusalananda & frabjous: Are there cases where one should be used instead of the other?
– Swivel
Sep 30 '17 at 19:14
1
@Swivel ThePS1variable may be unset or empty and the shell may still be interactive (but without a prompt). I would check$-to make sure.
– Kusalananda
Sep 30 '17 at 19:24
What exactly does
[ -z "$PS1" ] && return do?– syntagma
Jan 21 '15 at 14:34
What exactly does
[ -z "$PS1" ] && return do?– syntagma
Jan 21 '15 at 14:34
2
2
[ -z "$PS1" ] checks if the $PS (interactive prompt variable) is "zero length" (-z). If it is zero length, this means it has not been set, so Bash must not be running in interactive mode. The && return part exits from sourcing .bashrc at this point, under these conditions.– frabjous
Jan 21 '15 at 17:42
[ -z "$PS1" ] checks if the $PS (interactive prompt variable) is "zero length" (-z). If it is zero length, this means it has not been set, so Bash must not be running in interactive mode. The && return part exits from sourcing .bashrc at this point, under these conditions.– frabjous
Jan 21 '15 at 17:42
1
1
Another way to check for interactivity is to look for
i in "$-": case "$-" in *i*) ;; *) return ;; esac.– Kusalananda
Jan 9 '17 at 19:38
Another way to check for interactivity is to look for
i in "$-": case "$-" in *i*) ;; *) return ;; esac.– Kusalananda
Jan 9 '17 at 19:38
@Kusalananda & frabjous: Are there cases where one should be used instead of the other?
– Swivel
Sep 30 '17 at 19:14
@Kusalananda & frabjous: Are there cases where one should be used instead of the other?
– Swivel
Sep 30 '17 at 19:14
1
1
@Swivel The
PS1 variable may be unset or empty and the shell may still be interactive (but without a prompt). I would check $- to make sure.– Kusalananda
Sep 30 '17 at 19:24
@Swivel The
PS1 variable may be unset or empty and the shell may still be interactive (but without a prompt). I would check $- to make sure.– Kusalananda
Sep 30 '17 at 19:24
|
show 6 more comments
off-topic, since the question is tagged /bash, but as some questions are closed as duplicate of this one that don't mention bash:
With zsh:
chpwd() ls
The chpwd() function is called by zsh whenever the current directory changes (by way of cd, pushd, popd...). tcsh has a similar feature and is probably where zsh got it from.
add a comment |
off-topic, since the question is tagged /bash, but as some questions are closed as duplicate of this one that don't mention bash:
With zsh:
chpwd() ls
The chpwd() function is called by zsh whenever the current directory changes (by way of cd, pushd, popd...). tcsh has a similar feature and is probably where zsh got it from.
add a comment |
off-topic, since the question is tagged /bash, but as some questions are closed as duplicate of this one that don't mention bash:
With zsh:
chpwd() ls
The chpwd() function is called by zsh whenever the current directory changes (by way of cd, pushd, popd...). tcsh has a similar feature and is probably where zsh got it from.
off-topic, since the question is tagged /bash, but as some questions are closed as duplicate of this one that don't mention bash:
With zsh:
chpwd() ls
The chpwd() function is called by zsh whenever the current directory changes (by way of cd, pushd, popd...). tcsh has a similar feature and is probably where zsh got it from.
edited Oct 2 '12 at 21:17
answered Oct 2 '12 at 16:50
Stéphane ChazelasStéphane Chazelas
302k56570924
302k56570924
add a comment |
add a comment |
Why not add an alias to your .bashrc file?
Something like:
alias cdls='cd "$@" && ls'
@don_crissti A funtion and an alias are different things. So why not?
– Jodka Lemon
Dec 16 '15 at 14:09
add a comment |
Why not add an alias to your .bashrc file?
Something like:
alias cdls='cd "$@" && ls'
@don_crissti A funtion and an alias are different things. So why not?
– Jodka Lemon
Dec 16 '15 at 14:09
add a comment |
Why not add an alias to your .bashrc file?
Something like:
alias cdls='cd "$@" && ls'
Why not add an alias to your .bashrc file?
Something like:
alias cdls='cd "$@" && ls'
edited Dec 16 '15 at 14:06
don_crissti
50.5k15134162
50.5k15134162
answered Dec 16 '15 at 13:41
Kenneth AarKenneth Aar
311
311
@don_crissti A funtion and an alias are different things. So why not?
– Jodka Lemon
Dec 16 '15 at 14:09
add a comment |
@don_crissti A funtion and an alias are different things. So why not?
– Jodka Lemon
Dec 16 '15 at 14:09
@don_crissti A funtion and an alias are different things. So why not?
– Jodka Lemon
Dec 16 '15 at 14:09
@don_crissti A funtion and an alias are different things. So why not?
– Jodka Lemon
Dec 16 '15 at 14:09
add a comment |
The common solution of creating alias for cd command is not perfect because there are other commands which can change your current directory like popd or even running a script with cd command in it.
It is better to use $PROMPT_COMMAND Bash hook which executes a command before returning a prompt.
The command (a function in our case) will execute ls only if directory has changed to reduce screen noise. Code for .bashrc:
#each console has its own file to save PWD
PrevDir=$(tty)
PrevDir=/tmp/prev-dir$PrevDir////-
#don't ls when shell launched
echo $PWD > $PrevDir
LsAfterCd() sed 1d
echo $PWD > $PrevDir
PROMPT_COMMAND=LsAfterCd
add a comment |
The common solution of creating alias for cd command is not perfect because there are other commands which can change your current directory like popd or even running a script with cd command in it.
It is better to use $PROMPT_COMMAND Bash hook which executes a command before returning a prompt.
The command (a function in our case) will execute ls only if directory has changed to reduce screen noise. Code for .bashrc:
#each console has its own file to save PWD
PrevDir=$(tty)
PrevDir=/tmp/prev-dir$PrevDir////-
#don't ls when shell launched
echo $PWD > $PrevDir
LsAfterCd() sed 1d
echo $PWD > $PrevDir
PROMPT_COMMAND=LsAfterCd
add a comment |
The common solution of creating alias for cd command is not perfect because there are other commands which can change your current directory like popd or even running a script with cd command in it.
It is better to use $PROMPT_COMMAND Bash hook which executes a command before returning a prompt.
The command (a function in our case) will execute ls only if directory has changed to reduce screen noise. Code for .bashrc:
#each console has its own file to save PWD
PrevDir=$(tty)
PrevDir=/tmp/prev-dir$PrevDir////-
#don't ls when shell launched
echo $PWD > $PrevDir
LsAfterCd() sed 1d
echo $PWD > $PrevDir
PROMPT_COMMAND=LsAfterCd
The common solution of creating alias for cd command is not perfect because there are other commands which can change your current directory like popd or even running a script with cd command in it.
It is better to use $PROMPT_COMMAND Bash hook which executes a command before returning a prompt.
The command (a function in our case) will execute ls only if directory has changed to reduce screen noise. Code for .bashrc:
#each console has its own file to save PWD
PrevDir=$(tty)
PrevDir=/tmp/prev-dir$PrevDir////-
#don't ls when shell launched
echo $PWD > $PrevDir
LsAfterCd() sed 1d
echo $PWD > $PrevDir
PROMPT_COMMAND=LsAfterCd
answered Jan 9 '17 at 19:17
JackJack
1916
1916
add a comment |
add a comment |
In bash you cannot recur to aliases for action that require parameter. For this there are functions. So put in your ~/.bashrc the following
mycd()
cd "$1"
ls
4
cd "$1" && lswould be better.
– Gilles
Sep 9 '11 at 23:39
To allow a parameter for the ls command, I use function mycd builtin cd $1 && ls $2 . Now you can call the command e.g. mycd .. -la
– Christian Schulzendorff
Feb 19 '16 at 9:20
@ChristianSchulzendorff: better to use the quotes:function mycd builtin cd "$1" && ls "$2".
– enzotib
Feb 19 '16 at 16:58
Does not work, I just tried it. The file was empty, but after adding your code, nothing changed.
– Black
Jun 25 '18 at 14:20
add a comment |
In bash you cannot recur to aliases for action that require parameter. For this there are functions. So put in your ~/.bashrc the following
mycd()
cd "$1"
ls
4
cd "$1" && lswould be better.
– Gilles
Sep 9 '11 at 23:39
To allow a parameter for the ls command, I use function mycd builtin cd $1 && ls $2 . Now you can call the command e.g. mycd .. -la
– Christian Schulzendorff
Feb 19 '16 at 9:20
@ChristianSchulzendorff: better to use the quotes:function mycd builtin cd "$1" && ls "$2".
– enzotib
Feb 19 '16 at 16:58
Does not work, I just tried it. The file was empty, but after adding your code, nothing changed.
– Black
Jun 25 '18 at 14:20
add a comment |
In bash you cannot recur to aliases for action that require parameter. For this there are functions. So put in your ~/.bashrc the following
mycd()
cd "$1"
ls
In bash you cannot recur to aliases for action that require parameter. For this there are functions. So put in your ~/.bashrc the following
mycd()
cd "$1"
ls
answered Sep 9 '11 at 19:03
enzotibenzotib
33.8k710395
33.8k710395
4
cd "$1" && lswould be better.
– Gilles
Sep 9 '11 at 23:39
To allow a parameter for the ls command, I use function mycd builtin cd $1 && ls $2 . Now you can call the command e.g. mycd .. -la
– Christian Schulzendorff
Feb 19 '16 at 9:20
@ChristianSchulzendorff: better to use the quotes:function mycd builtin cd "$1" && ls "$2".
– enzotib
Feb 19 '16 at 16:58
Does not work, I just tried it. The file was empty, but after adding your code, nothing changed.
– Black
Jun 25 '18 at 14:20
add a comment |
4
cd "$1" && lswould be better.
– Gilles
Sep 9 '11 at 23:39
To allow a parameter for the ls command, I use function mycd builtin cd $1 && ls $2 . Now you can call the command e.g. mycd .. -la
– Christian Schulzendorff
Feb 19 '16 at 9:20
@ChristianSchulzendorff: better to use the quotes:function mycd builtin cd "$1" && ls "$2".
– enzotib
Feb 19 '16 at 16:58
Does not work, I just tried it. The file was empty, but after adding your code, nothing changed.
– Black
Jun 25 '18 at 14:20
4
4
cd "$1" && ls would be better.– Gilles
Sep 9 '11 at 23:39
cd "$1" && ls would be better.– Gilles
Sep 9 '11 at 23:39
To allow a parameter for the ls command, I use function mycd builtin cd $1 && ls $2 . Now you can call the command e.g. mycd .. -la
– Christian Schulzendorff
Feb 19 '16 at 9:20
To allow a parameter for the ls command, I use function mycd builtin cd $1 && ls $2 . Now you can call the command e.g. mycd .. -la
– Christian Schulzendorff
Feb 19 '16 at 9:20
@ChristianSchulzendorff: better to use the quotes:
function mycd builtin cd "$1" && ls "$2" .– enzotib
Feb 19 '16 at 16:58
@ChristianSchulzendorff: better to use the quotes:
function mycd builtin cd "$1" && ls "$2" .– enzotib
Feb 19 '16 at 16:58
Does not work, I just tried it. The file was empty, but after adding your code, nothing changed.
– Black
Jun 25 '18 at 14:20
Does not work, I just tried it. The file was empty, but after adding your code, nothing changed.
– Black
Jun 25 '18 at 14:20
add a comment |
THIS ONE IS THE ONLY POSIX COMPATIBLE ONE-LINE VERSION THAT YOU CAN USE BY JUST TYPING: "cd yourdir"!!!:
Copy this:
altercd() cd() unset -f cd ; cd $*; ls ; altercd; ; altercd
Now you just can do!!!:
cd /
(files listed)
cd /home
(files listed)
etc...
add a comment |
THIS ONE IS THE ONLY POSIX COMPATIBLE ONE-LINE VERSION THAT YOU CAN USE BY JUST TYPING: "cd yourdir"!!!:
Copy this:
altercd() cd() unset -f cd ; cd $*; ls ; altercd; ; altercd
Now you just can do!!!:
cd /
(files listed)
cd /home
(files listed)
etc...
add a comment |
THIS ONE IS THE ONLY POSIX COMPATIBLE ONE-LINE VERSION THAT YOU CAN USE BY JUST TYPING: "cd yourdir"!!!:
Copy this:
altercd() cd() unset -f cd ; cd $*; ls ; altercd; ; altercd
Now you just can do!!!:
cd /
(files listed)
cd /home
(files listed)
etc...
THIS ONE IS THE ONLY POSIX COMPATIBLE ONE-LINE VERSION THAT YOU CAN USE BY JUST TYPING: "cd yourdir"!!!:
Copy this:
altercd() cd() unset -f cd ; cd $*; ls ; altercd; ; altercd
Now you just can do!!!:
cd /
(files listed)
cd /home
(files listed)
etc...
edited Jan 11 at 20:45
answered Jan 11 at 19:28
Luciano Andress MartiniLuciano Andress Martini
3,738931
3,738931
add a comment |
add a comment |
Place the below code in the .profile and it works.
Tested on HP-Unix box.
cdl()
if [ "$#" = 0 ]; then
cd ~ && ls -ltr
elif [ -d "$@" ]; then
cd "$@" && ls -ltr
else
echo "$@" directory not found!!!
fi
#SET YOUR ALIAS TO CD
alias cd="cdl"
add a comment |
Place the below code in the .profile and it works.
Tested on HP-Unix box.
cdl()
if [ "$#" = 0 ]; then
cd ~ && ls -ltr
elif [ -d "$@" ]; then
cd "$@" && ls -ltr
else
echo "$@" directory not found!!!
fi
#SET YOUR ALIAS TO CD
alias cd="cdl"
add a comment |
Place the below code in the .profile and it works.
Tested on HP-Unix box.
cdl()
if [ "$#" = 0 ]; then
cd ~ && ls -ltr
elif [ -d "$@" ]; then
cd "$@" && ls -ltr
else
echo "$@" directory not found!!!
fi
#SET YOUR ALIAS TO CD
alias cd="cdl"
Place the below code in the .profile and it works.
Tested on HP-Unix box.
cdl()
if [ "$#" = 0 ]; then
cd ~ && ls -ltr
elif [ -d "$@" ]; then
cd "$@" && ls -ltr
else
echo "$@" directory not found!!!
fi
#SET YOUR ALIAS TO CD
alias cd="cdl"
answered Oct 20 '16 at 16:21
user196161user196161
1
1
add a comment |
add a comment |
Even more handy - with ability to go back in history:
function cd()
if [ -d "$@" ]; then
echo -n "Stack: "
pushd "$@"
ls
else
builtin cd "$@"
fi
function popd()
builtin popd "$@" && ls
When you change directory a line with: Stack: (current_dir) (previous_dir) ... will be shown, then ls output. To go back in dirs history just pop this command: popd.
I added else so you'll see an error when trying to go to a wrong directory.
if you just do cd - it will bring you to your last dir that you were in.
– Ian
Nov 13 '17 at 12:25
add a comment |
Even more handy - with ability to go back in history:
function cd()
if [ -d "$@" ]; then
echo -n "Stack: "
pushd "$@"
ls
else
builtin cd "$@"
fi
function popd()
builtin popd "$@" && ls
When you change directory a line with: Stack: (current_dir) (previous_dir) ... will be shown, then ls output. To go back in dirs history just pop this command: popd.
I added else so you'll see an error when trying to go to a wrong directory.
if you just do cd - it will bring you to your last dir that you were in.
– Ian
Nov 13 '17 at 12:25
add a comment |
Even more handy - with ability to go back in history:
function cd()
if [ -d "$@" ]; then
echo -n "Stack: "
pushd "$@"
ls
else
builtin cd "$@"
fi
function popd()
builtin popd "$@" && ls
When you change directory a line with: Stack: (current_dir) (previous_dir) ... will be shown, then ls output. To go back in dirs history just pop this command: popd.
I added else so you'll see an error when trying to go to a wrong directory.
Even more handy - with ability to go back in history:
function cd()
if [ -d "$@" ]; then
echo -n "Stack: "
pushd "$@"
ls
else
builtin cd "$@"
fi
function popd()
builtin popd "$@" && ls
When you change directory a line with: Stack: (current_dir) (previous_dir) ... will be shown, then ls output. To go back in dirs history just pop this command: popd.
I added else so you'll see an error when trying to go to a wrong directory.
answered Oct 23 '16 at 14:49
Ctrl-CCtrl-C
1887
1887
if you just do cd - it will bring you to your last dir that you were in.
– Ian
Nov 13 '17 at 12:25
add a comment |
if you just do cd - it will bring you to your last dir that you were in.
– Ian
Nov 13 '17 at 12:25
if you just do cd - it will bring you to your last dir that you were in.
– Ian
Nov 13 '17 at 12:25
if you just do cd - it will bring you to your last dir that you were in.
– Ian
Nov 13 '17 at 12:25
add a comment |
I think it's good to enable ls's options in this way as cd takes no option.
cdls()
cd $$# && ls $@:0:$#-1
Umm...cddoes take options.
– Kusalananda
Jan 11 at 19:20
add a comment |
I think it's good to enable ls's options in this way as cd takes no option.
cdls()
cd $$# && ls $@:0:$#-1
Umm...cddoes take options.
– Kusalananda
Jan 11 at 19:20
add a comment |
I think it's good to enable ls's options in this way as cd takes no option.
cdls()
cd $$# && ls $@:0:$#-1
I think it's good to enable ls's options in this way as cd takes no option.
cdls()
cd $$# && ls $@:0:$#-1
answered Feb 14 '17 at 11:45
iBugiBug
801724
801724
Umm...cddoes take options.
– Kusalananda
Jan 11 at 19:20
add a comment |
Umm...cddoes take options.
– Kusalananda
Jan 11 at 19:20
Umm...
cd does take options.– Kusalananda
Jan 11 at 19:20
Umm...
cd does take options.– Kusalananda
Jan 11 at 19:20
add a comment |
Here's what I find useful (on Debian 9):
c()
cd "$@"
&& ls --color=always -C
This gives me truncated output with an ellipsis in case there are too many items in that directory so that the console stays clean:
$ c data/git/buildroot/package/
4th lua-markdown
a10disp lua-messagepack
acl lua-msgpack-native
acpica luaossl
acpid lua-periphery
[...]
$ ls -1 | wc --lines
1977
add a comment |
Here's what I find useful (on Debian 9):
c()
cd "$@"
&& ls --color=always -C
This gives me truncated output with an ellipsis in case there are too many items in that directory so that the console stays clean:
$ c data/git/buildroot/package/
4th lua-markdown
a10disp lua-messagepack
acl lua-msgpack-native
acpica luaossl
acpid lua-periphery
[...]
$ ls -1 | wc --lines
1977
add a comment |
Here's what I find useful (on Debian 9):
c()
cd "$@"
&& ls --color=always -C
This gives me truncated output with an ellipsis in case there are too many items in that directory so that the console stays clean:
$ c data/git/buildroot/package/
4th lua-markdown
a10disp lua-messagepack
acl lua-msgpack-native
acpica luaossl
acpid lua-periphery
[...]
$ ls -1 | wc --lines
1977
Here's what I find useful (on Debian 9):
c()
cd "$@"
&& ls --color=always -C
This gives me truncated output with an ellipsis in case there are too many items in that directory so that the console stays clean:
$ c data/git/buildroot/package/
4th lua-markdown
a10disp lua-messagepack
acl lua-msgpack-native
acpica luaossl
acpid lua-periphery
[...]
$ ls -1 | wc --lines
1977
answered Jan 11 at 19:17
SuperlexxSuperlexx
112
112
add a comment |
add a comment |
alias cd='builtin cd $1 && ls -l && builtin cd $1'
This add nothing that the other answers have not already covered.
– jasonwryan
Jul 17 '16 at 3:58
add a comment |
alias cd='builtin cd $1 && ls -l && builtin cd $1'
This add nothing that the other answers have not already covered.
– jasonwryan
Jul 17 '16 at 3:58
add a comment |
alias cd='builtin cd $1 && ls -l && builtin cd $1'
alias cd='builtin cd $1 && ls -l && builtin cd $1'
edited Jul 17 '16 at 3:11
answered Jul 17 '16 at 3:03
joekjoek
11
11
This add nothing that the other answers have not already covered.
– jasonwryan
Jul 17 '16 at 3:58
add a comment |
This add nothing that the other answers have not already covered.
– jasonwryan
Jul 17 '16 at 3:58
This add nothing that the other answers have not already covered.
– jasonwryan
Jul 17 '16 at 3:58
This add nothing that the other answers have not already covered.
– jasonwryan
Jul 17 '16 at 3:58
add a comment |