LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 01: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, 02:06 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
Blog Entries: 4

Rep: Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971
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, 04: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, 05:02 PM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

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

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

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