I have two questions. First, which command lists files and directories, but lists directories first?

Second question: I want to copy a list of files into a single directory, but make the target directory the first filename in the command.

  • 3
    ls has a --group-directories-first switch. – jasonwryan Oct 18 '11 at 0:27
  • 1
    -t is a possibly a GNU-only option to cp that takes an explicit target directory and hence can go anywhere in the command line, including the beginning. – jw013 Oct 18 '11 at 1:23

Got GNU?

The gnu version of ls has --group-directories-first. And cp has -t.


On systems that don't have gnu's ls, your best bet is two successive calls to find with -maxdepth n/-mindepth n and -type t with the appropriate options.

find . -maxdepth 1 -mindepth 1 -type d
find . -maxdepth 1 -mindepth 1 \! -type d

For copying files, with the target first, you would have to write a script that saves the first argument, then uses shift, and appends the argument to the end.

cp -r -- "$@" "$target"

Watch Out!

If you were planning on using these together - that is, collecting the list from find or ls (possibly by using xargs) and passing it to cp (or a cp wrapper), you should be aware of what dangers lie in parsing lists of files (basically, filenames can contain characters like newlines that can mess up your script). Specifically, look into find's -exec and -print0 options and xargs's -0 option.

An alternative tool for efficiently copying directory trees.

You might want to look into using rsync instead; it has lots of functionality that might make your job easier.

  • This is great. More generally, is there any way of knowing if I have the GNU version? (other than trying the option --group-directories-first). – Amelio Vazquez-Reina Sep 25 '12 at 21:02
  • 3
    You can run ls --version to find out which one you have. – Shawn J. Goff Sep 26 '12 at 12:26

Listing directories first for a non-GNU ls:

ls -l | sort

Note, this will list all the bunch of other weird stuff like symlinks, sockets and pipes as well (appropriately grouped, of course), but considering that stuff is quite rare that shouldn't be a problem. Otherwise the filter would be ls -l | grep '^(-|d)' | sort

  • 1
    +1 clever and shorter than the accepted answer. Except that it really should be sort -r. – PythonNut Oct 2 '14 at 4:42
  • 1
    ls -l | sort didn't work for me on a Mac. It groups the directories first but it didn't sort them nor did it sort the filenames that followed. – Jim Jun 10 '17 at 22:12
  • also note: using the ` | sort` will remove your color as it is now text. – JayRizzo May 10 '18 at 5:01

works for bash and zsh

just add alias in ~/.bashrc or ~/.zshrc

# Colorize the ls output ##
alias ls='LC_COLLATE=C ls -h --group-directories-first --color=auto'

Also if you have ZSH, you can use "k" utility

k -h --group-directories-first

enter image description here

just install it with antigen, by putting the following into ~/.zshrc

antigen bundle reduxionist/k --branch=add-sort-options
  • 1
    nice answer to offer the alias. – рüффп Jul 25 '18 at 9:28

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.