<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Move files to subdirectories in fixe groups by size - Linux Software				            </title>
            <link>https://www.tweaking4all.com/forum/linux-software/move-files-to-subdirectories-in-fixe-groups-by-size/</link>
            <description>Tweaking4All.com Discussion Board</description>
            <language>en-US</language>
            <lastBuildDate>Sat, 13 Jun 2026 21:34:37 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>Move files to subdirectories in fixe groups by size</title>
                        <link>https://www.tweaking4all.com/forum/linux-software/move-files-to-subdirectories-in-fixe-groups-by-size/#post-4143</link>
                        <pubDate>Mon, 25 Apr 2022 12:14:34 +0000</pubDate>
                        <description><![CDATA[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 sho...]]></description>
                        <content:encoded><![CDATA[<p>I had a directory with hundreds of files, which I wanted to move to several sub directories.<br />In this case: each subdirectory should have a sequential and unique number, and each directory should contain up to 100 files.<br />Directories should be created automatically ... </p>
<p>Basically from this:</p>
<pre contenteditable="false">dir1/
  file-1.txt
  file-2.txt
  ...
  file-n.txt</pre>
<p> </p>
<p>to:</p>
<pre contenteditable="false">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...</pre>
<p> </p>
<p>The following script does just that:</p>
<pre contenteditable="false">DirCounter=0; 
maxPerDir=50;

for f in *; 
do 
    d=$(printf %03d $((DirCounter/maxPerDir+1))); 
    mkdir -p $d; 
    mv "$f" $d; 
    let DirCounter++; 
done</pre>
<p> </p>
<p>Well, I did actually want the dirs to be filled with the largest files first, which comes with a bundle of problems.</p>
<p>To list files by size ("S" option), including linked files ("L" option), I used:</p>
<pre contenteditable="false">ls -SL1</pre>
<p>Note: option "1" is only needed when running this in a shell (versus a script) to make sure each filename uses one line.</p>
<p>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.</p>
<pre contenteditable="false">ls -SL1 &gt; ../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</pre>
<p> </p>
<p>Note: I placed files.txt one directory up, so it wouldn't be moved to one of the new sub directories.</p>
<p>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.</p>
<p>This script will go through "/share/Multimedia" and all its subdirectories, finding FILES with a filesize of about 1,8 Gb or bigger.</p>
<p>find /share/Multimedia/* -type f -size +1800000k -exec ln -vs "{}" ';'</p>
<p>All this combined:</p>
<p>1. Create a directory to placed the linked files (the original files are just linked here!)<br />2. Create links to all files that are about 1.8Gb or larger in size<br />3. Sort all files by size<br />4. Per 50 files, create a subdirectory and move the 50 largest files there<br />5. Repeat step 4 until we're out of files (end of files.txt).</p>
<pre contenteditable="false">mkdir MyLinkedVideos
cd MyLinkedVideos

find /share/Multimedia/* -type f -size +1800000k -exec ln -vs "{}" ';'

ls -SL1 &gt; ../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</pre>
<p> </p>
<p>This way I was able to toss certain amounts of files on Handbrake and make batches ... <br />Hope it's helpful for someone ...</p>]]></content:encoded>
						                            <category domain="https://www.tweaking4all.com/forum/linux-software/">Linux Software</category>                        <dc:creator>Hans</dc:creator>
                        <guid isPermaLink="true">https://www.tweaking4all.com/forum/linux-software/move-files-to-subdirectories-in-fixe-groups-by-size/#post-4143</guid>
                    </item>
							        </channel>
        </rss>
		