Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ls -l translator script

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
This is an old lab problem that I've never figured out and I give up. I've lost enough sleep over it.

For every file in the current directory, print the owners "Real World name" (in /etc/passwd/(field 5)).

I've come very close using a "for" command... ex.

for file in `ls -l`
do
owner=`awk '{print $3}' $file` #find the owners usrname #This is where my problem is. The "for" parses
#everything in ls -l thats between spaces/tabs.

realname=`grep $owner /etc/passwd | awk '{print $5}'`
echo "$file belongs to $realname"
done

# Am I not worthy!?


 
Here is something I just threw together. It complains on directories sometimes - but it can get you started...

#!/bin/ksh
files="*"
for file in $files ; do
data=`ls -l $file |awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10}'`
user=`echo $data|cut -f3 -d" "`
user2=`grep $user /etc/passwd|cut -f5 -d:`
if [ "$user2" = "" ] ; then
user2="$user"
fi
dataout=`echo "$data"|sed "s/$user/$user2/g"`
echo "$dataout"
done
 
#!/usr/bin/ksh

ls -l|grep -v "^total"|while read LINE
do
FILE=`echo $LINE|awk '{print $NF}'`
OWNER=`echo $LINE|awk '{print $3}'`
REAL_NAME=`grep "$OWNER" /etc/passwd|awk -F: '{print $5}'|head -1`
echo "FILE: $FILE"
echo "OWNER: $OWNER"
echo "REAL NAME: $REAL_NAME"
echo "--------------------------------------------------------------------------------"

done
Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
this will deal with file names containing spaces as well as "un-defined" users.

#!/usr/bin/ksh
for i in `ls -l | tail +2 | awk '{ print $3, $9, $10, $11, $12 }' | tr -s " " ",
"` ; do
OWNER=`echo $i | cut -d "," -f 1`
FNAME=`echo $i | cut -d "," -f 2-6 | tr -s "," " "`
grep $OWNER /etc/passwd > /dev/null 2>&1
EC=$?
if [ $EC = 0 ] ; then
echo "$FNAME belongs to `grep $OWNER /etc/passwd | cut -d ":" -f 5"
else
echo "$FNAME belongs to an un-defined user ID"
fi
done


crowe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top