Linux Shell Scripting: Recursively loop through directories and files

Posted on 2013-11-08
Last Modified: 2013-11-08
This only returns results from the directory: "dir" but I want results for all files in "dir" including all files in all subdirectories of "dir".
f in dir/*
 echo File: $f

Open in new window

Question by:hankknight
LVL 84

Assisted Solution

ozo earned 50 total points
ID: 39634255
find dir -exec echo File:  {} \;
find dir -print | xargs -L1 echo File:
LVL 16

Author Comment

ID: 39634301
How can I integrate that with my loop?  This does not work:
find qqq/ -exec echo File:  {}
 echo File: $f

Open in new window

LVL 68

Accepted Solution

woolmilkporc earned 450 total points
ID: 39634364
for f in $(find /path/to/qqq -type f)
 echo File: $f

To display just the filename without the path component:

echo File: $(basename $f)

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now