Behavior of Command Line Expansion in touch command
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Touch isn't doing nothing in the second case, it's touching each of the files and updating their access times. You just didn't give enough time between the two commands for the system time to change.
Code:
david:[~/test/touchtest]$ touch file{0..6}.txt
david:[~/test/touchtest]$ ls
total 0
-rw-rw-r-- 1 david david 0 2009-06-24 00:42 file0.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:42 file1.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:42 file2.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:42 file3.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:42 file4.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:42 file5.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:42 file6.txt
david:[~/test/touchtest]$ touch file[1-5].txt
david:[~/test/touchtest]$ ls
total 0
-rw-rw-r-- 1 david david 0 2009-06-24 00:42 file0.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:43 file1.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:43 file2.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:43 file3.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:43 file4.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:43 file5.txt
-rw-rw-r-- 1 david david 0 2009-06-24 00:42 file6.txt
david:[~/test/touchtest]$
As I understand it, [] specifies a matching range. In the first case there's nothing in the directory to match, so the touch command creates a new file. In the second case there are files that match the numbered range, so the program updates them instead. Notice how it only updated the files 1-5 from the group.
Hi David,
Thanks, your reply makes sense. However, regarding your comment:
Quote:
In the first case there's nothing in the directory to match, so the touch command creates a new file.
Why in first case the touch command didn't create five files?
The command updated access time of five files when they're there and it created only one file when they aren't. I suppose it should have created five files!
Hi David,
Thanks, your reply makes sense. However, regarding your comment:
Why in first case the touch command didn't create five files?
The command updated access time of five files when they're there and it created only one file when they aren't. I suppose it should have created five files!
The [1-5] magic is called filename generation ([-], *, ? etc. are used for this). If no file is found which matches the pattern, the string passed is left as it is. If found, only then its expanded. When you issued your first touch command, there were no files matching file[1-5].doc so it was taken literally.
Well, if there's are any files in the directory like "file.doc", "file1.doc", or "filewithalongname.doc", etc., it will update them. But if there are no matching filenames in the directory, then touch still has to do something with the string, and so it creates a new file called "file*.doc".
What it won't do is name the file with a list of ALL of the characters that * could match (fileabcdefg1234#$+!#.etc.doc), or even worse, a separate file for each possible match (filea.doc, fileb.doc, file7.doc, file%.doc....)!
"[1-5]" works in exactly the same way as * does; only the range of characters that it will match is more limited. It's not a character generating string, only a character matching string.
It is very simple. The shell does globbing on the pathname and passes the results of that globbing to touch as command line arguments. If the results of globbing are zero than the original pathname is passed to touch.
It's not a character generating string, only a character matching string.
Thanks David for your comments.
Now, what are the character generating strings and character matching strings?
For example, I noticed touch{1..5}.doc generated the files.
AFAIK, for character generation that's it. It's called brace expansion, and it's explained here, along with all the other special character expressions you may run across.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.