1

In my parent directory I have 78,160 files.
File names are: sb_604_dpm_0089000.dpx, sb_604_0089001.dpx etc.

I want to move the files to 4 sub directories subdir1, subdir2, subdir3, subdir4. There must be exactly 20,000 files in each directory in sequential order.

Is it to possible to move a certain range of files from the parent directory to the sub-directories i.e., sb_604_dpm_0089000.dpx to sb_604_dpm_0108,999.dpx files to subdir1 sb_604_dpm_0109000.dpx to sb_604_dpm_0128,999.dpx files to subdir2 etc.

The final folder will only contain the remaining files after the first 60,000.

All of these should be done using a batch file, ideally being able to specify/check the range before the move proceeds. Is this possible?

In DOS I would probably be able to work this out for myself writing a batch file, but with Linux I'm a little lost... I'm not a programmer, I'm just a linux user looking for faster/better ways to do this rather than the GUI, so any help appreciated. Please keep it as simple as possible!

Thanks in advance!

1
  • I will write you a quick script... one sec May 11, 2018 at 17:57

2 Answers 2

5

If it's a one-time task, you could use shell expansion like so:

mkdir subdir1 subdir2 subdir3 subdir4
mv sb_604_dpm_{0089000..0108999}.dpx subdir1
mv sb_604_dpm_{0109000..0128999}.dpx subdir2
mv sb_604_dpm_{0129000..0148999}.dpx subdir3
mv sb_604_dpm_*.dpx                  subdir4

{1..5} expands to 1 2 3 4 5, so the above commands expand to the first/second/third 20,000 files, and the last line handles the rest.

2
  • That was quick - thank-you PerlDuck!
    – rich
    May 11, 2018 at 18:45
  • +1 because OP will be more likely to understand it than something more fancy. May 11, 2018 at 21:05
2

Something like this little script should do it for you:

#!/bin/bash

files=(*.dpx)
folder=1
count=0

if [ ! -d "folder$folder" ]; then
   mkdir "folder$folder"
fi

for i in "${files[@]}"
do
   mv "$i" "folder$folder"
   let count=$count+1
   if [ "$count" == "20000" ]; then
      let count=0
      let folder=$folder+1
      if [ ! -d "folder$folder" ]; then
        mkdir "folder$folder"
      fi
   fi
done

Put script into a file inside of the folder with the files you want to move (eg folder in your home folder named "my-files") and name the file "move". Then from the command line:

$ cd ~/my-files
$ chmod 755 move
$ ./move
4
  • +1 Because this is way more flexible than my hardcoded suggestion. It might be a bit slow because mv is called 78,160 times (instead of 4 times). But an old rule says: don't optimize before profiling.
    – PerlDuck
    May 11, 2018 at 18:15
  • Out of curiosity I just measured: 1) create 20k files: touch sb_604_dpm_{0089000..0108999}.dpx 2) move one by one: time for i in sb_604_dpm_{0089000..0108999}.dpx; do mv $i subdir1/; done gives 25 seconds. 3) move all at once: time mv sb_604_dpm_{0089000..0108999}.dpx subdir1/ gives 0.4 seconds. But still: If it's a one-time task I wouldn't care. I just spent more time diagnosing than it actually took.
    – PerlDuck
    May 11, 2018 at 18:30
  • 1
    This is def not the fastest solution, but it works ;) May 11, 2018 at 18:31
  • Thank-you Joshua. Sorry to ask a basic question, but how do I implement that script...? In DOS I just used to save it as a batch file and double click - what is the Linux equivalent?
    – rich
    May 11, 2018 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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