LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-26-2012, 12:13 AM   #1
windbadboy
LQ Newbie
 
Registered: Jul 2009
Posts: 25

Rep: Reputation: 0
Question a question about touch command.


我想用touch命令在系统里的每个分区建一个文件,检查有没有分区所属的磁盘有无只读情况发生。
I want to create a file on every partition by touch command.
if my linux has 5 partitions like /home1-/home5,each partition represents one physical disk.
when I typed "touch /home[1-5]/1" on my linux,the result:
[root@cu tmp]# touch /home[1-5]/1
touch: cannot touch `/home[1-5]/1': No such file or directory



when I typed ls /home[1-5],It works well.how to get the result I want with one command?

Last edited by windbadboy; 12-26-2012 at 12:14 AM.
 
Old 12-26-2012, 02:40 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Quote:
Originally Posted by windbadboy View Post
when I typed ls /home[1-5],It works well.how to get the result I want with one command?
The expression
Code:
[1-5]
is an interval which is correctly interpreted by the ls command. Nothing to do with shell expansion, instead. If working in bash, you can try extended brace expansion as in
Code:
touch /home{1..5}/file
 
Old 12-26-2012, 09:37 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,453

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by colucix View Post
The expression
Code:
[1-5]
is an interval which is correctly interpreted by the ls command. Nothing to do with shell expansion, instead.
I find it hard to believe that you don't know better than that. It has everything to do with shell expansion and what the shell does when nothing matches the glob pattern. The ls command does not perform glob expansion.
 
Old 12-26-2012, 11:56 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Quote:
Originally Posted by rknichols View Post
I find it hard to believe that you don't know better than that. It has everything to do with shell expansion. and what the shell does when nothing matches the glob pattern. The ls command does not perform glob expansion.
To me an interval like [1-5] has nothing to do (again) with shell expansion, if you agree that shell expansions are those listed here. Instead, it is a range of alphanumeric characters, which falls into the category of patterns. This is the reason why the command mentioned in the OP
Code:
touch /home[1-5]/1
fails, since the shell does not perform any expansion here (even if the directories exist). On the contrary a real expansion does exactly what it is supposed to do:
Code:
touch /home{1..5}/1
The difference is very subtle and we could discuss for hours (what I'd rather avoid). Anyway, something to deepen into, for the sake of clarity.
 
Old 12-26-2012, 03:17 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010Reputation: 2010
I'd say you're both partially right. Or perhaps it's better to say you're both right in respects, but looking at it from different directions.

Something like [1-5] is read by the shell as a globbing pattern to match. When the shell parses the command line (of a simple command), it first looks in the current working directory (or another if the glob is prefixed by an existing path), and if anything matches that pattern, the pattern gets expanded into a list of those matching filenames as arguments. This is then passed to the command.

If nothing matches, then the results depend on the shell settings, but the default is usually to pass the unexpanded string directly as an argument.

In other words, the globbing pattern itself is not file expansion syntax, as it can also be used elsewhere for other purposes (e.g. case statements) but the shell does do file expansion using globs in one of its parsing steps (the last step, by the way).


rknichols is also correct in that the receiving command usually doesn't do any pattern matching of its own (unless its specifically designed to do so, and it manages to get an unexpanded pattern as an argument). The ls command, for one, only attempts to list out the list of pathnames it gets from the shell (or the $PWD if nothing is given), and would simply issue an error if it happened to get an unexpanded, unmatching, string.

For touch, however, if you use a pattern that matches existing files, then it receives that expanded list from the shell, and will update their mtimes (or whatever it's set to do). If it receives a non-matching pattern, it will simply create a new file with that name, just as it would with any other text string.

That's why, in commands like touch you don't use globbing, but brace expansion. The shell always takes brace expansion patterns and expands them to the full list of possible combinations. It doesn't try to match them to anything, so the command you use always gets all the combinations as individual arguments.

Last edited by David the H.; 12-26-2012 at 03:40 PM. Reason: a bit of rewording
 
Old 12-26-2012, 11:34 PM   #6
windbadboy
LQ Newbie
 
Registered: Jul 2009
Posts: 25

Original Poster
Rep: Reputation: 0
it works,thanks a lot,friendly men.
 
Old 12-27-2012, 12:05 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,453

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by colucix View Post
To me an interval like [1-5] has nothing to do (again) with shell expansion, if you agree that shell expansions are those listed here. Instead, it is a range of alphanumeric characters, which falls into the category of patterns.
That is one of the types of shell expansion. In the very reference you cited, it is called "File name expansion" (section 3.4.9, at the bottom of the page).
 
Old 12-27-2012, 02:40 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Quote:
Originally Posted by rknichols View Post
That is one of the types of shell expansion. In the very reference you cited, it is called "File name expansion" (section 3.4.9, at the bottom of the page).
True. I stand corrected!
 
Old 12-27-2012, 02:48 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Quote:
Originally Posted by David the H. View Post
When the shell parses the command line (of a simple command), it first looks in the current working directory (or another if the glob is prefixed by an existing path), and if anything matches that pattern, the pattern gets expanded into a list of those matching filenames as arguments.
Thank you David for the deep insight. What puzzles me now is why this doesn't work even if the directories exist
Code:
touch /etc/home[1-5]/1
Maybe because the files still don't exist? TIA.
 
Old 12-27-2012, 10:29 AM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,453

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by colucix View Post
What puzzles me now is why this doesn't work even if the directories exist
Code:
touch /etc/home[1-5]/1
Maybe because the files still don't exist? TIA.
The match is for the entire path, not just some of the components. That glob will expand to the full paths of any files named "1" in those 5 directories. Since there are (for the OP's case) no such files, the glob fails and the argument is passed literally to the touch command.
 
  


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
touch command for directories? HalfMadDad Linux - General 3 09-27-2011 09:53 PM
touch command question tungaw2001 Linux - Newbie 1 01-25-2010 10:28 PM
Behavior of Command Line Expansion in touch command ahmedb72 Linux - Newbie 10 07-01-2009 02:51 AM
tips with touch command rajveervashisth Linux - Newbie 2 08-06-2008 01:22 PM
touch command dominant Linux - Newbie 6 02-17-2004 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:18 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
Open Source Consulting | Domain Registration