LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-15-2011, 06:15 PM   #31
gross6
LQ Newbie
 
Registered: Dec 2010
Location: Roma (Italy)
Distribution: debian squeeze
Posts: 12

Rep: Reputation: 0

Deleted

Last edited by gross6; 01-26-2011 at 08:27 AM. Reason: My stupid observations
 
Old 01-24-2011, 09:25 AM   #32
gross6
LQ Newbie
 
Registered: Dec 2010
Location: Roma (Italy)
Distribution: debian squeeze
Posts: 12

Rep: Reputation: 0
Further update

Code:
#!/bin/bash
#file lsd
#provide disk usage of directories recursively in ls -lh listing

if [ $# -ne 1 ] #if no argument, assign pwd to var a
then
a='./'
else a=$1  #else assign argument to var a
fi

len=${#a} #extract length of a

#count char of a until the last occurrence of "/"
e=`expr "$a" : '.*/'`

#if count not equal to length of a
#(that is: argument without a final slash)
if [ "$e" -ne "$len" ]
then
a=${a}/  #add slash to argument
fi

#change the 5th field of "ls -lh" with "du -hs" 
ls -lh "$a" | awk -v dir_arg="${a}" '
     BEGIN {
     gsub(/ /,"\\ ",dir_arg)
     gsub(/'"'"'/,"'"\\\'"'",dir_arg)
     }
     substr($1,1,1)=="d"{
         {
         if ($3==$8 || $4==$8) 
         dir_listed = $8    
         else
         dir_listed = substr($0,index($0,$8))
         }
     gsub(/ /,"\\ ",dir_listed)
     gsub(/'"'"'/,"'"\\\'"'",dir_listed)
     gsub(/'"\("'/,"'"\\\("'",dir_listed)
     gsub(/'"\)"'/,"'"\\\)"'",dir_listed)
     ("du -hs "dir_arg dir_listed) | getline size
     split(size,size_)
     sub($5,size_[1],$5)
     }
     substr($1,1,1)=="t"{
     printf ("Files total size %s\n",$2)
     }
     substr($1,1,1)!="t" && substr($1,1,1)!="d"{
     printf ("%s %3s %s %s %5s %s %s %s\n",$1,$2,$3,$4,$5,$6,$7,substr($0,index($0,$8)))
     }
     substr($1,1,1)=="d"{
     printf ("%s %3s %s %s %5s %s %s %s\n",$1,$2,$3,$4,$5,$6,$7,dir_listed)
     }'

#print the whole dimension of argument
echo -n "Grand total size  "; du -sh "$a"

#updated 2011/01/23
This is another update..
Bye.
 
Old 01-25-2011, 04:35 AM   #33
louisJ
LQ Newbie
 
Registered: Jun 2007
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks to you all, for answering this post. I guess this will be useful to others.
The script is great.
I marked the thread as solved.
 
Old 02-23-2011, 04:56 PM   #34
gross6
LQ Newbie
 
Registered: Dec 2010
Location: Roma (Italy)
Distribution: debian squeeze
Posts: 12

Rep: Reputation: 0
Update

Code:
#!/bin/bash

# file lsd
# provide disk usage of directories in ls -lh listing

# version 0.1.0 2011-02-20 by lindis

# Thanks to colucix, ed and PHV for awk tips. See:
# http://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-ls-display-the-real-size-for-directories-638975/
# http://dbaspot.com/forums/shell/200908-awk-print-x.html
# http://www.tek-tips.com/viewthread.cfm?qid=1264376&page=1


#verifying gawk existence
if [ ! -f /usr/bin/gawk ]
    then echo
    echo " To run this program you need gawk installed"
    echo; exit 1
fi

#gets option "-a"
while getopts "a" Option
do
    case $Option in
       a ) op=a     ;;
       * ) op=other ;;
    esac
done

if [ "$op" == "other" ]
    then exit 1
fi

if [ "$op" == "a" ]
    then shift $(($OPTIND - 1))
fi

#if no argument, assign pwd to var "dir" 
if [ -z "$1" ] 
    then dir='./'
    else dir=$1  #else assign argument to var "dir"
fi

len=${#dir} #extract length of "dir"

#count char of "dir" until the last occurrence of "/"
e=`expr "$dir" : '.*/'`

#if count not equal to length of "dir"
#(that is: argument without a final slash)
if [ "$e" -ne "$len" ]
    then dir=${dir}/  #add slash to argument
fi

#assign to array arr nr field of each row of ls -l
declare -a arr
arr=(`ls -l /usr/ | gawk ' {print NF} '`)

#change the 5th field of "ls -lh" with "du -hs" using gawk 
ls -lh"$op" "$dir" 2>&1 | gawk -v dir_arg="${dir}" -v num_field="${arr[1]}" --re-interval '
     
BEGIN{
    gsub(/ /,"\\ ",dir_arg) #escape spaces
    gsub(/'"'"'/,"'"\\\'"'",dir_arg) #escape apostrophes
}

#assign the right name to dirs to be listed
num_field==8{
dir_listed = gensub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){7}/,"","g")    
}

num_field==9{
dir_listed = gensub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){8}/,"","g")    
}

#escape spaces and parentheses of dirs name listed
substr($1,1,1)=="d"{
    gsub(/ /,"\\ ",dir_listed) 
    gsub(/'"'"'/,"'"\\\'"'",dir_listed)
    gsub(/'"\("'/,"'"\\\("'",dir_listed)
    gsub(/'"\)"'/,"'"\\\)"'",dir_listed)
    {
        DOUBLE_DOT=0 #flag to skip parent dir to be faster
        if (dir_listed=="..")
            {
                dir_listed = "."
                DOUBLE_DOT=1
            }
    }
    #substitute 5th field of ls with 1st field of du 
    ("du -sh "dir_arg dir_listed) | getline du_line
    close("du -sh "dir_arg dir_listed) #to prevent "too many open files" error
    split(du_line,du_line_arrow)
    sub($5,du_line_arrow[1],$5)
}

#print files total size
substr($1,1,1)=="t"{
    printf ("Files total size %s\n",$2)
}

#print file rows 
{
if (substr($1,1,1)!="t" && substr($1,1,1)!="d")
    { 
        if (num_field==8) pr8(dir_listed)
        else pr9(dir_listed)
    }
}

#print dirs rows
{
if (substr($1,1,1)=="d")
    {
        if (DOUBLE_DOT==1) #parent dir skipped
            printf ("          parent file_name (..) intentionally skipped\n")
        else
        {
            gsub(/'"\\\\"'/,"",dir_listed) #delete escape chars
            if (num_field==8) pr8(dir_listed)
            else pr9(dir_listed)
        }
    }
}

function pr8 (file_name)
{ printf ("%s %3s %7s %7s %5s %s %s %s\n",$1,$2,$3,$4,$5,$6,$7,file_name)}

function pr9 (file_name)
{ printf ("%s %3s %7s %7s %5s %s %s %s %s\n",$1,$2,$3,$4,$5,$6,$7,$8,file_name)}

' #single quote to close awk command
  
#print the whole dimension of argument
echo -n "Grand total size  "; du -sh "$dir" 2>/dev/null

exit 0
Another update that eliminates some troubles and add a new feature.
1) No problems with spaces parentheses or other strange characters.
2) No problems with 9 fields of ls -lh command.
3) You can use the -a option to scan hidden files/directories too.

Use:
Save (as root) the above script (naming it lsd) in /usr/local/bin and give it the execute permission:
Code:
chmod +x /usr/local/bin/lsd
Install gawk program if not already installed.

You can indicate as argument the directory to scan too.
Of course for large directories it will take a while.
If there are 'du:' error messages some dirs size may be wrong. So try as root or using 'sudo' command.

Bye
 
Old 12-14-2011, 02:59 PM   #35
idleup
LQ Newbie
 
Registered: Dec 2011
Posts: 1

Rep: Reputation: Disabled
This is a nice script, there is only one problem for me. Is there a way to add the --color option to the 'ls' command? I tried modifying the script but everything I tried would generate errors.

- Matt
 
Old 12-20-2011, 05:10 AM   #36
gross6
LQ Newbie
 
Registered: Dec 2010
Location: Roma (Italy)
Distribution: debian squeeze
Posts: 12

Rep: Reputation: 0
Quote:
Originally Posted by idleup View Post
This is a nice script, there is only one problem for me. Is there a way to add the --color option to the 'ls' command? I tried modifying the script but everything I tried would generate errors.

- Matt
The best is to modify the coreutils source code. But it is not a simple task. In example the less command loses the colors too. Try
Code:
>ls -l | less
and see what happens. Perhaps in the future someone (me?) will find a simpler solution.

Bye
 
Old 01-02-2012, 02:00 PM   #37
gross6
LQ Newbie
 
Registered: Dec 2010
Location: Roma (Italy)
Distribution: debian squeeze
Posts: 12

Rep: Reputation: 0
Quote:
Originally Posted by idleup View Post
This is a nice script, there is only one problem for me. Is there a way to add the --color option to the 'ls' command? I tried modifying the script but everything I tried would generate errors.

- Matt
Try the following code:
Code:
#!/bin/bash


    # file lsdc
    # provide disk usage of directories in ls -lh listing with colored dirs

    # version 0.1.0 2012-01-02 by lindis

    # Thanks to colucix, ed and PHV for awk tips. See:
    # http://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-ls-display-the-real-size-for-directories-638975/
    # http://dbaspot.com/forums/shell/200908-awk-print-x.html
    # http://www.tek-tips.com/viewthread.cfm?qid=1264376&page=1


    #verifying gawk existence
    if [ ! -f /usr/bin/gawk ]
        then echo
        echo " To run this program you need gawk installed"
        echo; exit 1
    fi

    #gets option "-a"
    while getopts "a" Option
    do
        case $Option in
           a ) op=a     ;;
           * ) op=other ;;
        esac
    done

    if [ "$op" == "other" ]
        then exit 1
    fi

    if [ "$op" == "a" ]
        then shift $(($OPTIND - 1))
    fi

    #if no argument, assign pwd to var "dir"
    if [ -z "$1" ]
        then dir='./'
        else dir=$1  #else assign argument to var "dir"
    fi

    len=${#dir} #extract length of "dir"

    #count char of "dir" until the last occurrence of "/"
    e=`expr "$dir" : '.*/'`

    #if count not equal to length of "dir"
    #(that is: argument without a final slash)
    if [ "$e" -ne "$len" ]
        then dir=${dir}/  #add slash to argument
    fi

    #assign to array arr nr field of each row of ls -l
    declare -a arr
    arr=(`ls -l /usr/ | gawk ' {print NF} '`)

    echo

    #change the 5th field of "ls -lh" with "du -hs" using gawk
    ls -lh"$op" "$dir" 2>&1 | gawk -v dir_arg="${dir}" -v num_field="${arr[1]}" -v reset_color="\033[0m" --re-interval '
         
    BEGIN{
        gsub(/ /,"\\ ",dir_arg) #escape spaces
        gsub(/'"'"'/,"'"\\\'"'",dir_arg) #escape apostrophes
    }

    #assign the right name to dirs to be listed
    num_field==8{
    dir_listed = gensub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){7}/,"","g")   
    }

    num_field==9{
    dir_listed = gensub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){8}/,"","g")   
    }

    #escape spaces and parentheses of dirs name listed
    substr($1,1,1)=="d"{
        #gsub(/'"\033\[0m"'/,"",dir_listed)
		#gsub(/'"\033\[01;34m"'/,"",dir_listed)
		gsub(/ /,"\\ ",dir_listed)
        gsub(/'"'"'/,"'"\\\'"'",dir_listed)
        gsub(/'"\("'/,"'"\\\("'",dir_listed)
        gsub(/'"\)"'/,"'"\\\)"'",dir_listed)
        {
            DOUBLE_DOT=0 #flag to skip parent dir to be faster
            if (dir_listed=="..")
                {
                    dir_listed = "."
                    DOUBLE_DOT=1
                }
        }
        #substitute 5th field of ls with 1st field of du
        ("du -sh "dir_arg dir_listed" 2>/dev/null") | getline du_line
        close("du -sh "dir_arg dir_listed) #to prevent "too many open files" error
        split(du_line,du_line_arrow)
        sub($5,du_line_arrow[1],$5)
    }

    #print files total size
    substr($1,1,1)=="t"{
        printf ("Files total size %s\n",$2)
    }

    #print file rows
    {
    if (substr($1,1,1)!="t" && substr($1,1,1)!="d")
        {
            if (num_field==8) pr8(dir_listed)
            else pr9(dir_listed)
        }
    }

    #print dirs rows
    
    substr($1,1,1)=="d"{
            if (DOUBLE_DOT==1) #parent dir skipped
                printf ("          parent file_name (..) intentionally skipped\n")
            else
            {
                gsub(/'"\\\\"'/,"",dir_listed) #delete escape chars
				sub(/'""'/,"\033[01;34m",dir_listed)
                if (num_field==8) pr8(dir_listed)
                else pr9(dir_listed)
            }
    }
    

    function pr8 (file_name)
    { printf ("%s %3s %7s %7s %5s %s %s %s %s \n",$1,$2,$3,$4,$5,$6,$7,file_name,reset_color)}

    function pr9 (file_name)
    { printf ("%s %3s %7s %7s %5s %s %s %s %s %s \n",$1,$2,$3,$4,$5,$6,$7,$8,file_name,reset_color)}
	
    ' #single quote to close gawk command
	     
    #print the whole dimension of argument
    echo -n "Grand total size  "; du -sh "$dir" 2>/dev/null
   	echo

    exit 0
If your goal is to show dirs with different color maybe the script hit the target.
If you prefer a different color change digits 34 in row 124.
Let me know.
Bye.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I can't make screen size changes "stick" -- they're always set back after a reboot PTrenholme Ubuntu 15 02-29-2008 07:45 PM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 04:07 PM
How to display "real time" view of process running in openSUSE 10.1 ramirez.alex SUSE / openSUSE 6 02-27-2007 04:48 AM
K3b: - Howto re-dock "Directories" and "Contents" windows back into the main window? hagies Linux - Software 4 04-26-2006 09:38 AM
Kernel "make config" - is this for real? orange400 Linux - General 21 04-25-2004 06:32 AM


All times are GMT -5. The time now is 08:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration