pedja
pedja

Reputation: 3413

Iterate all files in a directory (including files that start with '.' - hidden files) in linux/android

I want to iterate through all files in a specified directory including hidden files in a script

i tried something like this:

for f in /home/pedja/test/*; do

Which shows only visible files (files that don't start with .)
If i do this:

for f in /home/pedja/test/.*; do

It shows only hidden files (files that starts with .)

How can i list all files in a single for loop

Upvotes: 0

Views: 446

Answers (4)

konsolebox
konsolebox

Reputation: 75578

This form runs it on a subshell but that's the safest you can have when your shell does not support process substitution.

find /home/pedja/test/ -mindepth 1 -maxdepth 1 -name '.*' | while read -r file; do
    ...
done

Upvotes: 0

vks
vks

Reputation: 67988

           IFS=$'\n'; for f in $(ls -a);do echo "$f"; done

Upvotes: 1

lihao
lihao

Reputation: 781

use brace expansion:

for f in /home/pedja/test/{,.}*; do echo "$f"; done

Upvotes: 1

jotik
jotik

Reputation: 17920

Try this:

for f in `find /home/pedja/test -maxdepth 1`; do

Upvotes: 1

Related Questions