#!/bin/sh ## ## Nautilus ## SCRIPT: 04_anyfile4Dir_SPACE-USEDby_FILES_1LEV_AGEsort_ls-awk.sh ## ## PURPOSE: This utility will list the files in the directory, sorted by age, ## and showing the size of all the files in the same ## easily-comparable units (MB), with rigid (highly readable) ## columnar formatting. ## ## The directories in this directory are also listed. ## ## METHOD: This script puts the output of 'ls' in a text file --- after ## piping the output through 'sed', 'grep', and 'awk' --- to ## produce a nicely formatted report. ## ## Shows the text file using a text-file viewer of the user's ## choice. ## ## HOW TO USE: In the Nautilus file manager, right-click on the name of ## ANY file (or directory) in the desired current directory. ## Then select this script to run (name above). ## ## Created: 2010apr07 ## Changed: 2011may02 Added $USER to a temp filename. ## Changed: 2011may11 Get 'nautilus-scripts' directory via an include script. ## Changed: 2012feb29 Changed the script name in the comment above. ## FOR TESTING: (show statements as they execute) # set -x ############################################################ ## Prep a temporary filename, to hold the list of filenames ## with size and age info. ############################################################ ## NOTE: ## We could put the report in the /tmp directory, rather than ## junking up the current directory with this report. ## ## BUT it may be useful to have the report file in the directory ## to which the report applies. ################################################################# OUTFILE="${USER}_space_files_onelevel_agesort_temp.lis" CURDIR="`pwd`" if test ! -w "$CURDIR" then OUTFILE="/tmp/$OUTFILE" fi if test -f "$OUTFILE" then rm -f "$OUTFILE" fi ########################################################################## ## PUT THE REPORT INFO (from ls -latr) INTO AN IN-MEMORY VARIABLE, $OUT. ########################################################################## ## ISSUE THE 'ls -latr' COMMAND -- LOCALLY. ########################################################################## ## SAMPLE OUTPUT FROM 'ls -latr', on Linux (Ubuntu 9.10): ## $ ls -latr ## total 756 ## -rw-r--r-- 1 user1 user1 675 2009-11-01 16:33 .profile ## -rw-r--r-- 1 user1 user1 167 2009-11-01 16:33 examples.desktop ## -rwxr-xr-x 1 user1 user1 3180 2009-11-01 16:33 .bashrc ## -rw-r--r-- 1 user1 user1 220 2009-11-01 16:33 .bash_logout ## drwxr-xr-x 4 root root 4096 2009-11-01 16:33 .. ## drwx------ 3 user1 user1 4096 2009-11-01 16:41 .dbus ## -rw------- 1 user1 user1 256 2009-11-01 16:41 .pulse-cookie ## -rw------- 1 user1 user1 16 2009-11-01 16:41 .esd_auth ########################################################################## ## The first line ('total ###') will be used to convert the number ### ## from 512-byte blocks to Megabtyes and display in the heading of the ## report. ## The remaining lines will be sorted and reformated for the report body. ########################################################################## ## FOR TESTING: # set -x OUT=`ls -latr $CURDIR` ## FOR TESTING: # set - ########################################################################## ## CALCULATE TOTAL SIZE OF THE DIRECTORY FROM ## the first line ('total ###'). ########################################################################## ## Convert the number ### from 512-byte blocks to ## Megabtyes and display in the heading of the report. ## (1048576 = 1024 * 1024) ########################################################################## BLOCKS=`echo "$OUT" | head -1 | cut -d" " -f2` TOTMEG=`echo "scale=3; $BLOCKS * 512 / 1048576" | bc` ################################################################## ## SET REPORT HEADING for size of files and other file info. ################################################################## HOST_ID="`hostname`" echo "\ ..................... `date '+%Y %b %d %a %T%p'` ............................ DISK USAGE OF FILES (and directory 'indexes') IN DIRECTORY: $CURDIR ON HOST: ${HOST_ID} at *ONE* level under this directory, NOT all levels. SORTED BY *AGE* --- OLDEST FILES (and links) AT THE TOP. DIRECTORIES ARE BELOW THE FILES (and links). This report was generated by running the 'ls -latr' command on $HOST_ID . TOTAL SPACE used by files at this directory level is $BLOCKS 512-byte blocks --- about $TOTMEG Megabytes. AGE-SORT ************* Disk usage File-type & Last-Modified Filename or link (MegaBytes) Permissions Owner Group Date-Time/Yr (with embedded blanks, if any) ------------- ----------- -------- -------- ---------------- ---------------------- GigMeg.KilByt | | | | " > "$OUTFILE" ########################################################################## ## GENERATE REPORT BODY --- from contents saved in $OUT. ########################################################################## ## NOTE on 'ls -l' format, Unix: ## ## In 'ls -l' output, if the file is not more than a year old, ## $1=perms $3=userid $5=bytes $6=mmm, $7=dd $8=hrs:min $NF=filename ## ## For a file more than a year old, $8=yyyy, where yyyy denotes year. ## Example: ## -rw-r--r-- 1 root root 2297 Aug 21 2007 asound.names ########################################################################## ## NOTE on 'ls -l' format, on Linux (Ubuntu 9.10): ## ## $1=perms $3=userid $5=bytes $6=yyyy-mm-dd, $7=hrs:min $8=filename ## Example: ## -rwxr-xr-x 1 user1 user1 2369 2009-11-22 02:07 .bash_aliases ########################################################################## ## NOTE on filenames with embedded spaces and 'awk': ## ## To handle filenames with embedded spaces, we use 'substr($0,COLfilnam)' ## in awk, instead of '$8' --- where ## we set COLfilnam with the expression 'COLfilnam = index($0,$8)'. ## ## The 'substr($0,COLfilnam)' gets the substring of the record, $0, ## starting at beginning of filename and going to the end of the record. ## ######################################################################### ## FOR TESTING: # set -x ######################################################### ## THIS FORM MIXES directories with regular-files and links. # # echo "$OUT" | tail +2 | \ # awk '{printf ("%13.6f %-10s %-8s %-8s %-10s %5s %s\n", $5/1000000, $1, $3, $4, $6, $7, $8 )}' \ # >> "$OUTFILE" ######################################################### ############################################ ## Add NON-directories --- i.e. ## FILES (and LINKS, etc.) --- to the report. ############################################ ########################################################################### ## Note: 'tail =2' does not work as designed on some Linuxes (Ubuntu 9.10). ## We use "sed '1d'" instead. ########################################################################### echo "$OUT" | sed '1d' | grep -v '^d' | \ awk '{ COLfilnam = index($0,$8) ; \ printf ("%13.6f %-10s %-8s %-8s %-10s %5s %s\n", $5/1000000, $1, $3, $4, $6, $7, substr($0,COLfilnam) )}' \ >> "$OUTFILE" ############################################ ## Add a separator line --- between ## FILES and DIRECTORIES. ############################################ echo " Directories:" >> "$OUTFILE" ######################################### ## Add DIRECTORIES to the report. ######################################### echo "$OUT" | sed '1d' | grep '^d' | \ awk '{ COLfilnam = index($0,$8) ; \ printf ("%13.6f %-10s %-8s %-8s %-10s %5s %s\n", $5/1000000, $1, $3, $4, $6, $7, substr($0,COLfilnam) )}' \ >> "$OUTFILE" ## FOR TESTING: # set - ######################################################################## ## Add TRAILER to report. ######################################################################## echo " | | | | GigMeg.KilByt ------------- ----------- -------- -------- ---------------- ---------------------- (MegaBytes) File-type & Owner Group Date-Time/Yr Filename or link Disk usage Permissions Last-Modified (with embedded blanks, if any) ************* AGE-SORT ..................... `date '+%Y %b %d %a %T%p'` ............................ The output above was generated by the script $0 which ran the 'ls -latr' command on host $HOST_ID . ----------------- PROCESSING METHOD: The script uses a 'pipe' of commands (ls, sed, grep, awk) like: ls -latr | sed '1d' | grep -v '^d' | awk '{ ... print ... }' and ls -latr | sed '1d' | grep '^d' | awk '{ ... print ... }' ------------ FEATURE NOTE: This utility provides formatting - bytes-to-MegaBytes conversion, - directories separated from other files, - heading/trailer info, - fixed-width columns that is not available with only the 'ls' command. --------------------- DIRECTORIES-SIZE NOTE: A line that starts with 'd' in the permissions string shows the size of the 'index' (i.e. table of contents) of a directory -- NOT the size of all the files and subdirectories under that directory. ..................... `date '+%Y %b %d %a %T%p'` ............................ " >> "$OUTFILE" #################################################### ## Show the listing. #################################################### ## . $HOME/.gnome2/nautilus-scripts/.set_VIEWERvars.shi . $HOME/.freedomenv/feNautilusScripts/set_DIR_NautilusScripts.shi . $DIR_NautilusScripts/.set_VIEWERvars.shi $TXTVIEWER "$OUTFILE" &