Fast command-line directory browsing

cdls() { if [[ $1 != "" ]] ; then cd $1; ls; else ls; fi };
Not really alternative, just giving a different behavior listing current directory if no directory given.

By: nnsense
2015-05-11 15:52:09

1 Alternatives + Submit Alt

  • After typing cd directory [enter] ls [enter] so many times, I figured I'd try to make it into a function. I was surprised how smoothly I was able to integrate it into my work on the command line. Just use cdls as you would cd. It will automatically list the directory contents after you cd into the directory. To make the command always available, add it to your .bashrc file. Not quite monumental, but still pretty convenient. Show Sample Output

    function cdls { cd $1; ls; }
    joem86 · 2009-03-10 19:13:47 6

What Others Think

My 'cd' has evolved into a large script that copes with a lot of careless typing :-) . About the command. It's a little more complicated than it needs to be. First, the [[ test is heavy duty, you don't need it. In fact if you only want to test if $1 exists try if [ -n "$1" ] Remember to always quote your variables. . Next: if there's an arg do "cd; ls", otherwise do "ls". How about: arg => cd, then ls whatever. if [ -n "$1" ]; then cd "$1"; fi; ls; . But bash allows you to abbreviate simple boolean logic: [ -n "$1" ] && cd "$1" . That leaves: cdls() { [ -n "$1" ] && cd "$1"; ls; }
flatcap · 336 weeks and 4 days ago
So much to learn here! Thank you ;)
nnsense · 324 weeks and 4 days ago
any new recent updates with the forum??
Killersmile · 29 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: