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? |
Quote:
Code:
[1-5] Code:
touch /home{1..5}/file |
Quote:
|
Quote:
Code:
touch /home[1-5]/1 Code:
touch /home{1..5}/1 |
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. |
it works,thanks a lot,friendly men.
|
Quote:
|
Quote:
|
Quote:
Code:
touch /etc/home[1-5]/1 |
Quote:
|
All times are GMT -5. The time now is 09:45 PM. |