LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-19-2012, 11:42 AM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,367

Rep: Reputation: 55
How can I use sudo with a wildcard in file names? (* doesn't work)


How can I use sudo with a wildcard in file names?

Code:
# sudo /usr/bin/head -4 /root/*.txt
/usr/bin/head: cannot open `/root/*.txt' for reading: No such file or directory
It looks like its literally trying to open "*.txt" rather than [match any characters].txt

Code:
# sudo /usr/bin/head -1 /root/test-2012-4-19.txt
hello world
TIA!
 
Old 04-19-2012, 12:37 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
What it is saying is that you have not .txt files in the root directory.
 
Old 04-19-2012, 12:39 PM   #3
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,367

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by grail View Post
What it is saying is that you have not .txt files in the root directory.
You are mistaken, as you can see from the second command there is a txt file in the root directory.
 
Old 04-19-2012, 12:55 PM   #4
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Which distro you are talking from?

*buntu implements "sudo" totally different than any other Linux.
 
Old 04-19-2012, 12:59 PM   #5
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,367

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by uhelp View Post
Which distro you are talking from?

*buntu implements "sudo" totally different than any other Linux.
I'm using CentOS
 
Old 04-19-2012, 02:13 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Ok my bad ... didn't look at second example ... although I discovered your issue is that the asterisk is not expanded due to the security. What this means is that everything you pass
to sudo will be read literally for the root directory at least due to the fact that a standard user does not have access. A simple confirmation you can try is the following:
Code:
$ sudo ls -la /root
<here you will see whatever files and folders exist>
$ sudo ls -la /root/*
ls: cannot access /root/*: No such file or directory
 
Old 04-19-2012, 02:34 PM   #7
dayid
Member
 
Registered: Apr 2012
Location: Austin, TX
Posts: 44

Rep: Reputation: Disabled
Quote:
Originally Posted by abefroman View Post
How can I use sudo with a wildcard in file names?

Code:
# sudo /usr/bin/head -4 /root/*.txt
/usr/bin/head: cannot open `/root/*.txt' for reading: No such file or directory
It looks like its literally trying to open "*.txt" rather than [match any characters].txt
Code:
sudo sh -c "/usr/bin/head -4 /root/*.txt"
This is also useful when trying to redirect output with sudo, e.g,:
Code:
sudo sh -c "grep foo /tmp/bar.txt > /tmp/bar2.txt"

Last edited by dayid; 04-19-2012 at 02:36 PM. Reason: clarified
 
1 members found this post helpful.
Old 04-19-2012, 02:40 PM   #8
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,367

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by dayid View Post
Code:
sudo sh -c "/usr/bin/head -4 /root/*.txt"
This is also useful when trying to redirect output with sudo, e.g,:
Code:
sudo sh -c "grep foo /tmp/bar.txt > /tmp/bar2.txt"
That worked, thank you sir!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sudo Wildcard Cottsay Linux - Software 2 06-11-2007 08:11 PM
sudo doesn't work slack_prad Linux - General 2 02-21-2007 08:38 AM
sudo doesn't work oomfoofoo Debian 3 08-13-2005 08:34 AM
Second CD doesn't work - short file names auditude Slackware - Installation 4 02-10-2004 12:53 AM
sudo doesn't work mla Linux - Software 4 10-01-2003 08:34 PM


All times are GMT -5. The time now is 06:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration