You can do things like this using command substitution. In Bash:
mv $(ls -d [sort options] source/* | tail -n8) destination
The $(command)
will run whatever is enclosed in it and substitute the text output into the outer command. That ls
command will print the path to each file in the source directory sorted according the the flags you specify, one per line, so tail
can just take the last few. Thus the above would expand to
mv source/file1 source/file2 source/file3 ... source/file8 destination
You can just run ls -d [sort options] source/* | tail -n8
to see what files it will copy.
Depending on how you're ordering the output of ls
and the file naming, you may be able to do what you're looking for more simply just using some variant of mv source/name_* destination
to copy everything starting with "name_" to the destination directory.
EDIT:
The above breaks when there are spaces in the file names. A more complicated alternative that addresses this would be
ls -d1 [sort options] source/* | tail -n8 | tr '\n' '\0' | xargs --null mv -t destination
though it still makes use of parsing ls
output to get sorting in any order that isn't alphabetical.
mv dir1/* dir2
.ls
?Documents
directory are mixed up and not well organised. So, for instance, I have createdCurriculum_Vitae
directory in theDocuments
directory (i.e. ./Documents/Curriculum_Vitae) and I want to put all files related to the subject in that directory. However, there are a lot of those file and I don't want to move them one-by-one.