Search code examples
bashshellls

bash script list files from given user


I have a problem with this one. It is constantly returning me, not a directory, but is certainly is

#!/usr/local/bin/bash  
DIR=$1  
if [ -d "$DIR" ]; then  
        ls -1Apl /home/$DIR | grep -v /\$  
else  
        echo "not a directory"
fi

One more thing, I need a little hint. I have to list files from a given user in a given directory, where I get both the user and directory as parameters. Just suggestions, please.


Solution

  • Are you in the /home directory when you run this? If not, you may want to change it to:

    if [ -d "/home/$DIR" ]; then
    

    to match the ls command. This is assuming you're running it with something like myscript pax to examine the /home/pax directory, which seems to be the case.

    And if you want to only list those files in there owned by a specific user, you can use awk to only print those with column 3 set to the desired value ($usrnm), something like:

    ls -1Apl /home/$DIR | grep -v /\$ | awk -v user=${usrnm} '$3==user{print}{}'