Shell script question

N

Niwrad

Guest
Is there a shell script or command that will list out the permissions for every directory from a file's path?

e.g: If we wanted to run the script on ~/dir1/dir2/dir3/file, the desirable output would be something like:

drwxrwxr-t 333 root admin 333 Jun 3 13:33 Users
drwxr-xr-x 33 $shortname $shortname 333 Jun 3 13:33 $shortname
drwxr-xr-x 3 $shortname $shortname 333 May 3 13:33 dir1
drwxr-xr-x 3 $shortname $shortname 333 May 3 13:33 dir2
drwxr-xr-x 3 $shortname $shortname 333 May 3 13:33 dir3
-rwxr-xr-x 3 $shortname $shortname 333 May 3 13:33 file
 
Joined
Dec 30, 2002
Messages
2,118
Reaction score
23
Points
38
Location
Sunny So Cal
Your Mac's Specs
G5•2x1.8•1.5•320•8x+/-

rman


Retired Staff
Joined
Dec 24, 2002
Messages
12,637
Reaction score
168
Points
63
Location
Los Angeles, California
Your Mac's Specs
14in MacBook Pro M1 Max 32GB 2TB
ls -al will give you a long list of what you have in the current directory, including the dot files (hidden files).
 

rman


Retired Staff
Joined
Dec 24, 2002
Messages
12,637
Reaction score
168
Points
63
Location
Los Angeles, California
Your Mac's Specs
14in MacBook Pro M1 Max 32GB 2TB
Another question occurs to me. Do you mean only the top most directories/folders in the current directory/folder or all of the directories/folders downward in the current directory/folder? Looking at your example, it appears staright forward, just want to be sure.
 
OP
N

Niwrad

Guest
What I'm interested in is not the files in a directory, but the files that compose the absolute path of the directory. One possible use for such a script would be to identify permission problems when troubles arise in opening a file. The script should show ONLY the file itself and its super-directories along with their "ls -l" info.
 

rman


Retired Staff
Joined
Dec 24, 2002
Messages
12,637
Reaction score
168
Points
63
Location
Los Angeles, California
Your Mac's Specs
14in MacBook Pro M1 Max 32GB 2TB
Here is my understanding of what you want. You want to parse a string of directories, down to the file. At each directory level you want the file permissions of that directory. So if a file was located in the following path (i.e. /export/home/niward/direct1/direct2/file). So you would the permission of the export directory, followed by the home directory, and the niward, etc. Is this what you mean?
 

rman


Retired Staff
Joined
Dec 24, 2002
Messages
12,637
Reaction score
168
Points
63
Location
Los Angeles, California
Your Mac's Specs
14in MacBook Pro M1 Max 32GB 2TB
I believe that a perl script can handle that job easier. I have a little understanding of perl. It would take some time and a little work to create a shell script.

What is it that you are trying to do? Maybe there is another solution to your problem.
 
OP
N

Niwrad

Guest
Is there any way to use a loop that will start from the top directory and work its way through the path? I think I could get this script to work if I knew how to make the list command treat a directory like a file when listing, i.e. when given a directory argument, the ls -l command would list the directory information as opposed to the information of its contents. Does anyone know of a command on Darwin that allows you to simply get a directory's info (in long format)?
 

rman


Retired Staff
Joined
Dec 24, 2002
Messages
12,637
Reaction score
168
Points
63
Location
Los Angeles, California
Your Mac's Specs
14in MacBook Pro M1 Max 32GB 2TB
Use ls -ld to only list the directory and not its contents.
 
OP
N

Niwrad

Guest
So basically if we want the whole path we need a script that executes something like the following series of commands:

For "/export/home/niward/direct1/direct2/file"

ls -l file > file_info
ls -ld >> file_info
cd ..
ls -ld >> file_info
cd ..
ls -ld >> file_info
cd ..
etc..........

This loop seems like it might work, and you can run the loop until[ -e ../ ] is false. Will it work?

I know how to do loop "checks", but don't know how to loop a command yet, anyone know how to do this? If so, what is the general form?
 
OP
N

Niwrad

Guest
Here is my attempt at solving the problem using a loop. It creates the "$file info" file, but only contain the file and its parent. What am I doing wrong?

1 #!/bin/sh
2 # This script creates a file listing the permissions that affect the acc essability of a file.
3
4 file="$1"
5
6 ls -ld $file > "$file info"
7
8 if [ -e ../ ] ; then
9 ls -ld >> "$file info"
10
11 while [ -e ../ ] ; do
12 cd ..
13
14 done
15 fi
 

rman


Retired Staff
Joined
Dec 24, 2002
Messages
12,637
Reaction score
168
Points
63
Location
Los Angeles, California
Your Mac's Specs
14in MacBook Pro M1 Max 32GB 2TB
There is a better way. Before I go farther, how many times are you going to run this script that you are trying to create?

You can move to the directory you want and then parse the path string. That is why perl would be a better solution.
 
OP
N

Niwrad

Guest
I haven't learned Perl yet (it's on my to-do list this summer), but have heard that it is a great tool to have in your box. Right now I'm just focusing on getting comfortable with my OS. Such a script would in application be actually quite useful, but I'm more interested in the process of solving the problems using shell script so I can become more familiar with them and there basic forms.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top