Jump to content

How do you display the folder info?


Big_Baller
 Share

Recommended Posts

How would a user obtain this listing?

total 25
drwxr-xr-x	2 dshoe	users	2048	May	11	12:04	backups
drwxr-xr-x	2 dshoe	users	1024	May	10	10:02	bin
drwxr-xr-x	2 dshoe	users	1024	May	7	13:22	c_source
drwxr-xr-x	2 dshoe	users	1024	Apr	2	07:52	cgi_files
drwxr-xr-x	2 dshoe	users	1024	Apr	2	07:52	cgi_scripts
drwxr-xr-x	4 dshoe	users	1024	Jan	12	07:32	ny
-rw-r--r--	1 dshoe	users	1954	May	11	06:49	p1
drwxr-xr-x	2 dshoe	users	1024	Apr	2	07:52	perl
-rw-r--r--	1 dshoe	users	1954	May	6	16:42	pers
drwxrwxrwx	2 dshoe	users	1024	Apr	7	13:06	public_html
-rw-r--r--	1 dshoe	users	3392	Apr	23	07:25	sales
-rwxr--r--	1 dshoe   	 users	228	May	10	10:33	sargs
drwx------	2 dshoe	users	1024	Apr	26	06:53	scripts
-rw-r--r--	1 dshoe	users	131	Mar	22	11:20	setup
Total bytes:	17899
Total files:	5
Directories:	9
And scripts:	1

 

I know it starts off with the ls -l command but I'm not sur how you get the total bytes and total files thingy near the bottem. Thx

Link to comment
Share on other sites

How would a user obtain this listing?
total 25
drwxr-xr-x	2 dshoe	users	2048	May	11	12:04	backups
...
-rw-r--r--	1 dshoe	users	131	Mar	22	11:20	setup
Total bytes:	17899
Total files:	5
Directories:	9
And scripts:	1

 

I know it starts off with the ls -l command but I'm not sur how you get the total bytes and total files thingy near the bottem. Thx

 

That looks to me that is produced by a script that combines the output of both 'ls' and 'file' commands (or maybe there is a new version of ls which does this, but I don't think so).

 

dirty coded such script could be something like this:

 

#! /bin/bash

# produces Big_Baller's desired output.

 

DIR=${1:-./}

LS_L=$(/bin/ls -l $DIR)

FILES=$(/bin/ls $DIR)

 

# first output:

echo "${LS_L}"

 

# second output (total bytes in listing):

echo "${LS_L}" | awk '{suma=suma + $5} END{print "Total bytes: "suma}'

 

# third output (file types):

DIRECTORIES=0

TOTAL_FILES=0

SCRIPTS=0

for file in $FILES;

do

    if [ -d $file ]; then

        DIRECTORIES=$(( ++DIRECTORIES ))

    else

        TOTAL_FILES=$(( ++TOTAL_FILES ))

        # file types (add as many as you like):

        case "$(file $file)" in

            *script*) SCRIPTS=$(( ++SCRIPTS ));;

            *) :;;

        esac

    fi

done

 

printf "Total files: %5d\n" $TOTAL_FILES

printf "Directories: %5d\n" $DIRECTORIES

printf "And scripts: %5d\n" $SCRIPTS

(*)Sorry if mubcolorize script's output is a bit odd, ;) Just trying to put some color note here!

 

that script produces this output:

aru@paleo ~$ ./ls.adv
total 360
drwxr-xr-x   4 aru aru    216 oct 14 12:07 1004
drwxrwxr-x   2 aru aru    552 oct 27 18:15 Desktop
drwxr-xr-x   2 aru aru    176 oct 20 16:14 RCS
-rw-r--r--   1 aru aru    173 oct 14 15:45 TODO
drwxr-xr-x   6 aru aru    400 sep 30 20:43 acuario
-rwxr-xr-x   1 aru aru    904 nov 11 20:23 baja.kernel.nuevo
drwxr-x---   3 aru aru   2168 nov  8 17:23 bin
lrwxrwxrwx   1 aru aru     13 abr 21  2004 biopython -> src/biopython
drwx------   2 aru aru     88 nov 11 20:21 boot
drwxr-xr-x   7 aru aru    272 oct 14 11:22 doc
drwxr-xr-x   2 aru aru    128 nov 11 20:15 dwld_tmp_kernel
lrwxrwxrwx   1 aru aru     30 mar  7  2004 empresas -> /home/aru/var/misc/empresas
drwxrwxr-x   2 aru aru    176 nov 11 17:17 etc
drwx------   9 aru aru    632 nov 12 10:08 evolution
drwxr-xr-x   2 aru aru    496 may 14 10:01 gnupg
-rw-r--r--   1 aru aru   1353 nov  1 14:47 hmontoliu_pubkey.gpg
drwxr-xr-x   4 aru aru    632 nov  8 12:15 lib
-rw-r--r--   1 aru aru 327931 nov 11 17:18 list_cooker
drwxr-xr-x   3 aru aru   6824 oct 24 18:21 list_empr_alim
-rwxr-xr-x   1 aru aru    779 nov 12 11:58 ls.adv
-rw-r--r--   1 aru aru     59 nov 11 20:21 myresult
drwxr-xr-x   3 aru aru    120 oct 14 11:39 opt
drwxr-xr-x   6 aru aru    168 nov  8 20:23 salmon
drwxr-xr-x   3 aru aru    216 nov 12 11:55 solotemp
drwxr-xr-x  10 aru aru    312 oct 14 11:50 src
-rw-r--r--   1 aru aru    984 nov  8 11:56 test.hw.test.sh
drwxr-xr-x   6 aru aru   1888 nov 12 11:26 tmp
lrwxrwxrwx   1 aru aru     10 feb 18  2004 trucos -> doc/trucos
drwxr-xr-x   8 aru aru    224 nov  8 17:04 var
lrwxrwxrwx   1 aru aru     14 abr 21  2004 webscripts -> src/webscripts
drwxrwxr-x   3 aru aru     80 oct 15 17:23 workspace
Total bytes: 348018
Total files:     7
Directories:    24
And scripts:     3
aru@paleo ~$

 

HTH

Edited by aru
Link to comment
Share on other sites

...the same improved to output all the file types:

 

#! /bin/bash

# produces Big_Ballers desired output.

DIR=${@:-./}

# first output:

LS_L=$(/bin/ls -l $DIR)

echo "$LS_L"

# second output (total bytes in listing):

printf "\nTotal bytes: %5d\n" $(echo "${LS_L}"|awk '{X=X+$5}END{print X}')

# third output (file types):

DIRECTORIES=0 TOTAL_FILES=0 SCRIPTS=0

FILES=$(/bin/ls $DIR)

for file in $FILES;

do

    if [ -d $file ]; then

        DIRECTORIES=$(( ++DIRECTORIES ))

    else

        TOTAL_FILES=$(( ++TOTAL_FILES ))

    fi

done

FINAL_OUTPUT="Total files: %5d\nDirectories: %5d\n"

printf "$FINAL_OUTPUT" $TOTAL_FILES $DIRECTORIES

 

# improved filetype stuff

echo

echo "File types found:"

file $DIR/* | sed 's/.*:[ ]*//' |\

awk -F ':' '{type[$NF]++} END{

    for (i in type)

        printf("%d file(s) found is(are) \"%s\"\n", type, i)| "sort"

    }' | grep -v directory

 

so now the output becomes:

aru@paleo ~$ ./ls.adv.2
total 364
drwxr-xr-x   4 aru aru    216 oct 14 12:07 1004
drwxrwxr-x   2 aru aru    584 nov 12 17:12 Desktop
drwxr-xr-x   2 aru aru    176 oct 20 16:14 RCS
-rw-r--r--   1 aru aru    179 nov 12 17:29 TODO
drwxr-xr-x   6 aru aru    400 sep 30 20:43 acuario
-rwxr-xr-x   1 aru aru    904 nov 11 20:23 baja.kernel.nuevo
drwxr-x---   3 aru aru   2168 nov  8 17:23 bin
...
drwxr-xr-x   3 aru aru    216 nov 12 11:55 solotemp
drwxr-xr-x  10 aru aru    312 oct 14 11:50 src
-rw-r--r--   1 aru aru    984 nov  8 11:56 test.hw.test.sh
drwxr-xr-x   6 aru aru   1856 nov 12 16:56 tmp
lrwxrwxrwx   1 aru aru     10 feb 18  2004 trucos -> doc/trucos
drwxr-xr-x   8 aru aru    224 nov  8 17:04 var
lrwxrwxrwx   1 aru aru     14 abr 21  2004 webscripts -> src/webscripts
drwxrwxr-x   3 aru aru     80 oct 15 17:23 workspace

Total bytes: 349003
Total files:     9
Directories:    25
 
File types found:
1 file(s) found is(are) "ASCII text, with CRLF line terminators"
1 file(s) found is(are) "PGP armored data public key block"
1 file(s) found is(are) "symbolic link to `/home/aru/var/misc/empresas'"
1 file(s) found is(are) "symbolic link to `doc/trucos'"
1 file(s) found is(are) "symbolic link to `src/biopython'"
1 file(s) found is(are) "symbolic link to `src/webscripts'"
3 file(s) found is(are) "ASCII text"
4 file(s) found is(are) "Bourne-Again shell script text executable"
aru@paleo ~$

 

or

aru@paleo ~$ ./ls.adv.2 bin/
total 386
-rwx------  1 aru aru  1670 may  9  2003 CVSPortal.sh
drwxrwxr-x  2 aru aru  1736 nov  8 17:23 RCS
-rw-rw-r--  1 aru aru  3834 abr 22  2003 README.list
-rwx------  1 aru aru   579 may  9  2003 UxName
...
...
-rwxr-xr-x  1 aru aru  2886 oct 14 20:28 vimdsktop
-rwx------  1 aru aru  1829 dic 31  2003 webscripts
-rwxr-xr-x  1 aru aru   339 jul 23  2003 webscriptsalt
-rwx------  1 aru aru  1508 abr 11  2004 webword2.py
-rwx------  1 aru aru   522 oct 14 11:39 x2.sh
-rwxr-xr-x  1 aru aru   498 feb  3  2004 xineDVDiso
-rwxr-xr-x  1 aru aru  2312 may  9  2003 xinitrc.sh

Total bytes: 262356
Total files:    71
Directories:     2

File types found:
1 file(s) found is(are) "ISO-8859 English text"
1 file(s) found is(are) "broken symbolic link to `/home/tete/src/programas/backup/bin/backup.sh'"
1 file(s) found is(are) "empty"
...
...
1 file(s) found is(are) "symbolic link to `nuevo_script.sh'"
1 file(s) found is(are) "symbolic link to `nuevoscript'"
3 file(s) found is(are) "Bourne shell script text executable"
3 file(s) found is(are) "a /usr/bin/python script text executable"
55 file(s) found is(are) "Bourne-Again shell script text executable"

 

The output becomes interesting, maybe if I develop the script it will be helpfull for me to mantain my ~aru folder which has loads of old files many empty, broken or who knows :P

Link to comment
Share on other sites

Amazing, absolutely amazing how you can just dream up a script in such a short time :o

And don't go telling me its easy. :D

:thumbs:

 

Moving this to the Tips & Tricks forum

Link to comment
Share on other sites

localhost:~# apt-get install tree
Reading Package Lists... Done
Building Dependency Tree... Done
The following NEW packages will be installed:
 tree
0 upgraded, 1 newly installed, 0 to remove and 249 not upgraded.
Need to get 26.6kB of archives.
After unpacking 94.2kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com hoary/universe tree 1.5.0-1 [26.6kB]
Fetched 26.6kB in 0s (29.9kB/s)

Preconfiguring packages ...
Selecting previously deselected package tree.
(Reading database ... 78535 files and directories currently installed.)
Unpacking tree (from .../archives/tree_1.5.0-1_i386.deb) ...
Setting up tree (1.5.0-1) ...
localhost:~# cd dload/gib
localhost:~/dload/gib# tree -sugD
.
|-- [root     root           27842 Sep 10 20:39]  gib-0.2-bin.tar.gz
|-- [500      500             7431 Sep  9 17:13]  gib-hound.png
|-- [500      500            24064 Sep  9 17:13]  gib.exe
|-- [500      500            94131 Sep  9 17:13]  gib.glade
`-- [root     root            4096 Sep 10 20:40]  icons
   |-- [500      500             2969 Sep  9 17:13]  apps
   |-- [500      500              289 Sep  9 17:13]  devices
   |-- [500      500              888 Sep  9 17:13]  emblems
   |-- [500      500              454 Sep  9 17:13]  filesystems
   |-- [500      500             2709 Sep  9 17:13]  mimetypes
   `-- [500      500              168 Sep  9 17:13]  stock

1 directory, 10 files
localhost:~/dload/gib#

doesn't give a total though.

Link to comment
Share on other sites

doesn't give a total though.

and doesn't give filetype info either, which was the interesting part of Big_Baller's output ;)

 

And don't go telling me its easy.  :)

Believe me, it is :P ... but thanks for those nice words :headbang:

Link to comment
Share on other sites

I've edited the script to make it a bit more useful (though it is not yet finished):

#! /bin/bash
# produces Big_Ballers desired output.
DIR=${@:-./}
# first output:
LS_L=$(/bin/ls -l $DIR)
echo "$LS_L"
# second output (total bytes in listing):
printf "\nTotal bytes: %5d\n" $(echo "${LS_L}"|awk '{X=X+$5}END{print X}')
# third output (file types):
DIRECTORIES=0 TOTAL_FILES=0 SCRIPTS=0
FILES=$(/bin/ls $DIR)
REAL_FILES="" # added <--
for file in $FILES;
do
   if [ -d $file ]; then
       DIRECTORIES=$(( ++DIRECTORIES ))
   else
       TOTAL_FILES=$(( ++TOTAL_FILES ))
       REAL_FILES="${REAL_FILES} $DIR/$file" # added <--
   fi
done
FINAL_OUTPUT="Total files: %5d\nDirectories: %5d\n"
printf "$FINAL_OUTPUT" $TOTAL_FILES $DIRECTORIES
                                                                                                                                    
# improved filetype stuff # edited <--
echo
echo "File types found:"
file $REAL_FILES | sed 's/: */:/g' |\
awk -F ':' '{files[$NF]=(files[$NF]"\t"$1"\n"); type[$NF]++} END{
   for (i in type) {
           printf("%d file(s) found is(are) \"%s\"\n", type[i], i)
           print files[i] }
   }'

the parts I've changed are marked with '<--'

 

Now the output is (srtipped some parts):

aru@paleo ~$ ./ls.adv.2
total 383
drwxr-xr-x   4 aru aru    216 oct 14 12:07 1004
drwxrwxr-x   2 aru aru   7144 nov 12 18:23 CASA
-rw-r--r--   1 aru aru    466 oct 14 11:58 CASA.COSASxHACER
-rw-rw-r--   1 aru aru      9 abr 25  2004 foobar

[...]
drwxr-xr-x  10 aru aru    312 oct 14 11:50 src
-rw-r--r--   1 aru aru    984 nov  8 11:56 test.hw.test.sh
drwxr-xr-x   6 aru aru   1856 nov 13 12:14 tmp
lrwxrwxrwx   1 aru aru     10 feb 18  2004 trucos -> doc/trucos
drwxr-xr-x   8 aru aru    224 nov  8 17:04 var
lrwxrwxrwx   1 aru aru     14 abr 21  2004 webscripts -> src/webscripts
drwxrwxr-x   3 aru aru     80 oct 15 17:23 workspace

Total bytes: 357499
Total files:    11
Directories:    28

File types found:
1 file(s) found is(are) "ASCII news text"
       .//arti

4 file(s) found is(are) "Bourne-Again shell script text executable"
       .//baja.kernel.nuevo
       .//ls.adv
       .//ls.adv.2
       .//test.hw.test.sh

1 file(s) found is(are) "ASCII text, with CRLF line terminators"
       .//list_cooker

1 file(s) found is(are) "ISO-8859 text"
       .//CASA.COSASxHACER

3 file(s) found is(are) "ASCII text"
       .//foobar
       .//TODO
       .//myresult

1 file(s) found is(are) "PGP armored data public key block"
       .//hmontoliu_pubkey.gpg

aru@paleo ~$

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...