[awk] print all filenames in directory

Hello,

I was given a (I suppose) a simple task which I don't know how to do. Before coming here I have read a dozen of awk tutorials (full read, not just line-skipping) but I still don't know how to do this.

Task:
Write an script file 'check.awk' with a parameter current directory that finds all read-only files and displays these. Check also that these exist and are not directories.

First off, I don't really understand the instruction. How to pass a parameter current directory? "./"?
I started with a simplified task - to print out a string for each file in a given directory like this:

BEGIN{
}
{
  print "found a file"
}
END{
}

But, it doesn't work:
awk -f 9.awk testdir
awk: 9.awk:3: fatal: cannot open file `testdir' for reading (Success)

Please help me out, I've been hammering my head for hours now :wall:

Removed homework warning as per OP's request (the OP claims this is not a school work).

To the OP:

You can definitely use awk for this task and it's definitely not the right tool for this task ...

My point exactly! I know this is a piece of cake with "ls" and I don't really understand why to do it with awk. It's like driving a screw with a hammer - totally wrong tool.

But this doesn't change my situation, sadly.

Can someone tell me what is wrong with
awk: 9.awk:3: fatal: cannot open file `testdir' for reading (Success)
?

Well, you could issue 'ls' from within awk using 'system' call :slight_smile:

echo | awk '{system("ls") }' 

This single line will do it, if you can use ls. It will list all files that are bigger than 0.

ls -l | awk '$5 > 0 {print $9}'

OP needs to check file is read only and not a directory, noting was mentioned about size > 0

How about this:

echo * | awk '{ for(f=1;f<=NF;f++) { if((getline dummy < $f) == -1) print $f; else close($f) }}'

Or to read directory passed in as input:

echo /home/test/dir | awk ' { DIR=$0; while ("echo "DIR"/*" | getline ) { for(f=1;f<=NF;f++) if(getline dummy < $f == -1) print $f; close($f) } }'