user1161080
user1161080

Reputation: 175

Shell scripting- processing hidden files

I'm working on a project where I have to process the contents of a directory passed in as an argument, and I need to include invisible files (ones that start with .) as well. This is how I'm approaching it

#!/bin/bash

cd $1
for file in `dir -a -d * `;
do
#more code blah blah 

even though I use the -a tag on the dir command, it still ignores invisible files. Any ideas why?

Upvotes: 0

Views: 979

Answers (1)

SiegeX
SiegeX

Reputation: 140457

Just do:

#!/bin/bash

shopt -s dotglob
cd "$1"
for file in *; do
  # more code blah blah
done

From the bash manpage

When a pattern is used for filename expansion, the character ‘.’ at the start of a filename or immediately following a slash must be matched explicitly, unless the shell option dotglob is set.

Upvotes: 5

Related Questions