#!/bin/bash #Write the shell program which produces a report from the output of ls -l in the following format # file1 # file2 # [DIR] test/ # Total regular files : 7 # Total directories : 4 # Total symbolic links : 0 # Total size of regular files : 2940 # -------------------------------------------------------------------- # This is a free shell script under GNU GPL version 2.0 or above # Copyright (C) 2005 nixCraft project. # Feedback/comment/suggestions : http://cyberciti.biz/fb/ # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- rf=0 dir=0 syml=0 totsize=0 output="" for f in * do if [ -f $f ] then output=$f ((rf++)) size=$(ls -l "${f}" | awk '{ print $5}') totsize=$((totsize+size)) fi if [ -d $f ] then output="[DIR] $f/" ((dir++)) fi if [ -L $f ] then output="[LINK] $f@" ((syml++)) fi echo $output done echo "Total regular files : $rf" echo "Total directories : $dir" echo "Total symbolic links : $syml" echo "Total size of regular files : $totsize"
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 3 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Thanks a lot for all these examples. Very useful to learn shell scripts for a newbie like me :)
Hi
there is a problem with your script
when you execute it , if there is any file or directory which it’s name contains ” ” ( i mean space ) , you would get some error.
to deal this error you should modify all your if clause to :
if [ -f “$f” ]
another note is that , we expect a “ls” comand to accept an argument as a path , for example : ls /etc/ —> should list all the files and directories in /etc/.
to deal this i suggest you to modify these lines :
output=””
cd “$1” ##this line is added between the 2 lines you had##
for f in *
.
.
.
echo “Total size of regular files : $totsize”
cd “-” > /dev/null ## and this line is added at the end of script##
——————
thnx for your nice ideas in scripting
—
Best Regards , H.Noroozi
nixcraft site is absolutely such a helpful site to me.
its very user friendly especially to those who are newbie in linux like me.
easy to understand and gives a good example. thanks to nixcraft!!