LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-25-2017, 02:51 PM   #1
azurite
LQ Newbie
 
Registered: May 2016
Posts: 29

Rep: Reputation: Disabled
Need to loop script on subfolders in a directory


Hello everyone,

I'm am a newbie to coding so I am reaching out in hopes that I can get some help from this forum.

I am trying to run the script below from a single directory, however the directory has many subfolders where the file I need is located. I have bolded the file name below also.

Another thing is that for me to run this script successfully, at the moment I have to type in the actual path to the .ecclog file in terminal like so
Code:
scriptname.sh /TestFolder/.extension

Is there any way I can make the script run through all the subfolders and find the file it needs without having to type in the actual path in terminal? I have about 160 of these folders and manually running the script on each of those folders one by one would take forever. I'm looking for an easier way and I hope someone here can help me out!

Last edited by azurite; 07-06-2017 at 11:05 PM.
 
Old 06-25-2017, 04:09 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
not tested I just wrote it in here.

Code:
#!/bin/bash

working_dir="put search path here - parent/root dir name"

while read FILENAME
do

add code in here for file processing-
it will go through each file separately - recursively 

done< <(find "$working_dir" -type f -name "*.ecclog")
this way you can have the script anywhere in your system and when you run it it will still search wherever the path is set to look.

the value within FILENAME is a complete absolute path and filename. you might have to break it down to work with it using
Code:
f=$FILENAME
path=${f%/*}
xfile=${f##*/}
title=${xfile%.*}
ext=${xfile##*.}

Last edited by BW-userx; 06-25-2017 at 04:20 PM.
 
Old 06-25-2017, 04:28 PM   #3
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
This should do it, or at least help you:

Code:
find . -name '*.ecclog' -exec scriptname.sh {} \;
That will run scriptname.sh with a parameter of every file ending in .ecclog from the current directory downwards.
 
1 members found this post helpful.
Old 06-25-2017, 08:17 PM   #4
azurite
LQ Newbie
 
Registered: May 2016
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
not tested I just wrote it in here.

Code:
#!/bin/bash

working_dir="put search path here - parent/root dir name"

while read FILENAME
do

add code in here for file processing-
it will go through each file separately - recursively 

done< <(find "$working_dir" -type f -name "*.ecclog")
this way you can have the script anywhere in your system and when you run it it will still search wherever the path is set to look.

the value within FILENAME is a complete absolute path and filename. you might have to break it down to work with it using
Code:
f=$FILENAME
path=${f%/*}
xfile=${f##*/}
title=${xfile%.*}
ext=${xfile##*.}
Quote:
Originally Posted by Laserbeak View Post
This should do it, or at least help you:

Code:
find . -name '*.ecclog' -exec scriptname.sh {} \;
That will run scriptname.sh with a parameter of every file ending in .ecclog from the current directory downwards.
If I understand correctly, your suggestions will make the script go into every subfolder and grab the .ecclog file unique to that subfolder and it will continue to do so until it has gone through and ran on all the 160 subfolder that I have? I ask because I forgot to mention that every subfolder that I have, has inside it a uniquely numbered file that ends in .ecclog.
 
Old 06-25-2017, 08:31 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Yes it will find all files ending in 'ecclog' anywhere below the leading directory
 
Old 06-26-2017, 01:11 AM   #6
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by azurite View Post
If I understand correctly, your suggestions will make the script go into every subfolder and grab the .ecclog file unique to that subfolder and it will continue to do so until it has gone through and ran on all the 160 subfolder that I have? I ask because I forgot to mention that every subfolder that I have, has inside it a uniquely numbered file that ends in .ecclog.
Yes, it will. If you want to test what it does, you can put echo in to see what will be run, like this:

Code:
find . -name '*.ecclog' -exec echo scriptname.sh {} \;
 
Old 06-26-2017, 07:47 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Laserbeak View Post
Yes, it will. If you want to test what it does, you can put echo in to see what will be run, like this:

Code:
find . -name '*.ecclog' -exec echo scriptname.sh {} \;
the only big difference between what I wrote and this one is placement of the "command/script' when ran - my code was so you can run the script from anywhere, whereas this line of code you have to be within the parent directory, that can be ealiy changed to.
Code:
find /path/to/look/into -name '*.ecclog' -exec echo scriptname.sh {} \;
that too should work. if the while loop then within your coe a few lines could be removed to accommodate the loop, that basename part because the other chunk of code
Code:
f=$FILENAME
path=${f%/*}
xfile=${f##*/}
title=${xfile%.*}
ext=${xfile##*.}
gives you everything you need to work with, the path to the file "path", the file name "title" - the extension of same said file "ext"


so you can just sit in your home dir and run that one liner and or the code in while loop I gave you in a script.

Either way that should getter done
 
Old 06-26-2017, 08:53 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935
Don't try to do it in bash. Every "real" scripting language that I can think of has a file-finder. Simply choose the language you like, and insert a #!shebang line as the first line of your script identifying that language. Then, write the "shell script" in that language and you're done.
 
1 members found this post helpful.
Old 06-26-2017, 09:27 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
all of this:

Code:
#!/bin/sh

if [ $# -lt 1 ] ; then 
  echo "Usage: `basename $0` <eddy current ecclog file>"
  exit 1;
fi

logfile=$1;
basenm=`basename $logfile .ecclog`;



nums=`grep -n 'Final' $logfile | sed 's/:.*//'`;
basename gets you the prefex of the filename, yes?
Code:
userx%voider ⚡ ~ ⚡> testfile="home/dir/filename.egg"
userx%voider ⚡ ~ ⚡> basenm=$(basename $testfile .egg)
userx%voider ⚡ ~ ⚡> echo "$basenm"
filename
userx%voider ⚡ ~ ⚡>
becomes this:
Code:
#!/bin/bash

working_dir=

while find FILENAME
do

f=$FILENAME
path=${f%/*}
xfile=${f##*/}
title=${xfile%.*}
ext=${xfile##*.}

basenm=$title
#searching for the keyword 'Final' inside file using complete path and file name.
nums=$(grep -n 'Final' $FILENAME | sed 's/:.*//')

the rest of your code...

done< <(find "working_dir" -type f -name "*.ecclog" )
or this
Code:
#!/bin/bash

working_dir=

while find FILENAME
do

basenm=$(basename $FILENAME .ecclog)

#searching for the keyword 'Final' inside file using complete path and file name.

nums=$(grep -n 'Final' $FILENAME | sed 's/:.*//')

the rest of your code...

done< <(find "working_dir" -type f -name "*.ecclog" )

Last edited by BW-userx; 06-26-2017 at 09:29 AM.
 
Old 06-26-2017, 01:24 PM   #10
azurite
LQ Newbie
 
Registered: May 2016
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
all of this:

Code:
#!/bin/sh

[strike]if [ $# -lt 1 ] ; then 
  echo "Usage: `basename $0` <eddy current ecclog>
....
....
becomes this:
Code:
#!/bin/bash

working_dir=

while find FILENAME
do

f=$FILENAME
path=${f%/*}
xfile=${f##*/}
title=${xfile%.*}
ext=${xfile##*.}
...
...
Quote:
Originally Posted by Laserbeak View Post
Yes, it will. If you want to test what it does, you can put echo in to see what will be run, like this:

Code:
find . -name '*.ecclog' -exec echo scriptname.sh {} \;
Quote:
Originally Posted by grail View Post
Yes it will find all files ending in 'ecclog' anywhere below the leading directory


Okay, I ran the following in terminal but received an error (see screenshot). I'm not sure what I'm doing wrong. Please excuse me if it's something blatantly obvious -- I'm very new to scripting so you guys might have to really walk me through it baby steps.
Code:
find . -name '*.ecclog' -exec ec_plot.sh {} \;
Here's a screenshot of my directory and the error I received in terminal.
Attached Thumbnails
Click image for larger version

Name:	Screenshot at 2017-06-26 11_16_32.jpg
Views:	11
Size:	164.3 KB
ID:	25329  

Last edited by azurite; 06-26-2017 at 01:25 PM.
 
Old 06-26-2017, 01:40 PM   #11
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by azurite View Post
Okay, I ran the following in terminal but received an error (see screenshot). I'm not sure what I'm doing wrong. Please excuse me if it's something blatantly obvious -- I'm very new to scripting so you guys might have to really walk me through it baby steps.
Code:
find . -name '*.ecclog' -exec ec_plot.sh {} \;
Here's a screenshot of my directory and the error I received in terminal.
Several possible issues:

You may need to put the full pathname of the script in the find command, so instead of just ec_plot.sh, it'd be /path/to/ec_plot.sh

Or maybe your shebang isn't right in ec_plot.sh itself. Make sure you have

Code:
#!/bin/bash
or whatever is appropriate as the first line in your script. You can find out where bash is installed on your system by typing in:

Code:
which bash
Plus make sure the script is set to be executable:

Code:
chmod 755 ec_plot.sh
edit:

Ooh, I just saw your script, and the shebang problem is almost certainly it! You have #!/bin/sh which probably doesn't exist on your system, replace it with #!/bin/bash

Last edited by Laserbeak; 06-26-2017 at 01:43 PM.
 
Old 06-26-2017, 01:47 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
make sure that the ec_plot.sh is in the very same dir you are in when you are running that
Code:
find . -name '*.ecclog' -exec ec_plot.sh {} \;
it looks like you are right clicking desktop -> open terminal. that gets you the current dir is desktop.
if you do that then just
Code:
cd /path to where your db files are at[enter]
and that ec_plot.sh has to be in that dir too. else you need to add absolute path within the
Code:
find /path/where/to/look -name '*.ecclog' -exec /path/where/this/is/at/ec_plot.sh {} \;
 
Old 06-26-2017, 01:50 PM   #13
azurite
LQ Newbie
 
Registered: May 2016
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Laserbeak View Post
Several possible issues:

You may need to put the full pathname of the script in the find command, so instead of just ec_plot.sh, it'd be /path/to/ec_plot.sh

Or maybe your shebang isn't right in ec_plot.sh itself. Make sure you have

edit:

Ooh, I just saw your script, and the shebang problem is almost certainly it! You have #!/bin/sh which probably doesn't exist on your system, replace it with #!/bin/bash
the script is executable and I fixed the shebang as you suggested. Now, I get the following error.

Code:
username@computer:~/Desktop/DWITESTSUB$ find . -name '*.ecclog' -exec /home/natasha/Desktop/
DWITESTSUB/ec_plot.sh
find: missing argument to `-exec'

Last edited by azurite; 06-26-2017 at 01:51 PM.
 
Old 06-26-2017, 01:53 PM   #14
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
If I'm right, and /bin/sh isn't there, you can also copy /bin/bash to /bin/sh instead of changing your script. Bash actually changes its behavior slightly when ran as sh to be as compatible as possible with the original sh. If the script was written to run in sh, you could run into problems when running it in bash when not in sh emulation mode. (Although the differences are not very many)
 
Old 06-26-2017, 01:57 PM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
example
script named
runFindFromAfar
Code:
#!/bin/bash

echo $1

Code:
userx%slackwhere ⚡ ~ ⚡> find /home/userx/scripts/testing/testdata -type f -exec /home/userx/scripts/testing/runFindFromAfar {} \;
/home/userx/scripts/testing/testdata/File-May-4
/home/userx/scripts/testing/testdata/File-May-7
/home/userx/scripts/testing/testdata/File-May-6
/home/userx/scripts/testing/testdata/File-May-3
/home/userx/scripts/testing/testdata/File-May-8
/home/userx/scripts/testing/testdata/File-May-0
/home/userx/scripts/testing/testdata/File-May-5
/home/userx/scripts/testing/testdata/File-May-2
/home/userx/scripts/testing/testdata/File-May-1

Last edited by BW-userx; 06-26-2017 at 01:58 PM.
 
  


Reply



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
How to make a loop to enter in a folder and then in its subfolders. (BASH) asero12 Programming 2 11-14-2013 11:29 PM
[SOLVED] Help making specific script loop over multiple files in directory novloski Linux - Newbie 3 05-11-2011 05:13 AM
[SOLVED] Shell Script - Use variable in a for loop with directory path Tech109 Linux - General 2 01-19-2011 10:22 AM
[SOLVED] Bash help needed: How to recursively browse subfolders in a for loop? frisil Linux - General 13 02-20-2010 12:08 AM
Loop through all files in a directory. Bash/Perl script? Nzo Linux - Newbie 9 12-09-2009 07:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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