Latest LQ Deal: Linux Power User Bundle
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 10-29-2010, 11:27 AM   #1
Registered: Jun 2009
Posts: 111

Rep: Reputation: 17
Angry BashScript For Loop for files...HELP

Hello all,
I was wondering if there is a way to do the following:

Having a list of files:

I wan to create a for loop where I use each file prefix (suppose 122, 132,158,...) and run a command for them:

for <all files in directory>
<execute cmd X to 122_1 and 122_2>

It look very symple in C or a program where I can locate arrays or something..but I am having some problems with bash scripting.

Thanks for your help!
Old 10-29-2010, 11:59 AM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,075
Blog Entries: 14

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Assuming you have only one dot in each file name and each file name has that dot:

for file in $(ls)
do fbase=$(echo $file |awk -F. '{print $1}')
   <whatever $fbase>
Where you substitute whatever you want to do with the file's prefix. for the <whatever ...> line.
for file in $(ls)
do fbase=$(echo $file |awk -F. '{print $1}')
   echo Filename is $file and prefix of file is $fbase
This would output a line for each filename showing you what its original name was and what the part of the name was before the dot.
Old 10-29-2010, 12:17 PM   #3
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
for f in <list of files>
    <do whatever you want with $no_extension_f>
BTW it is not robust to generate a list of files using the ls command for reasons explained here.
Old 10-29-2010, 12:33 PM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Knowledge is a great thing. So depending on what you know about the file prior to writing / running the script is going to make all the difference in the world.
If the list of files is short then catkin's solution is fine.

Assuming the list is extensive (or at least long enough that you do not wish to type it in or that the filenames change) then you need other options.

1. All files exist in one directory and only those files:
for f in *
    <your stuff>
2. All in one folder but not sure what else there might be, ie directories and stuff:
for f in *
    if [[ -f "$f" ]] # file exists and is a regular file, may need to change test if say executable files or something
        <your stuff>
3. All in one directory but only want a particular extension and there are others:
for f in *.xyz
    if [[ -f "$f" ]]
        <your stuff>
If it gets much more complicated than the above then my last option would be:
while read -r f
    <your stuff>
done< <(find <path to files> -type f -name '*xyz'
-type f : find only files that fit the criteria
-name '*xyz' : probably the toughest part is to choose what the files have in common

man find should be your first port if this simple option is not working.

Note: The nice part about all these solutions is they will not get caught out if your files have white space in them.


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
[SOLVED] Grep error in Bashscript tuxianD Linux - Newbie 8 07-14-2010 08:50 AM
How to run a bashscript no matter what directory u stand in? kickarzt Linux - Newbie 4 03-16-2010 07:53 AM
Enter key in bashscript happy78 Linux - General 4 03-24-2006 02:15 AM
how do I use a bashscript code? (for torsmo) fatblueduck Linux - Software 9 07-29-2005 04:51 PM
bashscript: checking for numbers rev1 Programming 1 03-21-2004 02:46 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration