Sunday, October 19, 2014

ubuntu - Read files inside directory and group them on new sub-directory (creating the subdir and renaming the file)


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


000001.jpg
000002.jpg
...
000020.jpg

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


page_0001

Repeat until all 400000 have been processed!


page_0002/000021.jpg
page_0002/000022.jpg
...
page_0002/000040.jpg

Answer



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


Change with your needs:


#/bin/bash
target_prefix=page
SOURCE_DIR=YOUR_DIR #Change this to source dir
N=0
Z=0
for entry in $SOURCE_DIR/*
do
N=$((N+1))
Z=$((Z+1))
if [ "$N" == "1" ]; then
mkdir $SOURCE_DIR/$target_prefix-$Z
DIR="$SOURCE_DIR/$target_prefix-$Z"
fi
echo "Move image $entry number $N to dir $DIR"
mv $entry $DIR/$Z.jpg #Change to your file extension
if [ "$N" == "20" ]; then
N=0
fi
done

Hope this work.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...