user3091585
user3091585

Reputation: 41

How to look at files in a directory in Unix?

I'm writing a shell script that needs to print various data about the files and subdirectories in a directory. I start out by entering in the name of my directory like this. name=$1 so i'm just wondering how i would look in the directory and use the files and such inside it. Like how do i reference file1.txt and file2.txt etc.?

Upvotes: 0

Views: 89

Answers (3)

0x90
0x90

Reputation: 41002

find . -name '*.txt' -exec cat {} +

Or

find . -name "*.txt" | xargs cat

see xargs.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201487

You can use the find and wc command(s) to do that,

# As an example,
find "$name" -perm -u=rwx | wc -l 

Upvotes: 0

nPn
nPn

Reputation: 16758

assuming you have the directory you are interested in in $name then to get the files just do

files=`ls $name`

then for example you could to some thing like this

   files=`ls $name`
    for file in $files
    do
    if [ -r $name/$file ]
    then
    echo "$file is readable"
    fi
    done

Upvotes: 1

Related Questions