LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-29-2010, 11:27 AM   #1
Drigo
Member
 
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:
122_1.xyz
122_2.xyz
132_1.xyz
132_2.xyz
158_1.xyz
158_2.xyz
.
.
.

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>
end


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
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,680
Blog Entries: 14

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

Code:
for file in $(ls)
do fbase=$(echo $file |awk -F. '{print $1}')
   <whatever $fbase>
done
Where you substitute whatever you want to do with the file's prefix. for the <whatever ...> line.
e.g.
Code:
for file in $(ls)
do fbase=$(echo $file |awk -F. '{print $1}')
   echo Filename is $file and prefix of file is $fbase
done
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
catkin
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
Code:
for f in <list of files>
do
    no_extension_f=${f##*.}
    <do whatever you want with $no_extension_f>
done
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
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
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:
Code:
for f in *
do
    <your stuff>
done
2. All in one folder but not sure what else there might be, ie directories and stuff:
Code:
for f in *
do
    if [[ -f "$f" ]] # file exists and is a regular file, may need to change test if say executable files or something
    then
        <your stuff>
    fi
done
3. All in one directory but only want a particular extension and there are others:
Code:
for f in *.xyz
do
    if [[ -f "$f" ]]
    then
        <your stuff>
    fi
done
If it gets much more complicated than the above then my last option would be:
Code:
while read -r f
do
    <your stuff>
done< <(find <path to files> -type f -name '*xyz'
[/code]
-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.
 
  


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
[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


All times are GMT -5. The time now is 07:14 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration