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.
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?
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."
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.