I had a directory with hundreds of files, which I wanted to move to several sub directories.
In this case: each subdirectory should have a sequential and unique number, and each directory should contain up to 100 files.
Directories should be created automatically ...
Basically from this:
dir1/
file-1.txt
file-2.txt
...
file-n.txt
to:
dir1/
dir001/
file-1.txt
file-2.txt
...
file-50.txt
dir002/
file-51.txt
file-52.txt
...
file-100.txt
dir003/
file-101.txt
file-102.txt
...
file-150.txt
dir004/
...etc...
The following script does just that:
DirCounter=0;
maxPerDir=50;
for f in *;
do
d=$(printf %03d $((DirCounter/maxPerDir+1)));
mkdir -p $d;
mv "$f" $d;
let DirCounter++;
done
Well, I did actually want the dirs to be filled with the largest files first, which comes with a bundle of problems.
To list files by size ("S" option), including linked files ("L" option), I used:
ls -SL1
Note: option "1" is only needed when running this in a shell (versus a script) to make sure each filename uses one line.
The for-loop didn't like that very much, so I pushes all the filenames to a text file and used "cat" to loop through the files.
ls -SL1 > ../files.txt
DirCounter = 0;
maxPerDir = 50;
cat ../files.txt | while read f
do
d=$(printf %03d $((DirCounter/maxPerDir+1)));
mkdir -p $d;
mv "$f" $d;
let DirCounter++;
done
rm ../files.txt
Note: I placed files.txt one directory up, so it wouldn't be moved to one of the new sub directories.
Since it was intended to grab and recompress some of my video files, I'll also share the line I used to grab all files larger than a certain size, and link it in the current directory.
This script will go through "/share/Multimedia" and all its subdirectories, finding FILES with a filesize of about 1,8 Gb or bigger.
find /share/Multimedia/* -type f -size +1800000k -exec ln -vs "{}" ';'
All this combined:
1. Create a directory to placed the linked files (the original files are just linked here!)
2. Create links to all files that are about 1.8Gb or larger in size
3. Sort all files by size
4. Per 50 files, create a subdirectory and move the 50 largest files there
5. Repeat step 4 until we're out of files (end of files.txt).
mkdir MyLinkedVideos
cd MyLinkedVideos
find /share/Multimedia/* -type f -size +1800000k -exec ln -vs "{}" ';'
ls -SL1 > ../files.txt
DirCounter=0;
maxPerDir=50;
cat ../files.txt | while read f
do
d=$(printf %03d $((DirCounter/maxPerDir+1)));
mkdir -p $d;
mv "$f" $d;
let DirCounter++;
done
rm ../files.txt
This way I was able to toss certain amounts of files on Handbrake and make batches ...
Hope it's helpful for someone ...