I have a folder (watch) that got filled with a lot of temporary files by mistake. I've cleared out all of those files but the the folder is still 356 kB in size. In the past I've moved the folder out of the way, created a new folder with the same name, and copied all the files into it to get it back down to its former small size. Is there any way to get it back down to a small size without recreating the folder?

drwxr-xr-x 2 apache apache   4096 Nov 29  2014 details
drwxr-xr-x 2 apache apache 364544 Jan 21 17:24 watch
drwxr-xr-x 3 apache apache   4096 Jan 21 17:19 settings

watch has two small files: an .htaccess and an index.php.

I have an ext4 filesystem.


e4fsck supports -D flag which seems to do what you want:

try to optimize all directories, either by reindexing them if the filesystem supports directory indexing, or by sorting and compressing directories for smaller directories, or for filesystems using traditional linear directories.

Of course, you'll need to unmount the filesystem to use fsck, meaning downtime for your server.

You'll want to use the -f option to make sure e4fsck processes the file system even if clean.


# truncate -s1G a; mkfs.ext4 -q ./a; mount ./a /mnt/1
# mkdir /mnt/1/x; touch /mnt/1/x/{1..4000}
# ls -ld /mnt/1/x
drwxr-xr-x 2 root root 69632 Nov 22 12:54 /mnt/1/x/
# rm -f /mnt/1/x/*
# ls -ld /mnt/1/x
drwxr-xr-x 2 root root 69632 Nov 22 12:55 /mnt/1/x/
# umount /mnt/1
# e2fsck -f -D ./a
e2fsck 1.43.3 (04-Sep-2016)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 3A: Optimizing directories
Pass 4: Checking reference counts
Pass 5: Checking group summary information

./a: 12/65536 files (0.0% non-contiguous), 12956/262144 blocks
# mount ./a /mnt/1
# ls -ld /mnt/1/x
drwxr-xr-x 2 root root 4096 Nov 22 12:55 /mnt/1/x/
  • @StéphaneChazelas Thanks for testing it for me! Would it be too blunt if I asked to check e2fsck -f -D? Perhaps is saw a clean journal and decided there was nothing to do... – Dmitry Grigoryev Nov 22 '16 at 12:38
  • 1
    D'oh! my bad. It did appear to do something, but didn't indeed. With -f, it does bring the directory size down indeed. – Stéphane Chazelas Nov 22 '16 at 12:50
  • @StéphaneChazelas Thanks for being so helpful, Stéphane! – Dmitry Grigoryev Nov 22 '16 at 13:19
  • 1
    Thank you so much! This has always been one of those pet peeves of mine. While unmounting the filesystem to do this isn't always an option, I'm glad to know that there's a clean way to clean those folder sizes up. – Austin Burk Nov 22 '16 at 17:00

The answer to your question is for all practical purposes no. ext4 doesn't truncate directories, so you need to recreate them. You can improve on your previous technique by moving rather than copying the files to the new directory, this will probably be a lot faster and will only alter the ctime of the files.

If there are no subdirectories you could even use ln to hard link the files into the new directory.

Linux has a renameat2 system call which can be used to swap two names, so again assuming that you have no subdirectories you could create your new directory, link the files from the old to the new and then swap the old and new directories. A quick google came up with https://gist.github.com/eatnumber1/f97ac7dad7b1f5a9721f as example code for calling renameat2.

Of course everything is only data so you could use something like debugfs or even dd to edit the data structures directly on the disk. I would not suggest going down this route for the sake of 360KB of space.


using vim you can "edit" the directory. removing all but the ./ and ../ should "reset" the folders metadata

My downloads directory is 12k

vim Downloads/

" ============================================================================                                                                                                                                            
" Netrw Directory Listing                                        (netrw v150)
"   /home/harry/Downloads
"   Sorted by      name
"   Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
"   Quick Help: <F1>:help  -:go up dir  D:delete  R:rename  s:sort-by  x:exec
" ============================================================================
Moderncv Casual/

Here is an example of a directory that is the usual 4k vim Videos/

" ============================================================================                                                                                                                                       
" Netrw Directory Listing                                        (netrw v150)
"   /home/harry/Videos
"   Sorted by      name
"   Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
"   Quick Help: <F1>:help  -:go up dir  D:delete  R:rename  s:sort-by  x:exec
"  ============================================================================
EnVyUs vs NiP, Mirage - FACEIT Stage 3 Finals at Dreamhack Winter - Group B-O3pBF-3KmzM.mp4
The Tek 0201 - Is USA Like Nazi Germany-zrOo8LzvKvc.mp4
The Tek 0203 - YT Red, EU Net Neutrality Trouble, Drones Taking  Jobs-IEdUcjBGyEw.mp4.part
The Tek 0204 - Logan's Favorite Episode of the Year-iga7kB1NhKY.mp4
VP vs TSM, Mirage - FACEIT Stage 3 Finals at Dreamhack Winter - Group A Winners Match-3jfepl5Of0o.mp4

My understanding of the matter is that Directories under Linux (or *Nix) are just "special" files that can store information about files that live "below" it. I found on my system only vim could do this, using nano or others just showed a blank file, I guess the way it reads things differently

Here is a nice stackexchange question that answers what folders (directorys) are under Linux How are directories implemented in Unix filesystems?

  • Sorry for the late reply, buuut removing all but .. and . involves removing the two vital files with the folder that I need to keep there. There's only two files in there, anyway...so in short, it doesn't work to reset the size of the folder itself. – Austin Burk Nov 16 '16 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.