- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- ll file sizes challenge
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2002 02:47 AM
07-12-2002 02:47 AM
Was wondering if anyone would be up to the challenge of writing a script that could parse an ll result and take options -M -K -G to display file size in meg gig etc..
Thats all!
Reward as usual!
Later,
Bill "Broken calculator"
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2002 03:13 AM
07-12-2002 03:13 AM
Solutionhere my little quick and dirty script:
#!/bin/sh
LANG=C
DIV=1
FMT="%.0f"
for arg
do
case $arg in
-K) DIV=1024; FMT="%8.2fK" ;;
-M) DIV=1048576; FMT="%8.3fM" ;;
-G) DIV=1073741824; FMT="8.3fG";;
esac
done
ls -l $* |awk -vdiv=$DIV -vfmt=$FMT '{
printf("%-11s %2s %-10s %-8s ",$1,$2,$3,$4);
printf(fmt " ", int($5)/div);
printf("%s " , $6);
printf("%2s" , $7);
printf(" %5s" , $8);
for(i=9; i
printf(" %s\n", $NF);
}'
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2002 03:29 AM
07-12-2002 03:29 AM
Re: ll file sizes challenge
Nice script ANDREAS
I fixed it a little
#!/bin/sh
LANG=C
div=1
FMT="%.0f"
for arg
do
case $arg in
-K) div=1024; FMT="%8.2fK" ; shift 1 ;;
-M) div=1048576; FMT="%8.3fM" ; shift 1 ;;
-G) div=1073741824; FMT="%8.3fG" ; shift 1;;
esac
done
echo $*
ls -l $*|tail -n +2 |awk -vdiv=$div -vfmt=$FMT '{
printf("%-11s %2s %-10s %-8s ",$1,$2,$3,$4);
printf(fmt " ", int($5)/div);
printf("%s " , $6);
printf("%2s" , $7);
printf(" %5s" , $8);
for(i=9; i
printf(" %s\n", $NF);
}'
Steve Steel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2002 03:29 AM
07-12-2002 03:29 AM
Re: ll file sizes challenge
#!/usr/bin/sh
#
# EZ long-listing of files
#
#
set -u
export PATH=/usr/bin
typeset -L10 PERM
typeset -L3 LNK
typeset -L9 OWNER
typeset -L9 GROUP
typeset -R5 FINAL
typeset -L2 SUFFIX
# Fancy incantation to handle all possible $@ values
ll ${@+"$@"} | while read PERM LNK OWNER GROUP SIZE RESTOFLINE
do
if [ $PERM != total ]
then
if [ $SIZE -lt 1000 ]
then
FINAL=$SIZE
SUFFIX="b"
fi
if [ $SIZE -ge 1000 ]
then
FINAL=$(( SIZE / 1000 ))
SUFFIX="Kb"
fi
if [ $SIZE -ge 1000000 ]
then
FINAL=$(( SIZE / 1000000 ))
SUFFIX="Mb"
fi
if [ $SIZE -ge 1000000000 ]
then
FINAL=$(( SIZE / 1000000000 ))
SUFFIX="Gb"
fi
echo "$PERM $LNK $OWNER $GROUP $FINAL $SUFFIX $RESTOFLINE"
fi
done
-----
Now if you change the ll line from:
ll ${@+"$@"} | while read PERM LNK OWNER GROUP SIZE RESTOFLINE
to:
ll ${@+"$@"} | sort -rnk5 | while read PERM LNK OWNER GROUP SIZE RESTOFLINE
then the script will show files sorted by size. You can also sort by size as a 1-liner with:
ll | sort -rnk5 | more
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2002 04:52 AM
07-12-2002 04:52 AM
Re: ll file sizes challenge
i tried a shell script.
-----------------------
#!/usr/bin/sh
if [ $1 = "-K" ]
then
ll |awk '{print $1," "$2,$3,"\t"$4,"\t"$5/1024,$6,$7,$8,$9}'
else
if [ $1 = '-M' ]
then
ll |awk '{print $1," "$2,$3,"\t"$4,"\t"$5/1048568,$6,$7,$8,$9}'
else
if [ $1 = '-G' ]
then
ll |awk '{print $1," "$2,$3,"\t"$4,"\t"$5/1073741824,$6,$7,$8,$9}'
fi
fi
fi
--------------------------
usage sh <script name> (-K or -M or -G)
but output is not as clear as ll and for single file or selective files it won't work.
regds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2002 05:02 AM
07-12-2002 05:02 AM
Re: ll file sizes challenge
Thanks guys!
Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2002 05:31 AM
07-12-2002 05:31 AM
Re: ll file sizes challenge
I could not resist, to combine some stuff I learned from the previous three answers to a new variant.
No options needed, just save Script as "lG" and do hardlink to 'lK' and 'lM'.
Use embedded awk-variable CONVFMT to avoid sprintf and turn $5 into an expression by dividing through "div".
Pass standard wildcards or Sort options and have no functional change like "lK -rt".
#!/bin/sh
[[ `basename $0` = 'lG' ]] && div=1000000000
[[ `basename $0` = 'lM' ]] && div=1000000
[[ `basename $0` = 'lK' ]] && div=1000
fmt=`basename $0 | cut -c2`
ll $* | awk -vdiv=$div -vCONVFMT='%8.2f'$fmt '{ print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5/div "\t" $6 "\t" $7 "\t" $8 "\t" $9 }'
Have fun
Volker
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2002 08:28 AM
07-12-2002 08:28 AM
Re: ll file sizes challenge
I know this isn;t what you asked for. If we had access to the source code for ls we could implement our own little changes in there. I was looking at the source for FreeBSD (or OpenBSD, can't remember) and saw the following (sorry for the formatting if it doesn't come out right):
while ((ch = getopt(argc, argv, "1ACFLRSTWacdfgiklmnopqrstux")) != -1) {
switch (ch) {
/*
* The -1, -C and -l, -m and -x options all override each
* other so shell aliasing works right.
*/
case '1':
f_singlecol = 1;
f_column = f_columnacross = f_longform = f_stream = 0;
break;
case 'C':
f_column = 1;
f_longform = f_columnacross = f_singlecol = f_stream = 0
;
break;
case 'l':
f_longform = 1;
f_numericonly = 0;
f_column = f_columnacross = f_singlecol = f_stream = 0;
break;
** We could add the K,M,G as options that would act just like the l option.
Further down in the source you will find:
/* If -l or -s, figure out block size. */
if (f_longform || f_size) {
if (!kflag)
(void)getbsize(¬used, &blocksize);
blocksize /= 512;
}
*** Here we could use div function:
div($bytes, 1024)
div($bytes, 1048576)
div($bytes, 1073741824)
Sorry I couldn't do the real program because I don't have the source but it would have been fairly interesting. I guess this could be an addition to the challenge.