6
votes
Copy the n-largest files from a certain directory to the current one
Using zsh you can avoid all the pitfalls associated with parsing and sorting the output of ls:
cp -n -- specific/directory/*(.DOL[1,5]) ./
or with GNU cp (for the -toption):
cp -n -t ./ -- specific/...
6
votes
Copy the n-largest files from a certain directory to the current one
All my solutions treat only files, as requested, and can treat all type of files
(even with special characters).
If you want to use ls -S
do it the right way:
ls --zero -S | head -z -n5 | xargs -r0 cp ...
2
votes
Accepted
Copy the n-largest files from a certain directory to the current one
To integrate the other answers:
TL; DR: See below for viable solutions for bash and POSIX shells.
Why is the pipe working for the first item and failing for the rest?
Because the shell does not ...
2
votes
Copy the n-largest files from a certain directory to the current one
EDIT: New answer, works more completely:
The reason the original fails is that the directory name is added only to the first result, so the remaining results, not existing in the current directory, ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
ls × 1319bash × 211
linux × 154
shell × 148
files × 146
find × 127
directory × 107
command-line × 95
colors × 89
wildcards × 85
shell-script × 79
grep × 66
sort × 64
permissions × 54
filesystems × 46
macos × 44
terminal × 37
symlink × 36
filenames × 35
timestamps × 31
zsh × 30
disk-usage × 30
pipe × 26
awk × 24
xargs × 23