Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I use a wild card in chmod?

151 views
Skip to first unread message

T

unread,
Sep 22, 2015, 12:15:23 AM9/22/15
to
Hi All,

A single file name works

$ perl -We 'chmod 0755, "/home/linuxutil/perl/VecTest.pl";'

But a "*" wild card does not:


$ perl -We 'chmod 0755, "/home/linuxutil/perl/*";'
$ perl -We 'chmod 0755, "/home/linuxutil/perl/\*";'
$ perl -We 'chmod 0755, "/home/linuxutil/perl/{*}";'

No errors, attributes are unchanged. What am I doing wrong?

Many thanks,
-T

Jim Gibson

unread,
Sep 22, 2015, 1:10:29 AM9/22/15
to
Perl is not a shell, and does not automatically do file name expansion
on shell wildcard characters. You are trying to change the permissions
on a file called '/home/linuxutil/perl/*' which doesn't exist. It is
not an error to pass a non-existent file to chmod (apparently). chmod
returns the number of files successfully changed, which will be zero in
this case.

Your best approach would be to find the actual files in the directory
with the File::Find module or the combination of the opendir and
readdir functions.

You could also try the glob function, which does do file name
expansion. See 'perldoc -f glob'.

--
Jim Gibson

gamo

unread,
Sep 24, 2015, 1:31:09 PM9/24/15
to
El 22/09/15 a las 06:15, T escribió:
You are showing the Troll flag with that stupid one liners.

--
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance

T

unread,
Sep 25, 2015, 4:43:04 PM9/25/15
to
On 09/24/2015 10:30 AM, gamo wrote:
> El 22/09/15 a las 06:15, T escribió:
>> Hi All,
>>
>> A single file name works
>>
>> $ perl -We 'chmod 0755, "/home/linuxutil/perl/VecTest.pl";'
>>
>> But a "*" wild card does not:
>>
>>
>> $ perl -We 'chmod 0755, "/home/linuxutil/perl/*";'
>> $ perl -We 'chmod 0755, "/home/linuxutil/perl/\*";'
>> $ perl -We 'chmod 0755, "/home/linuxutil/perl/{*}";'
>>
>> No errors, attributes are unchanged. What am I doing wrong?
>>
>> Many thanks,
>> -T
>
> You are showing the Troll flag with that stupid one liners.
>

Oh gage me. Everyone has to learn sometime. You obviously
don't know the answer either.

T

unread,
Sep 25, 2015, 4:49:04 PM9/25/15
to
So something like

opendir $DIR, ${WorkingDir};
while ( $dir_entry = readdir $DIR ){
common language: do something with $dir_entry
}


I know I can test this, but if you know off the top of
your head (no doing my homework for me), does "readdir"
populate when you "opendir" or does "readdir" do a fresh
populate every time you call it?

Thank you for helping me with this!
-T


Benoit Izac

unread,
Sep 25, 2015, 6:19:14 PM9/25/15
to
Hello,

On Friday, September 25 2015 at 22:48, T. wrote in message
<mu4bs4$fiu$1...@dont-email.me>:

>> Your best approach would be to find the actual files in the directory
>> with the File::Find module or the combination of the opendir and
>> readdir functions.
>>
>> You could also try the glob function, which does do file name
>> expansion. See 'perldoc -f glob'.
>
> So something like
>
> opendir $DIR, ${WorkingDir};

What's append if $WorkingDir does not exist or if you don't have read
permission on it?

> while ( $dir_entry = readdir $DIR ){
> common language: do something with $dir_entry
> }

This is one of the three ways Jim gave you.

> I know I can test this, but if you know off the top of
> your head (no doing my homework for me), does "readdir"
> populate when you "opendir" or does "readdir" do a fresh
> populate every time you call it?

Did you try 'perldoc -f opendir' and 'perldoc -f readdir'? I guess you
didn't...

--
Benoit Izac

T

unread,
Sep 25, 2015, 7:00:48 PM9/25/15
to
On 09/25/2015 03:19 PM, Benoit Izac wrote:
> Hello,
>
> On Friday, September 25 2015 at 22:48, T. wrote in message
> <mu4bs4$fiu$1...@dont-email.me>:
>
>>> Your best approach would be to find the actual files in the directory
>>> with the File::Find module or the combination of the opendir and
>>> readdir functions.
>>>
>>> You could also try the glob function, which does do file name
>>> expansion. See 'perldoc -f glob'.
>>
>> So something like
>>
>> opendir $DIR, ${WorkingDir};
>
> What's append if $WorkingDir does not exist or if you don't have read
> permission on it?


I was only asking a theory question. I copied it from a piece
of working code. And yes, there is a "-d" test before
I open the guy. If it fails, I create the directory.
Then "opendir" can open in peace.

Most examples use "or die", which will not work for
me in anything other than a one liners. (I will
stick with "return" to dump out of a sub.)

>
>> while ( $dir_entry = readdir $DIR ){
>> common language: do something with $dir_entry
>> }
>
> This is one of the three ways Jim gave you.

True


>> I know I can test this, but if you know off the top of
>> your head (no doing my homework for me), does "readdir"
>> populate when you "opendir" or does "readdir" do a fresh
>> populate every time you call it?

> Did you try 'perldoc -f opendir' and 'perldoc -f readdir'? I guess you
> didn't...

Actually, I did not until you mentioned it. It doesn't
answer the question. So, poop.

And, I only wanted to know off the top of his head. Anything
more than that and I need to test it myself.

It would seem a waste of space to populate on "opendir" when
you don't even know what someone opened the directory, but
I have guessed wrong before.


gamo

unread,
Sep 25, 2015, 8:02:04 PM9/25/15
to
El 25/09/15 a las 22:42, T escribió:
I know that

@list = <.* *>;
chmod "0755", @list;

will do. But there is nonsense in doing that in a one liner
when you could just

$ sudo chmod 0755 *

in the shell. How many times it's necesary to change the
permisions? A nonsense question, thus.

T

unread,
Sep 25, 2015, 8:53:51 PM9/25/15
to
When I download things, I get varying permissions depending
on the type of file downloaded. I can look them up if you like.

None of them are what I need, which is 0766 and 0544. I can
explain that one too, if you like. So I am constantly having
to go and change things. (It is not obvious if you are not
familiar with Samba.)

So I like to add "chmod 0766 *" when I exit the directory
just for housekeeping. This includes test files I have
written that take on the default permission.

<code>
$ perl -We '@list = <.* *>;chmod 0755, @list;'; ls -al
</code>

worked. Thank you.

I am drawing a blank on how to set the path:
I am trying to get away from Bash, otherwise I'd just use
qx (...) and not have to learn anything new.

When I download things, I get varying permissions depending
on the type of file downloaded. I can look them up if you like.

None of them are what I need, which is 0766 and 0544. (I can
explain that one too, if you like.) So I am constantly having
to go and change things. (It is not obvious why if you are not
familiar with Samba.)

So I like to add "chmod 0766 *" when I exit the directory
just for housekeeping. This includes text files I have
written that take on the default permission. (I am always
writing myself notes.)

<code>
$ perl -We '@list = <.* *>;chmod 0755, @list;'; ls -al
</code>

worked. Thank you.

I am drawing a blank on how to set the path:

$ perl -We '@list = qw ("/home/linuxutil/perl/<.*"
"/home/linuxutil/perl/*>"); chmod 0766, @list;'

doesn't work

Do you know how to set the path?

Thank you for helping me with this,
-T

gamo

unread,
Sep 25, 2015, 9:16:05 PM9/25/15
to
El 26/09/15 a las 02:53, T escribió:
> Do you know how to set the path?

There are various maners, like using a module,
but the problem is

$curdir = `pwd`; # record the current dir
chdir "/home/linuxutils/perl/";
[do the permisissions thing]
chdir $curdir;

But there are opinions on both non using modules
and in non using backticks. So, another form could
be valid using

use Cwd;
my $curdir = getcwd;

T

unread,
Sep 25, 2015, 10:18:29 PM9/25/15
to
On 09/25/2015 06:15 PM, gamo wrote:
> El 26/09/15 a las 02:53, T escribió:
>> Do you know how to set the path?
>
> There are various maners, like using a module,
> but the problem is
>
> $curdir = `pwd`; # record the current dir
> chdir "/home/linuxutils/perl/";
> [do the permisissions thing]
> chdir $curdir;
>
> But there are opinions on both non using modules
> and in non using backticks. So, another form could
> be valid using
>
> use Cwd;
> my $curdir = getcwd;
>

That explains it. Thank you.

Benoit Izac

unread,
Sep 26, 2015, 6:20:45 AM9/26/15
to
Hello,

On Saturday, September 26 2015 at 01:00, T. wrote in message
<mu4jj4$f90$1...@dont-email.me>:

>>>> Your best approach would be to find the actual files in the directory
>>>> with the File::Find module or the combination of the opendir and
>>>> readdir functions.
>>>>
>>>> You could also try the glob function, which does do file name
>>>> expansion. See 'perldoc -f glob'.
>>>
>>> So something like
>>>
>>> opendir $DIR, ${WorkingDir};
>>
>> What's append if $WorkingDir does not exist or if you don't have read
>> permission on it?
>
> I was only asking a theory question. I copied it from a piece
> of working code. And yes, there is a "-d" test before
> I open the guy. If it fails, I create the directory.
> Then "opendir" can open in peace.

Not always. Another program could remove the directory after your "-d"
test or after you create the dir, and opendir will fail. Search for
"race condition" in your favourite search engine.

> Most examples use "or die", which will not work for
> me in anything other than a one liners. (I will
> stick with "return" to dump out of a sub.)

if (opendir $DIR, ${WorkingDir}) {
# opendir success
while (my $filename = readdir $DIR) { # readdir is in scalar context
# do something with $WorkingDir/$filename
}
closedir $DIR
} else {
# opendir failed
}

>>> I know I can test this, but if you know off the top of
>>> your head (no doing my homework for me), does "readdir"
>>> populate when you "opendir" or does "readdir" do a fresh
>>> populate every time you call it?
>
>> Did you try 'perldoc -f opendir' and 'perldoc -f readdir'? I guess you
>> didn't...
>
> Actually, I did not until you mentioned it. It doesn't
> answer the question. So, poop.

% perldoc -f readdir | head -n 5
readdir DIRHANDLE
Returns the next directory entry for a directory opened by
"opendir". If used in list context, returns all the rest of the
entries in the directory. If there are no more entries, returns
the undefined value in scalar context and the empty list in list

So:
1) opendir
2) readdir in scalar context returns the first entry
3) readdir in scalar context returns the next entry
...
n-1) readdir in scalar context returns the last entry
n) readdir in scalar context returns undef
n+1) closedir

Or:
1) opendir
2) readdir in list context returns all entries or empty list
3) closedir

What you don't understand here?

--
Benoit Izac

Benoit Izac

unread,
Sep 26, 2015, 6:42:48 AM9/26/15
to
Hello,

On Saturday, September 26 2015 at 02:53, T. wrote in message
<mu4q73$33l$1...@dont-email.me>:

> I am drawing a blank on how to set the path:
>
> $ perl -We '@list = qw ("/home/linuxutil/perl/<.*"
> "/home/linuxutil/perl/*>"); chmod 0766, @list;'

$ perl -e 'chdir "/home/linuxutil/perl" && chmod 0766, <.* *>'

But it's really bad idea because:
1) <.* *> returns "." and ".."
2) files and directories will have the same mode
3) 0766 (the owner can execute something that everybody can modify)

--
Benoit Izac

T

unread,
Sep 26, 2015, 5:20:44 PM9/26/15
to
On 09/26/2015 03:20 AM, Benoit Izac wrote:
> Hello,
>
> On Saturday, September 26 2015 at 01:00, T. wrote in message
> <mu4jj4$f90$1...@dont-email.me>:
>
>>>>> Your best approach would be to find the actual files in the directory
>>>>> with the File::Find module or the combination of the opendir and
>>>>> readdir functions.
>>>>>
>>>>> You could also try the glob function, which does do file name
>>>>> expansion. See 'perldoc -f glob'.
>>>>
>>>> So something like
>>>>
>>>> opendir $DIR, ${WorkingDir};
>>>
>>> What's append if $WorkingDir does not exist or if you don't have read
>>> permission on it?
>>
>> I was only asking a theory question. I copied it from a piece
>> of working code. And yes, there is a "-d" test before
>> I open the guy. If it fails, I create the directory.
>> Then "opendir" can open in peace.
>
> Not always. Another program could remove the directory after your "-d"
> test or after you create the dir, and opendir will fail.

They would have to be really, really fast! i also
am the only one controlling content in these directories.

> Search for
> "race condition" in your favourite search engine.
>
>> Most examples use "or die", which will not work for
>> me in anything other than a one liners. (I will
>> stick with "return" to dump out of a sub.)
>
> if (opendir $DIR, ${WorkingDir}) {
> # opendir success
> while (my $filename = readdir $DIR) { # readdir is in scalar context
> # do something with $WorkingDir/$filename
> }
> closedir $DIR
> } else {
> # opendir failed
> }

I think I will do both. Thank you!
1) I opendir and look for stuff
2) I add stuff to the directory, both from my script and externally
3) I want to change permissions on everything, old
and new, that resides in the current directory

Basically, does readdir populate each time it is called?

T

unread,
Sep 26, 2015, 5:25:47 PM9/26/15
to
On 09/26/2015 03:20 AM, Benoit Izac wrote:
> Hello,
>
> On Saturday, September 26 2015 at 01:00, T. wrote in message
> <mu4jj4$f90$1...@dont-email.me>:
>
>>>>> Your best approach would be to find the actual files in the directory
>>>>> with the File::Find module or the combination of the opendir and
>>>>> readdir functions.
>>>>>
>>>>> You could also try the glob function, which does do file name
>>>>> expansion. See 'perldoc -f glob'.
>>>>
>>>> So something like
>>>>
>>>> opendir $DIR, ${WorkingDir};
>>>
>>> What's append if $WorkingDir does not exist or if you don't have read
>>> permission on it?
>>
>> I was only asking a theory question. I copied it from a piece
>> of working code. And yes, there is a "-d" test before
>> I open the guy. If it fails, I create the directory.
>> Then "opendir" can open in peace.
>
> Not always. Another program could remove the directory after your "-d"
> test or after you create the dir, and opendir will fail.

Going to have to be really fast as the two command follow each other

> Search for
> "race condition" in your favourite search engine.
>
>> Most examples use "or die", which will not work for
>> me in anything other than a one liners. (I will
>> stick with "return" to dump out of a sub.)
>
> if (opendir $DIR, ${WorkingDir}) {
> # opendir success
> while (my $filename = readdir $DIR) { # readdir is in scalar context
> # do something with $WorkingDir/$filename
> }
> closedir $DIR
> } else {
> # opendir failed
> }

Again, going to have to be really fast. The same scenario would affect
this as you state above.
Does the readdir populate itself every time it is called,
or just once when opendir is called.

Thank you for helping me with this.
-T

T

unread,
Sep 26, 2015, 5:35:20 PM9/26/15
to

T

unread,
Sep 26, 2015, 5:39:52 PM9/26/15
to
On 09/26/2015 03:42 AM, Benoit Izac wrote:
Hi Benoit,

Thank you! I wrote it down.

I think what I will have to do to keep things under control
is to use "readdir". A little more worky, but what the heck ...

Don't worry about the 0766. It is a Samba thing for mapping
permissions to Windows. (My computer is a full server mock
up with Windows and Linux clients in Virtual Machines.)

No one can get into those directories without my specific
permission. Once they are there, they are expected to
have full access to everything.

-T

Jim Gibson

unread,
Sep 26, 2015, 6:55:10 PM9/26/15
to
In article <mu7237$ed3$1...@dont-email.me>, <T...@invalid.invalid> wrote:


> 1) I opendir and look for stuff
> 2) I add stuff to the directory, both from my script and externally
> 3) I want to change permissions on everything, old
> and new, that resides in the current directory
>
> Basically, does readdir populate each time it is called?

No, it either returns the next directory entry in scalar context or the
remaining directory entries in list (array) mode.

It is risky to iterate over any data set while simultaneously modifying
that data set. Bad things can happen, depending upon the
implementation. In the case of opendir and readdir, there a
straightforward solution:

1. Open directory with opendir
2. Read all directory entries in list context and save.
3. Call closedir to close directory handle.
4. Iterate over saved entry list and add or delete items to the
directory with impunity.

Also read docs on other ...dir functions mentioned in 'perldoc -f
opendir': telldir, seekdir, and rewinddir. You are still safer closing
and then re-opening a directory than calling rewinddir if you are
modifying the directory.

--
Jim Gibson

gamo

unread,
Oct 1, 2015, 1:26:59 PM10/1/15
to
El 26/09/15 a las 23:39, T escribió:
> On 09/26/2015 03:42 AM, Benoit Izac wrote:
>> Hello,
>>
>> On Saturday, September 26 2015 at 02:53, T. wrote in message
>> <mu4q73$33l$1...@dont-email.me>:
>>
>>> I am drawing a blank on how to set the path:
>>>
>>> $ perl -We '@list = qw ("/home/linuxutil/perl/<.*"
>>> "/home/linuxutil/perl/*>"); chmod 0766, @list;'
>>
>> $ perl -e 'chdir "/home/linuxutil/perl" && chmod 0766, <.* *>'
>>
>> But it's really bad idea because:
>> 1) <.* *> returns "." and ".."
>> 2) files and directories will have the same mode
>> 3) 0766 (the owner can execute something that everybody can modify)
>>
>
> Hi Benoit,
>
> Thank you! I wrote it down.
>
> I think what I will have to do to keep things under control
> is to use "readdir". A little more worky, but what the heck ...
>

You can still use it, solving the problems.

@list = <.* *>;
for (@list){
next if ($_ eq "." || $_ eq ".."); # problem 1
unless (-d $_ ){ # problem 2
chmod 0755, $_; # problem 3
}
}

If you can't figure this out, I wish you luck with any other approach.

> Don't worry about the 0766. It is a Samba thing for mapping
> permissions to Windows. (My computer is a full server mock
> up with Windows and Linux clients in Virtual Machines.)
>
> No one can get into those directories without my specific
> permission. Once they are there, they are expected to
> have full access to everything.
>
> -T


--
http://www.telecable.es/personales/gamo/
http://gamo.eu.pn/

T

unread,
Oct 2, 2015, 3:23:05 AM10/2/15
to
Hi Gamo,

This is what I eventually landed on. For my purposes "."
and ".." should be skipped.

Thank you for helping me with this!

-T

<code>
sub FixOwnerAndPermissions ( $$$$ ) {
my $WorkingDir = $_[0]; # path to directory
my $Permissions = $_[1]; # 0766, etc.
my $UserNumber = $_[2]; # user number from /etc/passwords (name
not allowed)
my $GroupNumber = $_[3]; # group number from /etc/passwords and
/etc/group (no names)

my $DIR;
my $dir_entry;

opendir $DIR, "${WorkingDir}";
while ( $dir_entry = readdir $DIR ){
chmod $Permissions, "${WorkingDir}/${dir_entry}";
chown $UserNumber, $GroupNumber, "${WorkingDir}/${dir_entry}";
}
closedir $DIR;
}
</code>

shar...@hotmail.com

unread,
Oct 2, 2015, 1:37:24 PM10/2/15
to
On Friday, 2 October 2015 12:53:05 UTC+5:30, T wrote:
[snip]

> <code>
> sub FixOwnerAndPermissions ( $$$$ ) {
> my $WorkingDir = $_[0]; # path to directory
> my $Permissions = $_[1]; # 0766, etc.
> my $UserNumber = $_[2]; # user number from /etc/passwords (name
> not allowed)
> my $GroupNumber = $_[3]; # group number from /etc/passwords and
> /etc/group (no names)
>
> my $DIR;
> my $dir_entry;
>
> opendir $DIR, "${WorkingDir}";
> while ( $dir_entry = readdir $DIR ){
> chmod $Permissions, "${WorkingDir}/${dir_entry}";
> chown $UserNumber, $GroupNumber, "${WorkingDir}/${dir_entry}";
> }
> closedir $DIR;
> }
> </code>



sub FixOwnerAndPermissions ( $$$$ ) {
my $WorkingDir = $_[0]; # path to directory
my $Permissions = $_[1]; # 0766, etc.
my $UserNumber = $_[2]; # user num from /etc/passwords (name notallowed)
my $GroupNumber = $_[3]; # group num from /etc/passwords & /etc/group (nonames)

> my $DIR; <--- move this within the opendir statement itself
> my $dir_entry; <--- move this in the while loop

opendir my $DIR, ${WorkingDir} or die "Cannot open dir $WorkingDir: $!";
DIR: while ( my $dir_entry = readdir $DIR ){
next DIR if $dir_entry =~ /^[.][.]?$/;
my $reald = join q{/}, $WorkingDir, $dir_entry;
chmod $Permissions, $reald or
die "Cannot chmod dir $reald: $!"; # a good idea to check status
chown $UserNumber, $GroupNumber, $reald or
die "Cant chown $reald: $!"; # check chown went thru or not
}
closedir $DIR or die "Cannot close dir $WorkingDir: $!";
}
0 new messages