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.
The gnu version of
On systems that don't have gnu's
ls, your best bet is two successive calls to
-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.
#!/bin/sh target="$1" shift cp -r -- "$@" "$target"
If you were planning on using these together - that is, collecting the list from
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
-print0 options and
You might want to look into using
rsync instead; it has lots of functionality that might make your job easier.
Listing directories first for a non-GNU
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
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
just install it with antigen, by putting the following into ~/.zshrc
antigen bundle reduxionist/k --branch=add-sort-options