Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Move files to subdi...
 
Share:
Notifications
Clear all

[Solved] Move files to subdirectories in fixe groups by size

1 Posts
1 Users
0 Reactions
1,041 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2728
Topic starter  

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


   
ReplyQuote
Share: