Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disk usage of files whose names match a regex, in Linux?

So, in many situations I wanted a way to know how much of my disk space is used by what, so I know what to get rid of, convert to another format, store elsewhere (such as data DVDs), move to another partition, etc. In this case I'm looking at a Windows partition from a SliTaz Linux bootable media.

In most cases, what I want is the size of files and folders, and for that I use NCurses-based ncdu:

                ncdu

But in this case, I want a way to get the size of all files matching a regex. An example regex for .bak files:

.*\.bak$ 

How do I get that information, considering a standard Linux with core GNU utilities or BusyBox?

Edit: The output is intended to be parseable by a script.

like image 621
Camilo Martin Avatar asked Feb 28 '12 16:02

Camilo Martin


People also ask

How do I analyze disk usage in Linux?

Linux commands to check disk space using:df command – Shows the amount of disk space used and available on Linux file systems. du command – Display the amount of disk space used by the specified files and for each subdirectory.

How do I check disk usage on all users in Linux?

Check Disk Usage in Linux Using the du Commanddu /home/user/Desktop — this command line allows users to see into the disk usage of their Desktop folders and files (subdirectories are included as well). du -h /home/user/Desktop — just like with df, the option -h displays information in a human-readable format.


1 Answers

I suggest something like: find . -regex '.*\.bak' -print0 | du --files0-from=- -ch | tail -1

Some notes:

  • The -print0 option for find and --files0-from for du are there to avoid issues with whitespace in file names
  • The regular expression is matched against the whole path, e.g. ./dir1/subdir2/file.bak, not just file.bak, so if you modify it, take that into account
  • I used h flag for du to produce a "human-readable" format but if you want to parse the output, you may be better off with k (always use kilobytes)
  • If you remove the tail command, you will additionally see the sizes of particular files and directories

Sidenote: a nice GUI tool for finding out who ate your disk space is FileLight. It doesn't do regexes, but is very handy for finding big directories or files clogging your disk.

like image 50
Michał Kosmulski Avatar answered Sep 20 '22 17:09

Michał Kosmulski