I'm trying to copy specific files between directories. So I listed all files with ls
, added a line number with cat -n
and then selected first 100 files I want to copy with head -100
. After that I used xargs
command but it does not work. Here is the code:
ls * | cat -n | head -100 | xargs -0 cp -t ~/foo/bar
The command reproduces a list of files on the screen and returns the warning File name too long
.
I have tried also with -exec cp -t
and its returns the message -bash: -exec: command not found
.
Edit: My filenames contain years (e.g. 1989a, 1989b,1991a, 1992c) so I would like to select all files published before a certain year (e.g. 1993).
This would result in 100 invocations of cp
but you could just use a loop:
count=0; for i in *; do cp "$i" ~/foo/bar; ((++count == 100)) && break; done
Another way you could do this would be to use an array:
files=( * )
cp "${files[@]:0:100}" ~/foo/bar
The glob *
expands to fill the array with the list of files in the current directory. The first 100 elements of the array are then copied.