LinuxQuestions.org
Visit Jeremy's Blog.
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 03-14-2006, 02:18 PM   #1
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 528

Rep: Reputation: 78
multiple file creation


Hi all

I'm trying to create many files with only one command line

here is what i want to do:
creating the files: test1.txt, test2.txt, test3.txt

so i try to do this:
touch test[1-3].txt

but it doesn't work... :-(

any idea?

thanks.

Last edited by angel115; 03-14-2006 at 02:19 PM.
 
Old 03-14-2006, 02:20 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Code:
touch foo{1,2,3}
 
Old 03-14-2006, 02:23 PM   #3
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 528

Original Poster
Rep: Reputation: 78
Thanks

and do you know how to do if you have to create many more like "100 files"

do you know how to create a range? like: test1.txt, test2.txt ....... to ..... test100.txt?
 
Old 03-14-2006, 02:32 PM   #4
RHCE2006
LQ Newbie
 
Registered: Mar 2006
Posts: 5

Rep: Reputation: 0
hi

hi

I am new to this forum please sorry me if i reply anything wrong

to create the you can try this command touch a1 a2 a3 ....

Thank you
RHCE2006
 
Old 03-14-2006, 02:32 PM   #5
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Play around with seq. You may want to look at printf (or the -g option to seq) to format the numbers somewhat more nicely (say, as 001, 002, ..., 100).

Just curious: why do you want to touch those files? It smells a little like homework... could I have a look at your script?
 
Old 03-14-2006, 02:40 PM   #6
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
RHCE: welcome abord LQ Always nice to see fresh flesh and warm blood or whatever the young people call it these days. You might want to introduce yourself in the "member introduction" forum, and (if you're a newbie) go read "Smart Questions" by ESR (google will help you find it).

I believe my first answer (touch a{1,2,3}) expands to your answer (touch a1 a2 a3), thus making them equivalent (under the assumption that the shell expands curly braces--in fact your solution is probably more portable to other shells).

And don't be afraid so come with good ideas--the worst that can happen is "no, that didn't work."
 
Old 03-14-2006, 02:59 PM   #7
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 528

Original Poster
Rep: Reputation: 78
Yes this is kind of home work i'm just trying to impove myself.

and i was just wanderying why does the command for exemple:
cp test[1-100].txt ~/copy
is working but not touch
touch test[1-100].txt
because in both cases, it's a range of numbers!!!

becaus i know i can do the same with a small script
for i in $[1-100] do;
touch test${ i }.txt
done

or some thing like that, and it will do the same job.
 
Old 03-14-2006, 03:13 PM   #8
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
The cp you write doesn't work on my system. I'm using bash version 3.00.16(1)-release (on debian testing, main only). Which shell are you using?

anyways, what you want to do is
Code:
for i in $(seq 1 100)
do touch test$i
done
Also, please post code inside code tags, like this:
Code:
code tags [ code ] like this [ / code ] do not have spurious spaces.
 
Old 03-14-2006, 03:34 PM   #9
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 528

Original Poster
Rep: Reputation: 78
Bash version: 2.05b.0(1)-release

if you create the directory copy in your home directory the command:
Code:
cp test[1-3].txt ~/copy
should work. no?

But anyway, i don't want to boring you.
it's not the end of the world if what i want to do with touch doesn't work.

Thanks for you paciance.
 
Old 03-14-2006, 03:50 PM   #10
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
2.05b.0(1)? I guess that makes you just as archaic as my mathematics department. I suggest you upgrade to something a little more recent.

Anyways, unless you can't get around that old piece of whatever it's a piece of, I don't think there's a real reason to not upgrade.
 
Old 08-14-2006, 06:31 AM   #11
stabu
Member
 
Registered: Mar 2004
Location: dublin IRL
Distribution: Slackv12.1, Slamd64v12.1,Xubuntu v8.10_64, FC8_64
Posts: 438
Blog Entries: 5

Rep: Reputation: 32
there goes another unanswered question. I want to do the same thing and in fact it's not homework.

I thought I got this working yesterday, merely by using
Code:
touch fakefile{a-k}.txt
and it created the fakefilea.txt, fakefileb.txt for me, but very disappointingly it didn't work the second time around.

That smells alright, it smells of a change in bash's global variables or something or other.

The problem here is finding out wha this "string argument range expansion" effect in bash is officially called. WHen I finally get to work properly, I'll report here for all those who tought that a thread with 10 replies might have the answer.
 
Old 08-14-2006, 08:07 AM   #12
stabu
Member
 
Registered: Mar 2004
Location: dublin IRL
Distribution: Slackv12.1, Slamd64v12.1,Xubuntu v8.10_64, FC8_64
Posts: 438
Blog Entries: 5

Rep: Reputation: 32
got it

OK, I worked it out finally. I might say, it took me some freaking hours, I'll explan so if you're impattient just skip to the bottom.

Ma and Angel115 wants to know how to create a bunch of files without using a for statement.

We need bash to expand on a range, for the touch command. This will let us create a huge number of files in one fell swoop, depending on the size of the range we give it. the suggestions above which used the comma are too tedious if you want to create a 100 or a 1000 files, so range expansion is the key benefit of this exercise.

However, the square brackets is only one type of expansion bash allows us. It can be used with the "ls" command when checking existing files, but it turns out that touch (nor echo) doesn't work that way. They expand not on square brackets but on braces.

In fact, I had got that far, but there's an added twist, the hyphen does not represent a range in brace expansion, the double dot does. That's where I fell down, I spent hours tryign to work out why the hyphen wasn't working.

So the brief answer to angel115 is:
Code:
touch test{1..3}.txt
That will creat your three files as ong as you're using bash v3 upwards, which you really should be doing.

It's a pretty cool thing, as you can wreak havoc on directory listings by creating say 100 000 files in one fell swoop, i.e.
Code:
touch fakefile{1..100000}.txt
Hilarious!
 
  


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
File Creation Time? enine Linux - General 9 05-17-2006 12:46 PM
multiple files creation latino Programming 1 07-09-2005 04:25 AM
RPM Spec file creation: %file section question davidas Linux - Newbie 0 03-16-2004 11:36 PM
file creation in C ratheesh Programming 1 01-08-2004 06:39 AM
rpm file creation mayankjohri Linux - Software 4 03-17-2003 08:07 AM


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