[awk] print all filenames in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [awk] print all filenames in directory
# 1  
Old 03-12-2011
[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:

Code:
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 Smilie
# 2  
Old 03-12-2011
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 ...

Last edited by radoulov; 03-12-2011 at 07:11 AM..
# 3  
Old 03-12-2011
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)
?
# 4  
Old 03-13-2011
Well, you could issue 'ls' from within awk using 'system' call Smilie
Code:
echo | awk '{system("ls") }'

# 5  
Old 03-13-2011
Can you us "ls" at all?

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}'
# 6  
Old 03-13-2011
OP needs to check file is read only and not a directory, noting was mentioned about size > 0

How about this:
Code:
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:

Code:
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) } }'


Last edited by Chubler_XL; 03-13-2011 at 11:30 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

print all filenames in directory with path to another file

hi, i have a directory at /path/unix with the following files 1.txt 2.txt 3.txt 4.txt I want to make another file called filenames.txt at a different location called /path/home. So, my output file would be /path/home/filenames.txt with contents /path/unix/1.txt... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

2. Shell Programming and Scripting

Print and Append the matching filenames

Hi, I want to check all files in a folder for some specific start string and append all matching filenames with _1, _2 .... for each file found in the directory. But, $file below gives me all details of the files like access permissions, owner and filename etc. I just want all the filenames... (3 Replies)
Discussion started by: chetancrsp18
3 Replies

3. Shell Programming and Scripting

Script template for inputting filenames and print results

Hi, Hope you are all well. New to scripting, and all those characters are all a new language for me. Though hoping to get my little head round it all sooner or later. I was wondering whether anyone could help with a script template example. What I would like to happen is to run the script... (8 Replies)
Discussion started by: loky27
8 Replies

4. UNIX for Dummies Questions & Answers

Filenames change in a directory

Hi I have abc_ahb_one.v abc_ahb_two.v abc_ahb_three.v ........l like this -----upto abc_ahb_ninety.v in some directory. I need to change those file names to like below. ... (5 Replies)
Discussion started by: praneethk
5 Replies

5. Shell Programming and Scripting

How to print the last column of the list contain filenames with spaces.

Hi experts, I have the following data: I want the last filed in the output. How to print the last field , It contains the file names and few filenames with white spaces . -rw-r--r-- 1 root root 0 2010-04-26 16:57 file1 2space_File.txt -rw-r--r-- 1 root root 0 2010-04-26... (2 Replies)
Discussion started by: rveri
2 Replies

6. Shell Programming and Scripting

Use filenames to create directory.

I have many files similar to this one: AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf. I want a directory named AC41 and to put the file AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf into the directory. Next, a directory named AC85 and put the file into it. Also, continue to cycle through... (1 Reply)
Discussion started by: ndnkyd
1 Replies

7. Shell Programming and Scripting

Print filenames with spaces using awk

Hello all, I want to list the file contents of the directory and number them. I am using la and awk to do it now, #ls |awk '{print NR "." $1}' 1. alpha 2. beta 3. gamma The problem I have is that some files might also have some spaces in the filenames. #ls alpha beta gamma ... (7 Replies)
Discussion started by: grajp002
7 Replies

8. Shell Programming and Scripting

Change all filenames in a directory

I have a directory of files and each file has a random 5 digit string at the beginning that needs to be removed. Plus, there are some files that will be identically named after the 5 digit string is removed and I want those eliminated or moved. any ideas? (17 Replies)
Discussion started by: crumb
17 Replies

9. Shell Programming and Scripting

concatenating the filenames in a directory

hi all, I have a requirement where in i have to read all the filenames based on a pattern from a directory and concatenate all these file names and write it to another file. i am using the following code to do this var1='' for filename in $_DIR/${FILE_NAME}* do if if then... (7 Replies)
Discussion started by: nvuradi
7 Replies

10. Shell Programming and Scripting

looping thru filenames in a directory

Hi, i am very new to UNIX, i am trying to loop thru the files in a directory. I got the filenames into a variable using $files=`ls` Here $files will contain <filename1> <filename2> <filename3> I want to get one filename at a time and append it to some some text. forexample, ... (1 Reply)
Discussion started by: silas.john
1 Replies
Login or Register to Ask a Question