2

I'm looking for a command to list all files in a directory with ls command.

Requirements: - Alphabetically - Folders first, then files (both alphabetically) - No timestamps or other information, just the plain names

I've been experimenting and assembled this command but it's nowhere close to what I need:

ls -la | rev | cut -d' ' -f 1| rev

I would like an output like this (folders need to have a blue color):

folder_A
folder_B
folder_C

file_A  
file_B 
file_C
  • @Rinzwind Yes, that's almost what I need. I've updated my question to make it more clear what I need. So now I need to get rid off all the info like owner, timestamp, ... Because of the grep, the 'd' is colored red. I want all directory names to be colored in blue (or other color). – Michiel Mulders Aug 28 '17 at 11:45
  • Yes that's a good answer as well! Thanks! Works as I wanted! Do you think it possible to add a blank line? – Michiel Mulders Aug 28 '17 at 11:52
4

At least with recent versions of ls, you can add the option --group-directories-first to get a listing with directories above plain files. Your ls command is probably already aliased to use --color=auto but if not, you can add that (you could use --color=always, but that may cause undesirable results if you ever use the command in a pipe). Finally, -1 causes the output to be presented in a single column. So the full command would be

ls -1 --color=auto --group-directories-first
  • Create an alias in ~/.bashrc to make the entire expression accessible via a short custom command. For example append this line (alias dir="ls -1 --color=auto --group-directories-first") to the end of ~/.bashrc; then source ~/.bashrc and you will be able to use dir instead of the entire expression from the answer. – pa4080 Aug 30 '17 at 19:06
0

@Bruni answered with the tree command but removed it? This was actually a good answer, I dug into the man and found the solution: tree -L 1 --dirsfirst

This is the output:

.
├── docs
├── media
├── node_modules
├── src
├── test
├── API.md
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.rst
├── LICENSE
├── package.json
├── README.md
├── webpack.config.js
└── yarn.lock

5 directories, 8 files

How to get rid of the line '5 dirs, 8 files' without losing the color coding on the folders?

Edit: @steeldriver, thanks for your reply as well: ls -1 --color=auto --group-directories-first

  • 1
    To omit the '5 dirs, 8 files' summary, try adding --noreport – steeldriver Aug 28 '17 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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