dawnoflife
dawnoflife

Reputation: 1652

Looping through sub folders not working Unix

I have a folder with multiple sub-folders and each sub-folder contains 10-15 files. I want to perform a certain operation only on the text files in these folders. The folders contain other types of files as well. For now, I am just trying to write a simple for loop to access every file.

for /r in *.txt; do "need to perform this on every file"; done

This gives me an error -bash: ``/R': not a valid identifier

Thanks for the help.

P.S I am using cygwin on Win 7.

Upvotes: 0

Views: 933

Answers (3)

glenn jackman
glenn jackman

Reputation: 246754

With bash:

shopt -s globstar
for file in **/*.txt; do ...

Upvotes: 2

C. Ramseyer
C. Ramseyer

Reputation: 2382

Your /r is the problem, that's not a valid identifier (as bash said, you need to drop the /). Also, this won't recurse into subdirectories. If your operation is simple, you can directly use the exec option of find. {} is a placeholder for the filename.

find . -name "*.txt" -exec ls -l {} \;

Otherwise, try something like

for r in $( find . -name "*.txt" ) ; do
    echo $r
    #more actions...
done

Upvotes: 4

stefan bachert
stefan bachert

Reputation: 9608

I would use "find" for your application case

Something like

find . -name "*.txt" -exec doSomeThing {} \;

Upvotes: 1

Related Questions