Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Okay guys, this is an experimental question warning!

I often confuse cat and ls when quickly working on the console. Not because I forget their meanings, no. Just because I type without deeply thinking about the command - it just comes automatically. I am sure everyone knows what I am trying to explain: you don't have to think what to type if your task is to list, say, a text file - or a directory.

Now, why two different commands? Thinks would be more fluently if there was a command that lists a directory if the target is a directory and that lists the content of a file if the target is a file.

Does such a command exist?

I know there are editors (e. g. vim) with the ability to list a directory. But this is not what I want, as I want to stay on the console. Second, after opening a file through the vim directory listing there is no way back - at least none that I know.

If there is no out-of-the-box solution I will try to make a small bash script (wrapper) to cat and ls. But this is not preferred, because when working with many different systems it has to be placed on each of them...

share|improve this question
Emacs is having a mode for shell and for directory browsing ... (SCNR) –  frlan Apr 16 '14 at 18:41
Did you read my paragraph about the vim directory browsing mode? If yes, is emacs different/better here? In the end I don't want to leave the console into an application for actions beyond the cat/ls scope. –  Nicolas Apr 16 '14 at 20:47

1 Answer 1

up vote 2 down vote accepted

Simply put this function is your .bashrc:

  [[ -f "$1" ]] && cat "$1" || ls "$1";

It will cat regular files but call ls for directories

share|improve this answer
if this is for bash, then use [[ ... ]] instead of test <TEST> or [ ... ]. [ ... ] and test <TEST> are deprecated syntax and should only be used when [[ ... ]] is not available. –  Alexej Magura Apr 16 '14 at 21:36
@AlexejMagura, thanks for the deprecation notice, I've updated my answer –  Sylvain Pineau Apr 16 '14 at 21:39
Also note, that if $1 is a file or path containing a space: this function will break because $1 is not double quoted. –  Alexej Magura Apr 16 '14 at 21:41
@AlexejMagura: Good point –  Sylvain Pineau Apr 16 '14 at 21:42
This is soooo awesome that I exagerated Sylvain's function body to [[ $# -gt 1 ]] && cp "$1" "$2" || ([[ -f "$1" ]] && cat "$1" || ([[ $# -ge 1 ]] && ls -l "$1" || ls -l)); - this is awesome, crazy and magic at once! :D –  Nicolas Apr 17 '14 at 23:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.