Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/bin/bash
dir=~
readable=0
writeable=0
executeable=0
count=0
for file in `ls $dir`
do
if [ -r $file ]
then
let readable+=1
fi
if [ -w $file ]
then
let writeable+=1
fi
if [ -x $file ]
then
let executeable+=1
fi
let count+=1
done
echo "$count file(s)"
echo "There are $readable readable file(s)"
echo "There are $writeable writeable file(s)"
echo "There are $executeable executeable file(s)"
Your script kind of came late as I had to think to make one my self.
I thought some thing along these lines:
Code:
This will give you number of files with readable permission
Code:
ls -l | grep "r" | wc |awk '{print $1}'
This will give you number of files with writable permission
Code:
ls -l | grep "w" | wc |awk '{print $1}'
This will give you number of files with execute permission
Code:
ls -l | grep "x" | wc |awk '{print $1}'
What do you think,
Does it do the job too, not sure if it works every time.
It might not work if the executable file has a w in the name it'll count it as writable, it will give you 1 file if run it against writable file check, and so on.
Like neo said if the name, user or group has a r w x in it then your count will be inaccurate. You could have modified it to something like this to make sure it only checks the permissions:
ls -l | cut -f 1 -d ' ' | grep r | wc | awk '{print $1}'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.