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.
I guess the meaning of the title is far from obvious, but I wasn't able to come up with a better one.
So this is only for practice purposes.
Given several files under a directory, I'm trying to add some string, text, whatever to each one, so that each string is unique. For instance, file1 should contain the text "filea", file2, the text "fileb" and so on.
What I tried obviously didn't work:
Code:
for var_1 in $(ls .); do echo file{a..f} > $var_1; done
Not really sure why you thought this would yield different data when all the work is being done inside the loop.
Ultimately the issue I see here is that you would need to know ahead of time how many files you have in the directory? reason being is that your current example, if it worked, would only do so for 6 or less
files as the moment you encounter a seventh file you would be back to 'filea' and hence no unique string.
So you may need to provide more information on what you know prior to running the script before we can help you further??
I will add that the use of ls in a for loop is an enormous no no ... use the simple glob metacharacter as it will not have any of the word splitting issues of your current example:
There's a random number of files in a directory. I don't need a particular number. Let's say 10, if it's essential to solving the problem.
I'm simply experimenting, I'm trying to add succesive strings of the {a..z} (braces) type to the content of these 10 files. Ok, I understand that if it's 10 files, then I'd need something like {a..j}. So how do I add "a" to "file1", "b" to "file2" and so on, and so forth?
I don't need the answer, I need to know how to approach the problem. The solution is obviously very different from what I've tried, but I've no clue whatsoever.
I would place your alphabet in an array which can then be echoed to each file and an increment made to the counter. Again this will only work for 26 files, so an additional part of your code might
need to be a count of just how many files there are so you know ahead of time if the answer will be unique or not.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.