Home BASH [Bash Tips] How To cd and ls in one command
Bash function to cd and ls in one command

[Bash Tips] How To cd and ls in one command

By sk
874 Views

Whenever I cd into a directory, the next thing I spontaneously do - listing the contents of that directory using ls command. I don't know if all of you do this, but I guess some of you might have this habit. It would be better if we combine the functionality of the cd and ls in one command, wouldn't it? This brief guide explains how to list directory contents automatically whenever you cd into it in Linux.

How To cd and ls in one command

Please note that I am not talking about the one-liner commands. For instance, you can perform cd and ls with a single-line command like below.

$ cd dir && ls

Yes, It does work! But it is not the scope of this guide. We will do it with the help of a simple BASH function. This trick will work in all Unix-like distributions that supports BASH.

Open your ~/.bashrc file in your favorite editor:

$ nano ~/.bashrc

Add the following lines at the end:

cdls() {
        local dir="$1"
        local dir="${dir:=$HOME}"
        if [[ -d "$dir" ]]; then
                cd "$dir" >/dev/null; ls --color=auto
        else
                echo "bash: cdls: $dir: Directory not found"
        fi
}

BASH function to use cd and ls in one command

Here, I used the function name as cdls () for the sake of the easy remembering! You can name this function however you please. Also replace ls --color=auto parameter with your own. Save and close the file.

Run the following command to take effect the changes.

$ source ~/.bashrc

Now list the directory contents automatically whenever you cd into it like below.

Example:

$ cdls /var/log/
alternatives.log  btmp                   dpkg.log   kern.log   syslog               wtmp
apt               cloud-init.log         faillog    landscape  syslog.1
auth.log          cloud-init-output.log  installer  lastlog    tallylog
bootstrap.log     dist-upgrade           journal    lxd        unattended-upgrades
[email protected]:/var/log$ pwd
/var/log

cd and ls in one command

You can also add the following much simpler function in your ~/.bashrc file.

cdls() {
        cd "[email protected]" && ls;
}

Source the ~/.bashrc file to take effect the changes. Also don't use both functions at the same time with same name. Either use one function or use different name (E.g. cdls and cl) for each function.

Before I knew this trick, I usually do:

$ cd /var/log/

And then;

$ ls

Or,

$ cd /var/log/ && ls

Not anymore! Now I can be able to list any directory contents whenever I cd into it. This is handy when you're working with large number of directories often.

One disadvantage of this trick is you will have to wait a few seconds to minutes when cd into a directory that contains hundreds and thousands of files. In such cases, use the normal cd command.


Suggested read:


Hope this helps.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

4 comments

Jalal March 27, 2020 - 2:44 pm

Hi,
Very Useful function(cdls)
Thanks a lot

Reply
Lucien van Lint March 28, 2020 - 9:20 pm

Most (all?) shells accept ‘;’ as seperation between commands
bash$ cd ~; ls

Reply
zen April 3, 2020 - 5:17 pm

this is not a single command – its 2 commands seperated by the AND operator

Reply
sk April 3, 2020 - 5:41 pm

Yes, we include the 2 commands in a BASH function and run it as single command.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More