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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
07-07-2017, 08:01 AM
|
#1
|
Member
Registered: Apr 2017
Posts: 33
Rep: 
|
Using multiple files in a loop in bash shell
Greetings!
I have files with multiple extension in a folder like .txt, .update, .text. The file names are like
out1.txt,
out1.text,
out1.update,
out2.txt,
out2.text,
out2.update
and so on....My command looks like this:
Code:
./script.pl out1.txt out1.text out1.update
I would like to put this command in the loop for every file. I have tried to use a loop like:
Code:
for i in *.{txt,text,update}
But it takes the full name including the extension so I couldn't figure out how to deduce the specific extension files in the command.
Thanks in advance!
|
|
|
07-07-2017, 11:24 AM
|
#2
|
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.6
Posts: 5,906
|
Quote:
Originally Posted by Asoo
I would like to put this command in the loop for every file. I have tried to use a loop like:
Code:
for i in *.{txt,text,update}
But it takes the full name including the extension so I couldn't figure out how to deduce the specific extension files in the command.
Thanks in advance!
|
Looks like
Code:
for i in {txt,text,update}
would work to have only the extensions in the loop.
Is that what you wanted?
|
|
|
07-07-2017, 01:19 PM
|
#3
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
Quote:
Originally Posted by Asoo
Code:
for i in *.{txt,text,update}
But it takes the full name including the extension so I couldn't figure out how to deduce the specific extension files in the command.
|
That globbing *.{txt,text,update} reads the file names into the variable i one at a time.
If you do separate out everything after the final dot in the file name using only in bash, ksh, or zsh:
Code:
for i in *.txt; do
f=${i%.*};
e=${i##*.};
echo $f $e;
done;
|
|
1 members found this post helpful.
|
07-10-2017, 03:04 AM
|
#4
|
Member
Registered: Apr 2017
Posts: 33
Original Poster
Rep: 
|
Thank you for the help.
I did something like this and it worked:
Quote:
b=${f%.txt}
./script.pl $b.txt $b.text $b.update
|
Thanks!
|
|
|
All times are GMT -5. The time now is 02:46 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|