I have many files that are ordered by file name in a directory. I wish to copy the final N (say, N=4) files to my home directory. How should I do it?
cp ./<the final 4 files> ~/
I have many files that are ordered by file name in a directory. I wish to copy the final N (say, N=4) files to my home directory. How should I do it?
cp ./<the final 4 files> ~/
This can be easily done with bash/ksh93/zsh arrays:
a=(*)
cp -- "${a[@]: -4}" ~/
This works for all non-hidden file names even if they contain spaces, tabs, newlines, or other difficult characters (assuming there are at least 4 non-hidden files in the current directory with bash
).
a=(*)
This creates an array a
with all the file names. The file names returned by bash are alphabetically sorted. (I assume that this is what you mean by "ordered by file name.")
${a[@]: -4}
This returns the last four elements of array a
(provided the array contains at least 4 elements with bash
).
cp -- "${a[@]: -4}" ~/
This copies the last four file names to your home directory.
This will copy the last four files only to the home directory and, at the same time, prepend the string a_
to the name of the copied file:
a=(*)
for fname in "${a[@]: -4}"; do cp -- "$fname" ~/a_"$fname"; done
If we use a=(./some_dir/*)
instead of a=(*)
, then we have the issue of the directory being attached to the filename. One solution is:
a=(./some_dir/*)
for f in "${a[@]: -4}"; do cp "$f" ~/a_"${f##*/}"; done
Another solution is to use a subshell and cd
to the directory in the subshell:
(cd ./some_dir && a=(*) && for f in "${a[@]: -4}"; do cp -- "$f" ~/a_"$f"; done)
When the subshell completes, the shell returns us to the original directory.
The question asks for files "ordered by file name". That order, Olivier Dulac points out in the comments, will vary from one locale to another. If it is important to have fixed results independent of machine settings, then it is best to specify the locale explicitly when the array a
is defined. For example:
LC_ALL=C a=(*)
You can find out which locale you are currently in by running the locale
command.
If you are using zsh
you can enclose in parenthesis ()
a list of so called glob qualifiers which select desired files. In your case, that would be
cp *(On[1,4]) ~/
Here On
sorts file names alphabetically in reverse order and [1,4]
takes only first 4 of them.
You can make this more robust by selecting only plain files (excluding directories, pipes etc.) with .
, and also by appending --
to cp
command in order to treat files which names begin with -
properly, so:
cp -- *(.On[1,4]) ~
Add the D
qualifier if you also want to consider hidden files (dot-files):
cp -- *(D.On[1,4]) ~
on
) is default behaviour, so no need to specify this, and -
in front of the numbers counts filenames from the bottom.
– jimmij
Jan 26 '15 at 13:23
Here is a solution using extremely simple bash commands.
find . -maxdepth 1 -type f | sort | tail -n 4 | while read -r file; do cp "$file" ~/; done
Explanation:
find . -maxdepth 1 -type f
Finds all files in current directory.
sort
Sorts alphabetically.
tail -n 4
Only show last 4 lines.
while read -r file; do cp "$file" ~/; done
Loops over each line performing the copy command.
So long as you find the shell sort agreeable, you can just do:
set -- /path/to/source/dir/*
[ "$#" -le 4 ] || shift "$(($#-4))"
cp "$@" /path/to/target/dir
This is very similar to the bash
-specific array solution offered, but should be portable to any POSIX-compatible shell. Some notes about both methods:
It is important that you lead your cp
arguments w/ cp --
or you get one of either .
a dot or /
at the head of each name. If you fail to do this you risk a leading -
dash in cp
's first argument which can be interpreted as an option and cause the operation to fail or to otherwise render unintended results.
set -- ./*
or array=(./*)
.It is important when using both methods to ensure you have at least as many items in your arg array as you attempt to remove - I do that here with a math expansion. The shift
only happens if there are at least 4 items in the arg array - and then only shifts away those first args that make a surplus of 4..
set 1 2 3 4 5
case it will shift the 1
away, but in a set 1 2 3
case, it will shift nothing.a=(1 2 3); echo "${a[@]: -4}"
prints a blank line.If you are copying from one directory to another, you can use pax
:
set -- /path/to/source/dir/*
[ "$#" -le 4 ] || shift "$(($#-4))"
pax -rws '|.*/|new_prefix|' "$@" /path/to/target/dir
...which would apply a sed
-style substitution to all filenames as it copies them.
If there are only files and their names do not contain whitespace or newline (and you've not modified $IFS
) or glob characters (or some non-printable characters with some implementations of ls
), and don't start with .
, then you can do this:
cp -- $(ls | tail -n 4) ~/
a_
to ALL four files? I tried echo "a_$(ls | tail -n 4)"
, but it only prepends the first file.
– Sibbs Gambling
Jan 26 '15 at 06:29
ls
output is considered bad form because it typically fails horribly on file names that contain not only spaces, but also tabs, newlines, or other valid but "difficult" characters.
– HBruijn
Jan 26 '15 at 06:56
"${a[@]: -4}"
(which not even the author of that answer knew).
– Hauke Laging
Jan 26 '15 at 08:49
This is a simple bash solution without any loop code. Copy the last 4 files from the current dir to destination:
$ find . -maxdepth 1 -type f |tail -n -4|xargs cp -t "$destdir"
find
gives you the files in the wanted order? How do you ensure that files containing whitespace characters (including newlines) in their names are handled correctly?
– Kusalananda
Jan 22 '19 at 17:20
LC_ALL=C
– Olivier Dulac Jan 26 '15 at 13:05