Question

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.

Was it helpful?

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}{}'

OTHER TIPS

You're not testing for the existence of the same directory as you're trying to list - maybe you mean -d "/home/$DIR"? Or from your requirement, do you have two parameters?

user="$1"
dir="$2"

# and then examine "/home/$user/$dir"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top