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 02-11-2017, 02:55 PM   #1
Erichg2013
LQ Newbie
 
Registered: Feb 2017
Posts: 1

Rep: Reputation: Disabled
Loop through folders


Hi everyone,

I have around 40 folder where I want to be able to loop through them and perform some calculations to each file in each of the folders. Do you know of a way to that when I have the main folder (with the 40 folders) as current directory?

Any help will be much appreciated.

Best,

Eric
 
Old 02-11-2017, 03:06 PM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,463
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
Welcome.

There are several ways. See find and shopt -s globstar for two examples: http://mywiki.wooledge.org/BashFAQ/030#Recursively

Can you go into more detail about the actions you wish to perform in each folder?
 
Old 02-11-2017, 05:55 PM   #3
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
Code:
#!/bin/sh

for dir in ./*; do
  if [ -d "$dir" ]; then
    for file  in "$dir"/*; do
      if [ -f "$file" ]; then
       printf "do something with %s\n" "$file"
      fi
    done
  fi
done
A bit twisted, for my taste. The mentioned find and the given link are probably more clear. I used file as a varname to make it clear what is a file and what a folder, else i avoid such.
To put it short: it is a proposal, not well tested and not much thoughts on drawbacks of it.
 
Old 02-11-2017, 06:02 PM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,422
Blog Entries: 9

Rep: Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668
find is the appropriate tool. look at the '-exec' option, amongst others:
Code:
man find
 
  


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
Loop command on folders in a directory azurite Linux - Newbie 44 07-06-2016 04:32 PM
Loop mounting compressed folders: which ones are safe to compress? mahkoe Linux - General 1 12-13-2011 04:13 PM
Include loop to get files from different folders that have same name Perseus Programming 9 10-10-2011 04:37 AM
[SOLVED] Bash question (loop though folders and run a command) takayama Programming 4 02-14-2011 06:20 AM
BASH: Unzip files/folders from archive one at a time using a loop cade Programming 3 05-16-2010 11:48 PM

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

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