One line Perl Script to determine the largest file sizes on a Linux Server

du -k | sort -n | perl -ne 'if ( /^(\d+)\s+(.*$)/){$l=log($1+.1);$m=int($l/log(1024)); printf ("%6.1f\t%s\t%25s %s\n",($1/(2**(10*$m))),(("K","M","G","T","P")[$m]),"*"x (1.5*$l),$2);}' | more
This one line Perl script will display the smallest to the largest files sizes in all directories on a server.
Sample Output
4.0	K	                       **  ./bin
   4.0	K	                       **  ./.config/gnome-main-menu
   4.0	K	                       **  ./.config/.mono/certs/Trust
   4.0	K	                       **  ./Desktop
   4.0	K	                       **  ./.fvwm
   4.0	K	                       **  ./.gconfd
   4.0	K	                       **  ./.gnome2/accels
   4.0	K	                       **  ./.gnome2/keyrings
   4.0	K	                       **  ./.gnome2/nautilus-scripts
   4.0	K	                       **  ./.gnome2_private
   4.0	K	                       **  ./.gnupg/private-keys-v1.d
   4.0	K	                       **  ./.kde/share/config
   4.0	K	                       **  ./.mozilla/firefox/9ikhdhm7.default/extensions
   4.0	K	                       **  ./netv/netvault/clientpackages
   4.0	K	                       **  ./netv/netvault/extrabkl
   4.0	K	                       **  ./netv/netvault/extrapackages
   4.0	K	                       **  ./.qt
   4.0	K	                       **  ./.skel
   4.0	K	                       **  ./.wapi
   4.0	K	                       **  ./.zenuser.gconf.defaults
   4.0	K	                       **  ./.zenuser.gconf.mandatory
   8.0	K	                      ***  ./.config/.mono/certs
   8.0	K	                      ***  ./Documents
   8.0	K	                      ***  ./firefox/firefox/defaults/pref
   8.0	K	                      ***  ./.gconf/apps/gnome-settings/gnome-panel
   8.0	K	                      ***  ./.gconf/apps/nautilus/preferences
   8.0	K	                      ***  ./.gconf/apps/panel/applets/clock_screen0/prefs
   8.0	K	                      ***  ./.gconf/apps/panel/applets/main_menu_screen0
   8.0	K	                      ***  ./.gconf/apps/panel/applets/mixer_screen0
   8.0	K	                      ***  ./.gconf/apps/panel/applets/notification_area_screen0
   8.0	K	                      ***  ./.gconf/apps/panel/applets/show_desktop_button_screen0
   8.0	K	                      ***  ./.gconf/apps/panel/applets/tomboy_screen0
   8.0	K	                      ***  ./.gconf/apps/panel/applets/window_list_screen0/prefs
   8.0	K	                      ***  ./.gconf/apps/panel/general
   8.0	K	                      ***  ./.gconf/apps/panel/global
   8.0	K	                      ***  ./.gconf/apps/panel/toplevels/bottom_panel_screen0/background
   8.0	K	                      ***  ./.gconf/desktop/gnome/accessibility/keyboard
   8.0	K	                      ***  ./.gconf/desktop/gnome/background
   8.0	K	                      ***  ./.gconf/desktop/gnome/peripherals/keyboard/host-posadm1/0
   8.0	K	                      ***  ./.gconf/system/http_proxy
   8.0	K	                      ***  ./.gconf/system/proxy
   8.0	K	                      ***  ./.gnome2/share/cursor-fonts
   8.0	K	                      ***  ./.gnome2/share/fonts
   8.0	K	                      ***  ./.gnome/gnome-vfs
   8.0	K	                      ***  ./.kde/share
   8.0	K	                      ***  ./.nautilus/metafiles
   8.0	K	                      ***  ./src/.tmp_versions
  12.0	K	                      ***  ./.config/.mono
  12.0	K	                      ***  ./firefox/firefox/defaults
  12.0	K	                      ***  ./.gconf/apps/gnome-settings
  12.0	K	                      ***  ./.gconf/apps/nautilus
  12.0	K	                      ***  ./.gconf/desktop/gnome/accessibility
  12.0	K	                      ***  ./.gconf/desktop/gnome/peripherals/keyboard/host-posadm1
  12.0	K	                      ***  ./.kde
  12.0	K	                      ***  ./.mozilla/firefox/9ikhdhm7.default/chrome
  12.0	K	                      ***  ./.nautilus
  12.0	K	                      ***  ./.thumbnails/fail/gnome-thumbnail-factory
  16.0	K	                     ****  ./.gconf/apps/panel/applets/clock_screen0
  16.0	K	                     ****  ./.gconf/apps/panel/applets/window_list_screen0
  16.0	K	                     ****  ./.gconf/apps/panel/toplevels/bottom_panel_screen0
  16.0	K	                     ****  ./.gconf/desktop/gnome/peripherals/keyboard
  16.0	K	                     ****  ./netv/netvault/eula/Chinese/BIG5
  16.0	K	                     ****  ./netv/netvault/eula/ChineseSimp/GB2312
  16.0	K	                     ****  ./netv/netvault/eula/Korean/EUC-KR
  16.0	K	                     ****  ./.thumbnails/fail
  20.0	K	                     ****  ./firefox/firefox/chrome/icons/default
  20.0	K	                     ****  ./.gconf/apps/panel/toplevels
  20.0	K	                     ****  ./.gconf/desktop/gnome/peripherals
  20.0	K	                     ****  ./.gconf/system
  20.0	K	                     ****  ./.gnome2/share
  20.0	K	                     ****  ./.kbd
  20.0	K	                     ****  ./netv/netvault/eula/French/ISO-8859-1
  20.0	K	                     ****  ./netv/netvault/eula/JapaneseEUC/EUC-JP
  20.0	K	                     ****  ./.thumbnails
  24.0	K	                     ****  ./firefox/firefox/chrome/icons
  24.0	K	                     ****  ./firefox/firefox/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}
  24.0	K	                     ****  ./netv/netvault/eula/English
  24.0	K	                     ****  ./netv/netvault/eula/German/ISO-8859-1
  28.0	K	                    *****  ./.config
  28.0	K	                    *****  ./firefox/firefox/chrome
  28.0	K	                    *****  ./firefox/firefox/extensions
  28.0	K	                    *****  ./firefox/firefox/searchplugins
  28.0	K	                    *****  ./.gnome
  28.0	K	                    *****  ./.gnupg
  32.0	K	                    *****  ./netv/netvault/eula/Chinese
  32.0	K	                    *****  ./netv/netvault/eula/ChineseSimp
  36.0	K	                    *****  ./.gnome2
  40.0	K	                    *****  ./firefox/firefox/icons
  40.0	K	                    *****  ./netv/netvault/eula/Korean
  44.0	K	                    *****  ./.gconf/desktop/gnome
  44.0	K	                    *****  ./netv/netvault/eula/French
  44.0	K	                    *****  ./zlmagent/data/locale/en_US/LC_MESSAGES
  48.0	K	                    *****  ./.gconf/desktop
  48.0	K	                    *****  ./.metacity/sessions
  48.0	K	                    *****  ./netv/netvault/eula/German
  48.0	K	                    *****  ./netv/netvault/eula/JapaneseEUC
  48.0	K	                    *****  ./.ssh
  48.0	K	                    *****  ./zlmagent/data/locale/en_US
  52.0	K	                    *****  ./.metacity
  52.0	K	                    *****  ./zlmagent/data/locale/pt_BR/LC_MESSAGES
  56.0	K	                   ******  ./zlmagent/data/locale/pt_BR
  60.0	K	                   ******  ./zlmagent/data/locale/de_DE/LC_MESSAGES
  60.0	K	                   ******  ./zlmagent/data/locale/es_ES/LC_MESSAGES
  60.0	K	                   ******  ./zlmagent/data/locale/fr_FR/LC_MESSAGES
  64.0	K	                   ******  ./.mozilla/firefox/9ikhdhm7.default/bookmarkbackups
  64.0	K	                   ******  ./zlmagent/data/locale/de_DE
  64.0	K	                   ******  ./zlmagent/data/locale/es_ES
  64.0	K	                   ******  ./zlmagent/data/locale/fr_FR
  76.0	K	                   ******  ./.gconf/apps/panel/applets
 116.0	K	                  *******  ./.gconf/apps/panel
 124.0	K	                  *******  ./zlmagent/readmes
 144.0	K	                  *******  ./.gconf/apps
 148.0	K	                  *******  ./squid-old
 168.0	K	                  *******  ./netv/netvault/serverpackages
 168.0	K	                  *******  ./zlmagent/data/configureScripts
 184.0	K	                  *******  ./.gstreamer-0.10
 216.0	K	                 ********  ./.gconf
 232.0	K	                 ********  ./firefox/firefox/components
 260.0	K	                 ********  ./zlmagent/data/eulas
 272.0	K	                 ********  ./netv/netvault/eula
 300.0	K	                 ********  ./zlmagent/data/locale
 340.0	K	                 ********  ./.vmware/VMware vCenter Converter Standalone/Logs
 348.0	K	                 ********  ./.vmware/VMware vCenter Converter Standalone
 352.0	K	                 ********  ./.vmware
 624.0	K	                *********  ./firefox/firefox/dictionaries
   1.4	M	               **********  ./zlmagent/data/packages/runtime-deps/sles-10-i586
   1.4	M	               **********  ./zlmagent/data/packages/runtime-deps
   1.6	M	              ***********  ./zlmagent/data/packages/imaging/sles-10-i586
   1.6	M	              ***********  ./zlmagent/data/packages/imaging
   1.9	M	              ***********  ./src
   1.9	M	              ***********  ./.mozilla/firefox/9ikhdhm7.default/Cache
   3.4	M	             ************  ./netv/netvault/packages
   3.6	M	             ************  ./zlmagent/data/packages/sles-10-i586/sles-10-i586
   3.6	M	             ************  ./zlmagent/data/packages/sles-10-i586
   5.0	M	             ************  ./.mozilla/firefox/9ikhdhm7.default
   5.0	M	             ************  ./.mozilla/firefox
   5.0	M	             ************  ./.mozilla
   5.9	M	            *************  ./scripts
  14.0	M	           **************  ./zlmagent/data/packages/mono/sles-10-i586
  14.0	M	           **************  ./zlmagent/data/packages/mono
  24.9	M	          ***************  ./netv/netvault
  24.9	M	          ***************  ./netv
  25.9	M	          ***************  ./zlmagent/data/packages/client/sles-10-i586
  25.9	M	          ***************  ./zlmagent/data/packages/client
  28.4	M	          ***************  ./firefox/firefox
  46.5	M	         ****************  ./zlmagent/data/packages
  47.7	M	         ****************  ./zlmagent/data
  47.8	M	         ****************  ./zlmagent
  57.9	M	         ****************  ./firefox
  92.8	M	        *****************  ./acro
 184.5	M	       ******************  ./servername-backup
 437.2	M	      *******************  .

1
By: Q_Element
2012-02-07 15:49:19

These Might Interest You

  • Will work with filenames with spaces inside. Will not break in case of someone making directory that matches *.pm. And sorts from largest. Where largest is file size, not line count.


    0
    find . -type f -name '*.pm' -printf '%6s %p\n' | sort -nr | head -n 50
    depesz · 2011-02-09 16:19:11 0
  • My Programming Languages professor assigned my class a homework assignment where we had to write a Perl interpreter using Perl. I really like Python's interactive command line interpreter which inspired this Perl script.


    10
    perl -e 'while(1){print"> ";eval<>}'
    kzh · 2010-02-03 21:48:33 1
  • You must have PHP 5.4.0 or later to be able to run the built in server. This web server is designed for developmental purposes only, and should not be used in production. URI requests are served from the current working directory where PHP was started, unless the -t option is used to specify an explicit document root. If a URI request does not specify a file, then either index.php or index.html in the given directory are returned. If neither file exists, then a 404 response code is returned. If a PHP file is given on the command line when the web server is started it is treated as a "router" script. The script is run at the start of each HTTP request. If this script returns FALSE, then the requested resource is returned as-is. Otherwise the script's output is returned to the browser. Standard MIME types are returned for files with extensions: .css, .gif, .htm, .html, .jpe, .jpeg, .jpg, .js, .png, .svg, and .txt. The .htm and .svg extensions are recognized from PHP 5.4.4 onwards. More information here: http://php.net/manual/en/features.commandline.webserver.php Show Sample Output


    14
    php -S 127.0.0.1:8080
    VictorBjelkholm · 2013-05-19 11:23:17 2
  • * Find all file sizes and file names from the current directory down (replace "." with a target directory as needed). * sort the file sizes in numeric order * List only the duplicated file sizes * drop the file sizes so there are simply a list of files (retain order) * calculate md5sums on all of the files * replace the first instance of two spaces (md5sum output) with a \0 * drop the unique md5sums so only duplicate files remain listed * Use AWK to aggregate identical files on one line. * Remove the blank line from the beginning (This was done more efficiently by putting another "IF" into the AWK command, but then the whole line exceeded the 255 char limit). >>>> Each output line contains the md5sum and then all of the files that have that identical md5sum. All fields are \0 delimited. All records are \n delimited.


    0
    find . -type f -not -empty -printf "%-25s%p\n"|sort -n|uniq -D -w25|cut -b26-|xargs -d"\n" -n1 md5sum|sed "s/ /\x0/"|uniq -D -w32|awk -F"\0" 'BEGIN{l="";}{if(l!=$1||l==""){printf "\n%s\0",$1}printf "\0%s",$2;l=$1}END{printf "\n"}'|sed "/^$/d"
    alafrosty · 2013-10-22 13:34:19 0

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: