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 07-07-2017, 08:01 AM   #1
Asoo
LQ Newbie
 
Registered: Apr 2017
Posts: 29

Rep: Reputation: Disabled
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!
 
Old 07-07-2017, 11:24 AM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,432

Rep: Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055
Quote:
Originally Posted by Asoo View Post
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?
 
Old 07-07-2017, 01:19 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,655
Blog Entries: 3

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Quote:
Originally Posted by Asoo View Post
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.
Old 07-10-2017, 03:04 AM   #4
Asoo
LQ Newbie
 
Registered: Apr 2017
Posts: 29

Original Poster
Rep: Reputation: Disabled
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!
 
  


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
Using rm in Bash shell script on files/folders with spaces in for loop array kayasaman Programming 18 05-16-2013 06:34 AM
Bash Question(for loop): How to Zip multiple files under multiple directories Znrall Linux - General 2 08-01-2012 01:52 PM
Multiple variable for loop (bash) Phier Programming 9 11-24-2009 08:54 AM
Multiple variable for loop (bash) Phier Linux - Newbie 2 11-23-2009 11:13 AM
shell script - while loop with multiple conditions ronsha Programming 13 12-10-2005 04:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:56 PM.

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
Open Source Consulting | Domain Registration