How to check if linux user has .sh file?

0

Issue

I have to write script in bash that will check if logged users have any .sh files.
Checking who is logged in is simple just using:

w| awk '{print $1}'

But i have no idea how to check if they havy any .sh files

Solution

You need to read the output of the who command and use that in your find command.

Since the same user can be logged in multiple times, it’s a good idea to remove duplicates before looping.

#!/bin/bash
who| awk '{print $1}' | sort -u | while read -r username; do
    find /home/"$username" -name "*.sh"
done

Answered By – Barmar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More