Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-23-2005, 07:48 AM   #1
Registered: Oct 2005
Distribution: Ubuntu Edgy
Posts: 58

Rep: Reputation: 15
Bash programming

Hi everyone,

I have a little problem: I have around two hundred files that must be treated individually (like "command file1", "command file2").

Of course, the program that must treat them doesn't accept wildcards. Is there any simple way of doing a "ls file*" and then redirecting its output line by line to the command I must run. Something that would look like this (in an imagined, C-like language):

filelist= ls; // suppossing that the outpout is an array of strings.
for(i=0;i<lenght(filelist);i++) {
command filelist[i];

Old 11-23-2005, 08:17 AM   #2
Senior Member
Registered: Oct 2003
Posts: 2,977

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
The xargs command is used to do that in bash programing. It takes the output of one command and pipes it to a new command one at a time. Check out this link for a nice practical discussion:
Old 11-23-2005, 08:47 AM   #3
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Absolutely not! The xargs command takes multiple "words" from standard input and gives them all as arguments so a single command; example:

find . -name *.java | xargs tar -cf backup.tar

What you need is a loop. Assuming you may have spaces in some filenames (in which case cutting into shell words doesn't work), you'd have to run something like this:

ls -1 | while read filename; do command "$filename"; done

Old 11-23-2005, 08:58 AM   #4
Registered: Oct 2005
Distribution: Ubuntu Edgy
Posts: 58

Original Poster
Rep: Reputation: 15
Thanks a lot! Merci beaucoup!
Old 11-24-2005, 04:18 AM   #5
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
De rien You're welcome!
Old 11-26-2007, 08:01 AM   #6
LQ Newbie
Registered: Nov 2007
Posts: 1

Rep: Reputation: 0
bash programming

Hiya guys,
I hope somebody can help me,I am in a trouble with this program.
I tried to do loop-in-loop,but it didn't work and I don't know how can I do it.I have read documentation about it but I couldn't find solution for my problem.
We try to cut a part from a picture,and the first loop worked,but if we have more pictures and want to do the same with the others how can I do it?
how can I make variable the img?

Thanks for any help in advance!

echo setting up variables
rm temp.* temp2.* temp3.* temp4*


proc2d $filenm.hed temp.hed first=0 last=0
while [ $rot -lt 360 ]; do
echo this will produce the circle - the hard way
proc2d temp.hed temp0.hed trans=0,$y1
proc2d temp0.hed temp2.hed trans=0,$y2
rm temp.* temp0.*
proc2d temp2.hed temp.hed rot=$rot
rm temp2.*
let rot=rot+2

proc2d temp.hed temp3.hed
rm temp.* temp2.*
if [ $y1 -gt 0 ]; then y1=y1-1
if [ $y1 -eq 0 ]; then break
let rot=0
Old 11-26-2007, 08:14 AM   #7
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
xargs can be told to work one file at a time like this:
find /path/to/files -type f | xargs -n 1 yourcommand
If all the files are in a single directory, why not use a for loop:
for f in *.whatever; do yourcommand "$f"; done


find, loop, shell, xargs

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
Bash Programming moodupani Programming 1 08-31-2005 07:50 PM
bash programming alaios Programming 2 07-23-2005 01:52 AM
bash programming drisay Programming 6 12-20-2004 10:09 AM
bash programming cpukiller Linux - Newbie 1 11-04-2003 07:45 PM
bash programming bulliver Programming 2 04-05-2003 10:30 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration