Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory with thousands of files. How can I move 100 of the files (any files will do) to another location.

share|improve this question
Because Unix and Linux dont have one big website about its tools, so I simply go to website like about.com and some other website for the list of options available that I can possibly use.. but found nothing like tail – gaijin May 11 '11 at 1:53

10 Answers 10

up vote 23 down vote accepted
for file in $(ls -p | grep -v / | tail -100)
mv $file /other/location
share|improve this answer
Note: ls will output directory names, and will move entire directories if there are fewer than 100 files in the source, and the ls sort defaults are as expected... Here is a safer command find . -type f -maxdepth 1, or to ignore hidden files: find . -type f -maxdepth 1 \( ! -iname ".*" \) – Peter.O May 11 '11 at 2:31
good point, I was assuming only files in the directory. I have now corrected it to account for possible subdirectories – ennuikiller May 11 '11 at 2:42
Note that this approach only works if there are no special characters (whitespace, nonprintable characters, ``) in the file names. As a general rule, do not parse the output of ls. And always double-quote parameter and command substitutions. – Gilles May 11 '11 at 7:20
Which part accounts for 100 files? – Tshepang May 11 '11 at 8:50
Update to my previous comment: After reading Gilles' reference link, do not parse the output of ls, I've found that my find command was lacking. An arg was in the wrong place, and I've added null file-name endings. It is a bit long of a single line, but that's all I can do in a comment. Here is the fixed snippet: find . -maxdepth 1 -type f \( ! -iname ".*" \) -print0 | while read -rd $'\0' file ; do mv -- "$file" /other/location/ ; done – Peter.O May 11 '11 at 12:31

It's easiest in zsh:

mv -- *([1,100]) /other/location/

This moves the first 100 files in name lexicographic order. You can select a different sort order with the o glob qualifier, e.g. to move the 100 oldest files:

mv -- *(Om[1,100]) /other/location/

With other shells, you can do it in a loop with an early exit.

for x in *; do
  if [ "$i" = 100 ]; then break; fi
  mv -- "$x" /other/location/

Another portable way would be to build the list of files and remove all but the last 100.

share|improve this answer
+1 - Interesting about zsh. – boehj May 11 '11 at 11:49
+1 for safe shell expansion. Would also be more readable with the increment operation $(( i++ )) or $[ i++ ]? – user13742 Jan 16 '12 at 12:46
@hesse Some shells don't implement ++ and --. You can write : $((i+=1)) instead of i=$((i+1)); I'm not convinced that it's more readable. – Gilles Jan 16 '12 at 18:18
:-D, I actually edited this answer thinking it was mine... Sorry. Feel free to revert as that changes the meaning. – Stéphane Chazelas Jan 20 '15 at 18:08
@StéphaneChazelas I do wonder why you'd exclude directories and symlinks, the question said nothing about that. – Gilles Jan 20 '15 at 19:02

If you're not using zsh:

set -- *
[ "$#" -le 100 ] || shift "$(($# - 100))"
mv -- "$@" /target/dir

Would move the last (in alphabetical order) 100 ones.

share|improve this answer

The following oneliner in shell would help.

 foreach i (`find Source_Directory -type f --max-depth 1|tail -100`); do; {mv $i Target_Directory}; done
share|improve this answer

mmv is an outstanding utility which will also allow you to do mass renaming of files. (I had to sudo apt-get install mmv on my computer to install it.) Simple usage example: suppose you have a directory of files with extension .JPG that you'd like to change to a lowercase .jpg. The following command does the trick:

mmv \*.JPG \#1.jpg

The backslash is used to show a wildcard is coming up. The */JPG matches anything with a JPG extension. In the "to" portion of the command, the #1 uses the matching text from the first wildcard to rename the file. Of course, you can put a different path before the #1 to also move the file.

share|improve this answer
It would be more beneficial if you provided how you would actually use the tool you suggest to accomplish the goal. – Dason Dec 30 '11 at 5:55
added a usage example – Pete Dec 31 '11 at 2:25

The following worked for me. Sorry if it was posted previously, but I did not see it in a quick scan.

ls path/to/dir/containing/files/* | head -100 | xargs -I{} cp {} /Path/to/new/dir
share|improve this answer

I came by here, but I was needing to copy files in parts (99 each) from /DIR1 to /DIR2. I'll paste the script here to help otherz maybe:

# Thanks to <Jordan_U> @ #ubuntu
# 06 Dec 2014


for file in /DIR1/*; do
  cp "$file" /DIR2
  if [[ "$i" -ge "$copy_unit" ]]; then
    echo "Pausing, press enter to continue"
share|improve this answer
c=1; d=1; mkdir -p NEWDIR_${d}
for jpg_file in *.jpg
if [ $c -eq 100 ]
d=$(( d + 1 )); c=0; mkdir -p NEWDIR_${d}
mv "$jpg_file" NEWDIR_${d}/
c=$(( c + 1 ))

try this code

share|improve this answer

Try this:

find /source/directory -type f -maxdepth 1 -print | tail -100 | xargs -J % mv % /other/location/
share|improve this answer
This is incorrect, you're passing three arguments to mv, the last one of which (probably) isn't a directory. And it doesn't really answer the question - the asker wants to move a given number of files, not all of them. – Mat Dec 30 '11 at 9:13
Command updated. – Saumil Jan 5 '12 at 8:37

I know this thread is a pretty old, but I found the answers more complicated than I thought they should be. This worked in CentOS, but it seems simple enough that it should probably work in other distros.

cp `ls someDir | head -n 100` someDir100/
share|improve this answer
That doesn't work because the output of ls won't include the leading somedir/ prefix, and won't work for filename with blank or wildcard characters or start with -. – Stéphane Chazelas Nov 28 '12 at 14:38
Fair enough. I actually did cp ls | head -n 100 ../someDir100/ From inside the target directory and none of the file names satisfied those cases. Better to be lucky then good! – Jason Nov 28 '12 at 15:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.