How to escape wildcard in path when using sudo -u command [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite













This question already has an answer here:



  • Why isn't this sudo mv operation with wildcard working?

    2 answers



I need to access a file on an NFS fileserver where root squash is turned on. I'm using bash on Arch Linux. I am using this type of command:



# sudo -u authduser ls /mounted/filesystem/path/aa.user.js


This succeeds and the output is:




/mounted/filesystem/path/aa.user.js




However, there are multiple files, such as bb.user.js. When I try the following command it fails:



# sudo -u authduser ls /mounted/filesystem/path/*.user.js


The output is:




ls: cannot access '/mounted/filesystem/path/*.user.js': No such file or directory




I have tried quoting, escaping and various other things without success. What is the correct way?










share|improve this question













marked as duplicate by muru, Ipor Sircer, terdon bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 22 at 13:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • It does not appear to be a duplicate to me. You can see from the output in my question that it is not a permissions issue.
    – MountainX
    Nov 22 at 7:39






  • 1




    No, your output doesn't show that.
    – muru
    Nov 22 at 7:40










  • OK, you're correct. But given that the specifics are the opposite of that question, I think this is still a distinct and different question.
    – MountainX
    Nov 22 at 7:48














up vote
-1
down vote

favorite













This question already has an answer here:



  • Why isn't this sudo mv operation with wildcard working?

    2 answers



I need to access a file on an NFS fileserver where root squash is turned on. I'm using bash on Arch Linux. I am using this type of command:



# sudo -u authduser ls /mounted/filesystem/path/aa.user.js


This succeeds and the output is:




/mounted/filesystem/path/aa.user.js




However, there are multiple files, such as bb.user.js. When I try the following command it fails:



# sudo -u authduser ls /mounted/filesystem/path/*.user.js


The output is:




ls: cannot access '/mounted/filesystem/path/*.user.js': No such file or directory




I have tried quoting, escaping and various other things without success. What is the correct way?










share|improve this question













marked as duplicate by muru, Ipor Sircer, terdon bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 22 at 13:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • It does not appear to be a duplicate to me. You can see from the output in my question that it is not a permissions issue.
    – MountainX
    Nov 22 at 7:39






  • 1




    No, your output doesn't show that.
    – muru
    Nov 22 at 7:40










  • OK, you're correct. But given that the specifics are the opposite of that question, I think this is still a distinct and different question.
    – MountainX
    Nov 22 at 7:48












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite












This question already has an answer here:



  • Why isn't this sudo mv operation with wildcard working?

    2 answers



I need to access a file on an NFS fileserver where root squash is turned on. I'm using bash on Arch Linux. I am using this type of command:



# sudo -u authduser ls /mounted/filesystem/path/aa.user.js


This succeeds and the output is:




/mounted/filesystem/path/aa.user.js




However, there are multiple files, such as bb.user.js. When I try the following command it fails:



# sudo -u authduser ls /mounted/filesystem/path/*.user.js


The output is:




ls: cannot access '/mounted/filesystem/path/*.user.js': No such file or directory




I have tried quoting, escaping and various other things without success. What is the correct way?










share|improve this question














This question already has an answer here:



  • Why isn't this sudo mv operation with wildcard working?

    2 answers



I need to access a file on an NFS fileserver where root squash is turned on. I'm using bash on Arch Linux. I am using this type of command:



# sudo -u authduser ls /mounted/filesystem/path/aa.user.js


This succeeds and the output is:




/mounted/filesystem/path/aa.user.js




However, there are multiple files, such as bb.user.js. When I try the following command it fails:



# sudo -u authduser ls /mounted/filesystem/path/*.user.js


The output is:




ls: cannot access '/mounted/filesystem/path/*.user.js': No such file or directory




I have tried quoting, escaping and various other things without success. What is the correct way?





This question already has an answer here:



  • Why isn't this sudo mv operation with wildcard working?

    2 answers







bash permissions sudo nfs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 7:28









MountainX

4,8972471127




4,8972471127




marked as duplicate by muru, Ipor Sircer, terdon bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 22 at 13:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by muru, Ipor Sircer, terdon bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 22 at 13:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • It does not appear to be a duplicate to me. You can see from the output in my question that it is not a permissions issue.
    – MountainX
    Nov 22 at 7:39






  • 1




    No, your output doesn't show that.
    – muru
    Nov 22 at 7:40










  • OK, you're correct. But given that the specifics are the opposite of that question, I think this is still a distinct and different question.
    – MountainX
    Nov 22 at 7:48
















  • It does not appear to be a duplicate to me. You can see from the output in my question that it is not a permissions issue.
    – MountainX
    Nov 22 at 7:39






  • 1




    No, your output doesn't show that.
    – muru
    Nov 22 at 7:40










  • OK, you're correct. But given that the specifics are the opposite of that question, I think this is still a distinct and different question.
    – MountainX
    Nov 22 at 7:48















It does not appear to be a duplicate to me. You can see from the output in my question that it is not a permissions issue.
– MountainX
Nov 22 at 7:39




It does not appear to be a duplicate to me. You can see from the output in my question that it is not a permissions issue.
– MountainX
Nov 22 at 7:39




1




1




No, your output doesn't show that.
– muru
Nov 22 at 7:40




No, your output doesn't show that.
– muru
Nov 22 at 7:40












OK, you're correct. But given that the specifics are the opposite of that question, I think this is still a distinct and different question.
– MountainX
Nov 22 at 7:48




OK, you're correct. But given that the specifics are the opposite of that question, I think this is still a distinct and different question.
– MountainX
Nov 22 at 7:48










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










The issue with the wildcard is that it's expanded by the shell, before running the sudo command.



If only authduser will be able to list files under /mounted/filesystem/path/, but not your user who is running the sudo command, then the shell will not be able to expand the wildcard and will pass it unmodified. Since ls itself doesn't expand wildcards, this will result in the error you're seeing.



See Why isn't this sudo mv operation with wildcard working? for more details on that.




For a solution to this problem, you can use sudo's -s argument, which spawns a shell as the target user (rather than simply executing a command directly), in which case if you also give it a command, it will be passed to the shell using the -c option (which is the usual option for the shell to interpret and execute a command, including wildcard expansion.)



In that case, you'll want to put the command within quotes (preferably single quotes), so that the shell running the sudo command will not try to interpret the command, only the one spawned by sudo as the target user:



# sudo -u authduser -s 'ls /mounted/filesystem/path/*.user.js'


See the man page of sudo for more details, in particular take a look at the -s option.



UPDATE: It's not really possible to use sudo -s with a command here. See “sudo -s ” runs command in a shell, but wildcards or metacharacters not working for more details.






share|improve this answer






















  • I had tried the -s option, and that's the one I would prefer to use. I couldn't get it to work. Your command example also gives the error "No such file or directory" (with single quotes or double quotes). The command I gave in my answer does work. But I would prefer sudo -s, and if you figure out why it isn't working here, I'll accept your answer. Thanks.
    – MountainX
    Nov 22 at 8:02










  • @MountainX what Linux distribution are you using and which version of sudo? I believe the -s option is a recent addition, so might not be present in your version...
    – Filipe Brandenburger
    Nov 22 at 8:13










  • @MountainX Does this command work for you? sudo -u authduser -s whoami
    – Filipe Brandenburger
    Nov 22 at 8:21










  • Yes, sudo -u authduser -s whoami gives the expected result authduser.
    – MountainX
    Nov 25 at 1:42










  • @MoutainX sudo -s does not work here. See “sudo -s <command>” runs command in a shell, but wildcards or metacharacters not working for details on why it does not work.
    – Filipe Brandenburger
    yesterday

















up vote
0
down vote













The following approach works:



sudo -u authduser bash -c "ls /mounted/filesystem/path/*.user.js"


muru was correct that the issue is similar to Why isn't this sudo mv operation with wildcard working?, except the opposite in that root lacks permissions and the regular user has permissions. The issue comes down to order of precedence of operations, and the solution is based on expanding the wildcard after the sudo operation rather than before it.






share|improve this answer
















  • 2




    The issue is exactly the same: the target user for sudo has permissions, the user running sudo doesn't.
    – muru
    Nov 22 at 7:51

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










The issue with the wildcard is that it's expanded by the shell, before running the sudo command.



If only authduser will be able to list files under /mounted/filesystem/path/, but not your user who is running the sudo command, then the shell will not be able to expand the wildcard and will pass it unmodified. Since ls itself doesn't expand wildcards, this will result in the error you're seeing.



See Why isn't this sudo mv operation with wildcard working? for more details on that.




For a solution to this problem, you can use sudo's -s argument, which spawns a shell as the target user (rather than simply executing a command directly), in which case if you also give it a command, it will be passed to the shell using the -c option (which is the usual option for the shell to interpret and execute a command, including wildcard expansion.)



In that case, you'll want to put the command within quotes (preferably single quotes), so that the shell running the sudo command will not try to interpret the command, only the one spawned by sudo as the target user:



# sudo -u authduser -s 'ls /mounted/filesystem/path/*.user.js'


See the man page of sudo for more details, in particular take a look at the -s option.



UPDATE: It's not really possible to use sudo -s with a command here. See “sudo -s ” runs command in a shell, but wildcards or metacharacters not working for more details.






share|improve this answer






















  • I had tried the -s option, and that's the one I would prefer to use. I couldn't get it to work. Your command example also gives the error "No such file or directory" (with single quotes or double quotes). The command I gave in my answer does work. But I would prefer sudo -s, and if you figure out why it isn't working here, I'll accept your answer. Thanks.
    – MountainX
    Nov 22 at 8:02










  • @MountainX what Linux distribution are you using and which version of sudo? I believe the -s option is a recent addition, so might not be present in your version...
    – Filipe Brandenburger
    Nov 22 at 8:13










  • @MountainX Does this command work for you? sudo -u authduser -s whoami
    – Filipe Brandenburger
    Nov 22 at 8:21










  • Yes, sudo -u authduser -s whoami gives the expected result authduser.
    – MountainX
    Nov 25 at 1:42










  • @MoutainX sudo -s does not work here. See “sudo -s <command>” runs command in a shell, but wildcards or metacharacters not working for details on why it does not work.
    – Filipe Brandenburger
    yesterday














up vote
1
down vote



accepted










The issue with the wildcard is that it's expanded by the shell, before running the sudo command.



If only authduser will be able to list files under /mounted/filesystem/path/, but not your user who is running the sudo command, then the shell will not be able to expand the wildcard and will pass it unmodified. Since ls itself doesn't expand wildcards, this will result in the error you're seeing.



See Why isn't this sudo mv operation with wildcard working? for more details on that.




For a solution to this problem, you can use sudo's -s argument, which spawns a shell as the target user (rather than simply executing a command directly), in which case if you also give it a command, it will be passed to the shell using the -c option (which is the usual option for the shell to interpret and execute a command, including wildcard expansion.)



In that case, you'll want to put the command within quotes (preferably single quotes), so that the shell running the sudo command will not try to interpret the command, only the one spawned by sudo as the target user:



# sudo -u authduser -s 'ls /mounted/filesystem/path/*.user.js'


See the man page of sudo for more details, in particular take a look at the -s option.



UPDATE: It's not really possible to use sudo -s with a command here. See “sudo -s ” runs command in a shell, but wildcards or metacharacters not working for more details.






share|improve this answer






















  • I had tried the -s option, and that's the one I would prefer to use. I couldn't get it to work. Your command example also gives the error "No such file or directory" (with single quotes or double quotes). The command I gave in my answer does work. But I would prefer sudo -s, and if you figure out why it isn't working here, I'll accept your answer. Thanks.
    – MountainX
    Nov 22 at 8:02










  • @MountainX what Linux distribution are you using and which version of sudo? I believe the -s option is a recent addition, so might not be present in your version...
    – Filipe Brandenburger
    Nov 22 at 8:13










  • @MountainX Does this command work for you? sudo -u authduser -s whoami
    – Filipe Brandenburger
    Nov 22 at 8:21










  • Yes, sudo -u authduser -s whoami gives the expected result authduser.
    – MountainX
    Nov 25 at 1:42










  • @MoutainX sudo -s does not work here. See “sudo -s <command>” runs command in a shell, but wildcards or metacharacters not working for details on why it does not work.
    – Filipe Brandenburger
    yesterday












up vote
1
down vote



accepted







up vote
1
down vote



accepted






The issue with the wildcard is that it's expanded by the shell, before running the sudo command.



If only authduser will be able to list files under /mounted/filesystem/path/, but not your user who is running the sudo command, then the shell will not be able to expand the wildcard and will pass it unmodified. Since ls itself doesn't expand wildcards, this will result in the error you're seeing.



See Why isn't this sudo mv operation with wildcard working? for more details on that.




For a solution to this problem, you can use sudo's -s argument, which spawns a shell as the target user (rather than simply executing a command directly), in which case if you also give it a command, it will be passed to the shell using the -c option (which is the usual option for the shell to interpret and execute a command, including wildcard expansion.)



In that case, you'll want to put the command within quotes (preferably single quotes), so that the shell running the sudo command will not try to interpret the command, only the one spawned by sudo as the target user:



# sudo -u authduser -s 'ls /mounted/filesystem/path/*.user.js'


See the man page of sudo for more details, in particular take a look at the -s option.



UPDATE: It's not really possible to use sudo -s with a command here. See “sudo -s ” runs command in a shell, but wildcards or metacharacters not working for more details.






share|improve this answer














The issue with the wildcard is that it's expanded by the shell, before running the sudo command.



If only authduser will be able to list files under /mounted/filesystem/path/, but not your user who is running the sudo command, then the shell will not be able to expand the wildcard and will pass it unmodified. Since ls itself doesn't expand wildcards, this will result in the error you're seeing.



See Why isn't this sudo mv operation with wildcard working? for more details on that.




For a solution to this problem, you can use sudo's -s argument, which spawns a shell as the target user (rather than simply executing a command directly), in which case if you also give it a command, it will be passed to the shell using the -c option (which is the usual option for the shell to interpret and execute a command, including wildcard expansion.)



In that case, you'll want to put the command within quotes (preferably single quotes), so that the shell running the sudo command will not try to interpret the command, only the one spawned by sudo as the target user:



# sudo -u authduser -s 'ls /mounted/filesystem/path/*.user.js'


See the man page of sudo for more details, in particular take a look at the -s option.



UPDATE: It's not really possible to use sudo -s with a command here. See “sudo -s ” runs command in a shell, but wildcards or metacharacters not working for more details.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered Nov 22 at 7:55









Filipe Brandenburger

6,5801730




6,5801730











  • I had tried the -s option, and that's the one I would prefer to use. I couldn't get it to work. Your command example also gives the error "No such file or directory" (with single quotes or double quotes). The command I gave in my answer does work. But I would prefer sudo -s, and if you figure out why it isn't working here, I'll accept your answer. Thanks.
    – MountainX
    Nov 22 at 8:02










  • @MountainX what Linux distribution are you using and which version of sudo? I believe the -s option is a recent addition, so might not be present in your version...
    – Filipe Brandenburger
    Nov 22 at 8:13










  • @MountainX Does this command work for you? sudo -u authduser -s whoami
    – Filipe Brandenburger
    Nov 22 at 8:21










  • Yes, sudo -u authduser -s whoami gives the expected result authduser.
    – MountainX
    Nov 25 at 1:42










  • @MoutainX sudo -s does not work here. See “sudo -s <command>” runs command in a shell, but wildcards or metacharacters not working for details on why it does not work.
    – Filipe Brandenburger
    yesterday
















  • I had tried the -s option, and that's the one I would prefer to use. I couldn't get it to work. Your command example also gives the error "No such file or directory" (with single quotes or double quotes). The command I gave in my answer does work. But I would prefer sudo -s, and if you figure out why it isn't working here, I'll accept your answer. Thanks.
    – MountainX
    Nov 22 at 8:02










  • @MountainX what Linux distribution are you using and which version of sudo? I believe the -s option is a recent addition, so might not be present in your version...
    – Filipe Brandenburger
    Nov 22 at 8:13










  • @MountainX Does this command work for you? sudo -u authduser -s whoami
    – Filipe Brandenburger
    Nov 22 at 8:21










  • Yes, sudo -u authduser -s whoami gives the expected result authduser.
    – MountainX
    Nov 25 at 1:42










  • @MoutainX sudo -s does not work here. See “sudo -s <command>” runs command in a shell, but wildcards or metacharacters not working for details on why it does not work.
    – Filipe Brandenburger
    yesterday















I had tried the -s option, and that's the one I would prefer to use. I couldn't get it to work. Your command example also gives the error "No such file or directory" (with single quotes or double quotes). The command I gave in my answer does work. But I would prefer sudo -s, and if you figure out why it isn't working here, I'll accept your answer. Thanks.
– MountainX
Nov 22 at 8:02




I had tried the -s option, and that's the one I would prefer to use. I couldn't get it to work. Your command example also gives the error "No such file or directory" (with single quotes or double quotes). The command I gave in my answer does work. But I would prefer sudo -s, and if you figure out why it isn't working here, I'll accept your answer. Thanks.
– MountainX
Nov 22 at 8:02












@MountainX what Linux distribution are you using and which version of sudo? I believe the -s option is a recent addition, so might not be present in your version...
– Filipe Brandenburger
Nov 22 at 8:13




@MountainX what Linux distribution are you using and which version of sudo? I believe the -s option is a recent addition, so might not be present in your version...
– Filipe Brandenburger
Nov 22 at 8:13












@MountainX Does this command work for you? sudo -u authduser -s whoami
– Filipe Brandenburger
Nov 22 at 8:21




@MountainX Does this command work for you? sudo -u authduser -s whoami
– Filipe Brandenburger
Nov 22 at 8:21












Yes, sudo -u authduser -s whoami gives the expected result authduser.
– MountainX
Nov 25 at 1:42




Yes, sudo -u authduser -s whoami gives the expected result authduser.
– MountainX
Nov 25 at 1:42












@MoutainX sudo -s does not work here. See “sudo -s <command>” runs command in a shell, but wildcards or metacharacters not working for details on why it does not work.
– Filipe Brandenburger
yesterday




@MoutainX sudo -s does not work here. See “sudo -s <command>” runs command in a shell, but wildcards or metacharacters not working for details on why it does not work.
– Filipe Brandenburger
yesterday












up vote
0
down vote













The following approach works:



sudo -u authduser bash -c "ls /mounted/filesystem/path/*.user.js"


muru was correct that the issue is similar to Why isn't this sudo mv operation with wildcard working?, except the opposite in that root lacks permissions and the regular user has permissions. The issue comes down to order of precedence of operations, and the solution is based on expanding the wildcard after the sudo operation rather than before it.






share|improve this answer
















  • 2




    The issue is exactly the same: the target user for sudo has permissions, the user running sudo doesn't.
    – muru
    Nov 22 at 7:51














up vote
0
down vote













The following approach works:



sudo -u authduser bash -c "ls /mounted/filesystem/path/*.user.js"


muru was correct that the issue is similar to Why isn't this sudo mv operation with wildcard working?, except the opposite in that root lacks permissions and the regular user has permissions. The issue comes down to order of precedence of operations, and the solution is based on expanding the wildcard after the sudo operation rather than before it.






share|improve this answer
















  • 2




    The issue is exactly the same: the target user for sudo has permissions, the user running sudo doesn't.
    – muru
    Nov 22 at 7:51












up vote
0
down vote










up vote
0
down vote









The following approach works:



sudo -u authduser bash -c "ls /mounted/filesystem/path/*.user.js"


muru was correct that the issue is similar to Why isn't this sudo mv operation with wildcard working?, except the opposite in that root lacks permissions and the regular user has permissions. The issue comes down to order of precedence of operations, and the solution is based on expanding the wildcard after the sudo operation rather than before it.






share|improve this answer












The following approach works:



sudo -u authduser bash -c "ls /mounted/filesystem/path/*.user.js"


muru was correct that the issue is similar to Why isn't this sudo mv operation with wildcard working?, except the opposite in that root lacks permissions and the regular user has permissions. The issue comes down to order of precedence of operations, and the solution is based on expanding the wildcard after the sudo operation rather than before it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 7:45









MountainX

4,8972471127




4,8972471127







  • 2




    The issue is exactly the same: the target user for sudo has permissions, the user running sudo doesn't.
    – muru
    Nov 22 at 7:51












  • 2




    The issue is exactly the same: the target user for sudo has permissions, the user running sudo doesn't.
    – muru
    Nov 22 at 7:51







2




2




The issue is exactly the same: the target user for sudo has permissions, the user running sudo doesn't.
– muru
Nov 22 at 7:51




The issue is exactly the same: the target user for sudo has permissions, the user running sudo doesn't.
– muru
Nov 22 at 7:51


Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay