Super User is a question and answer site for computer enthusiasts and power users. 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've performed a recovery for old deleted files! The process was a complete success and recovered files from years ago.

Now the problem is going through all the 400000 files recovered of the type JPG alone.

To deal with this, I've already separated the files by their size and stored the ones that match the criteria on a different directory:

find /path/to/files/ -size +100k -print0 | xargs -0 cp --target-directory=/path/to/filtered/files/

Since the files are to be analyzed remotely, I've prepared a web page to show all files, allowing them to be saved locally. This web page will present the files 20 at a time with navigation arrows!

My question is how to split the 400000 files into sub-directories containing 20 files each, and have the files renamed sequentially:

Rename to


Move into sub-directory, by creating the sub-directory


Repeat until all 400000 have been processed!

share|improve this question
… renamed sequentially based on what? Their modification time? Their alphanumerical sort order? – slhck May 24 '12 at 12:33
@slhck, no particular specification for the rename, just giving some "decent" name to the file related with later processing issues, so, basically, renamed as they are read! – Zuul May 24 '12 at 13:11
Why do you need to have them in subdirectories? – Daniel Andersson May 25 '12 at 7:30
up vote 2 down vote accepted

I have the same problem. I write this code in BASH, and worked for me.

Change with your needs:



SOURCE_DIR=YOUR_DIR #Change this to source dir


for entry in $SOURCE_DIR/*

    if [ "$N" == "1" ]; then
        mkdir $SOURCE_DIR/$target_prefix-$Z

    echo "Move image $entry number $N to dir $DIR"

    mv $entry $DIR/$Z.jpg #Change to your file extension

    if [ "$N" == "20" ]; then

Hope this work.

share|improve this answer
I'll give it a try and get back to you soon! – Zuul Jun 6 '12 at 0:05
Tks, did the job pretty well! Didn't addressed the lending zeros issue, but I can deal with that at a later stage! Tks Again. – Zuul Jun 6 '12 at 9:59

The following Bash script should do the trick, but no guarantees.

for f in *
  fn=$(printf "%.6d" $i).jpg
  dn=page_$(printf "%.4d" $(($i%20)))
  mkdir -p $dn
  cp $f "$dn/$fn"
share|improve this answer
I'll try it out and give you some feedback! – Zuul May 24 '12 at 12:21
for f in * might not work with 400000 (!) files. That's just too many arguments to handle. Consider looping over find . -print0 results piping into while read -d '' -r file or similar. – slhck May 24 '12 at 12:26
Fixed the parentheses. I didn't try running this, as I didn't have a suitable set of files. – Alex Chamberlain May 24 '12 at 12:34
I've tried this, but it was issuing an unspecified error... Can't really tell what the error was :( – Zuul Jun 6 '12 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .