Shell Script To Run Commands In Users Home Directory

Shell script that run linux commands in all users home directories and outputs its results. Scripts accepts user name and command as the argument.

Shell Script

#Next line tell with shell to use

#we store in variable path to file

#We create function with name Usage
Usage () {

#printing help
echo "$0 - execute command in all HOME directories

usage: $0 [-a] command [arg ...]

-a: process HOME directories of all users

The given command will be executed with the arguments specified in the

home directory of users.


#exit from program
exit 1

#end of function

#we check if there less than 1 parameter
if [ $# -lt 1 ]


#if less – print help

#in other case

#we check passed parameters, starting from first
case "$1" in

#if parameter -a then we write to All variable value true
-a) All=true

#removing parameter
shift ;;

#if parameter starting with -, and not -a, we print help
-*) Usage ;;

#exit from case

#exit from fi loop

#we check if user pass -a as parameter
if [ "$All" = true ]

#if -a passed

#we get list of all users
cat /etc/passwd | cut -d':' -f1 | sed -e 's/:/ /g' > $UserListFile

# in other case

#print message
echo "Please enter users, type [ to exit"

#cleaning file with userlist
echo -n > $UserListFile

#starting while loop to read users
while read UserName Rest


#if user print [ to exit loop
if [ $UserName = "[" ]; then

#exit from loop

#if user print other data
#we check if user put any symbol

test -z "$UserName" && continue
#we check if user exist
grep "^$UserName" /etc/passwd > /dev/null || {

#if not exist print message to user
echo "Can't find user ${UserName}; ignored."

#we continue loop


#if user exist – we store it to userfile
echo $UserName >> $UserListFile

#exit of if loop

#exit from reading user input

#exit from if loop

#just print empty line
echo ""

#stop all other parameters as command we need to run
Command="[email protected]"

#getting list of users
UserList=`cat $UserListFile`

#getting user one by one from list
for user in ${UserList}


#one more empty line
echo ""

#getting user home directory
HomeDir=`grep "^$user" /etc/passwd | cut -d":" -f6`

# we check if we have read and execute permissions to cd and run command
[ -r "$HomeDir" -a -x "$HomeDir" ] || {

# if not just warn user
echo "Read or execute of folder $HomeDir denied"


#exit of check

#we telling user about what command we run and inside what directory
echo "Runnig $Command inside $HomeDir"

#entering home
cd $HomeDir

#running command

#cleaning, removing temp files
rm -rf $UserListFile

Sccript Output

./ ls

Please enter users, type exit to exit




Runnig ls inside /var/run/sshd

Runnig ls inside /home/yevhen

Desktop Downloads examples.desktop Pictures Templates

Documents Dropbox Music Public Videos

shell script run command home directory

Bobbin Zachariah 8:43 pm

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.