LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 10:42 AM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

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, 11:37 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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

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, 11:55 AM   #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, 11:59 AM   #5
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

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, 01:13 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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, 01: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 01:36 PM. Reason: clarified
 
1 members found this post helpful.
Old 04-19-2012, 01:40 PM   #8
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

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



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 07:11 PM
sudo doesn't work slack_prad Linux - General 2 02-21-2007 07:38 AM
sudo doesn't work oomfoofoo Debian 3 08-13-2005 07:34 AM
Second CD doesn't work - short file names auditude Slackware - Installation 4 02-09-2004 11:53 PM
sudo doesn't work mla Linux - Software 4 10-01-2003 07:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:17 PM.

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
Open Source Consulting | Domain Registration