LinuxQuestions.org
Review your favorite Linux distribution.
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 06-23-2009, 10:13 AM   #1
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Rep: Reputation: 15
Behavior of Command Line Expansion in touch command


Hi all,
Consider the following code:
Code:
#ls
#touch file[1-5].doc            # ----> pos1
#ls
file[1-5].doc
#rm -f file\[1-5\].doc
#ls
#touch file{1,2,3,4,5}.doc
#ls -l
total 20
-rw-r--r--  1 root root 0 Jun 23 19:10 file1.doc
-rw-r--r--  1 root root 0 Jun 23 19:10 file2.doc
-rw-r--r--  1 root root 0 Jun 23 19:10 file3.doc
-rw-r--r--  1 root root 0 Jun 23 19:10 file4.doc
-rw-r--r--  1 root root 0 Jun 23 19:10 file5.doc
#touch file[1-5].doc            # ----> pos2
#ls -l
total 20
-rw-r--r--  1 root root 0 Jun 23 19:10 file1.doc
-rw-r--r--  1 root root 0 Jun 23 19:10 file2.doc
-rw-r--r--  1 root root 0 Jun 23 19:10 file3.doc
-rw-r--r--  1 root root 0 Jun 23 19:10 file4.doc
-rw-r--r--  1 root root 0 Jun 23 19:10 file5.doc
As you see, the command "touch file[1-5].doc" created the file "file[1-5].doc" in line position 1. Whereas, it did nothing when issued in position 2.

Thanks in advance.
 
Old 06-23-2009, 10:44 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 06-23-2009, 11:46 PM   #3
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Original Poster
Rep: Reputation: 15
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!
 
Old 06-24-2009, 12:03 AM   #4
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,629

Rep: Reputation: Disabled
It created 7 files in the first case naming them file0.txt through file6.txt.
Look at the command that David issues at first attempt.
Code:
touch file{0..6}.txt
The second was the example for you to show that once the files are already there it will only change the time stamp for it.
 
Old 06-24-2009, 12:14 AM   #5
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Original Poster
Rep: Reputation: 15
Hi Linuxlover,
When I said "first case", I meant the command in my code example (not David's). The command was touch file[1-5].doc
 
Old 06-24-2009, 04:56 AM   #6
rikxik
Member
 
Registered: Dec 2007
Posts: 88

Rep: Reputation: 19
Quote:
Originally Posted by ahmedb72 View Post
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.

Last edited by rikxik; 06-24-2009 at 04:58 AM.
 
Old 06-24-2009, 10:00 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
What would you expect "touch file*.doc" to do?

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.
 
Old 06-25-2009, 08:33 AM   #8
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 299

Rep: Reputation: 62
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.

Here is the POSIX.1 specfication for touch: http://www.opengroup.org/onlinepubs/...ies/touch.html
Here is an explanation of shell globbing for Bash: http://tldp.org/LDP/abs/html/globbingref.html
 
Old 06-26-2009, 03:38 AM   #9
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
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.
 
Old 06-26-2009, 04:32 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 07-01-2009, 02:51 AM   #11
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Original Poster
Rep: Reputation: 15
Thanks David for that great reference link.

Do you recommend me any other links or books to learn Shell Scripting fundamentals?
 
  


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
Command Line Expansion & echo Merrida Fedora 4 05-26-2009 12:30 AM
Does bash shell parameter expansion run the risk of overflowing command line buffer? david1941 Linux - Software 1 04-28-2009 10:13 PM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 06:30 PM
What is command line expansion? Wizzy Linux - Newbie 2 03-22-2006 01:50 PM
bash shell command line expansion hansi umayangan Linux - General 2 03-13-2005 11:31 AM

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

All times are GMT -5. The time now is 11:58 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