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, 11: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, 11:44 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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-24-2009, 12:46 AM   #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, 01:03 AM   #4
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
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, 01: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, 05: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 05:58 AM.
 
Old 06-24-2009, 11:00 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 09:33 AM   #8
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 297

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, 04: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, 05:32 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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


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