Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
3  
What have you tried? How did it fail? –  pdo May 11 '11 at 1:37
    
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

7 Answers 7

up vote 9 down vote accepted
for file in $(ls -p | grep -v / | tail -100)
do
mv $file /other/location
done
share|improve this answer
2  
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
8  
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
1  
Which part accounts for 100 files? –  Tshepang May 11 '11 at 8:50
2  
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

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

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
1  
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

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

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
1  
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 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

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.

i=0
for x in *; do
  if [ "$i" = 100 ]; then break; fi
  mv -- "$x" /other/location/
  i=$(($i+1))
done
share|improve this answer
4  
+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++ ]? –  Herman Torjussen Jan 16 '12 at 12:46
1  
@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

Your Answer

 
discard

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.