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.
command linelsshell-script
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.
Best Answer
Got GNU?
The gnu version of
ls
has--group-directories-first
. Andcp
has-t
.No GNU?
On systems that don't have gnu's
ls
, your best bet is two successive calls tofind
with-maxdepth n
/-mindepth n
and-type t
with the appropriate options.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.Watch Out!
If you were planning on using these together - that is, collecting the list from
find
orls
(possibly by usingxargs
) and passing it tocp
(or acp
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 intofind
's-exec
and-print0
options andxargs
'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.