lara
lara

Reputation: 503

How to print all file names in a folder with awk or bash?

I would like to print all file names in a folder.How can I do this with awk or bash?

Upvotes: 1

Views: 22970

Answers (6)

Vaishnavi Varma
Vaishnavi Varma

Reputation: 1

for f in *; do var=`find "$f" | wc -l`; echo "$f: $var"; done

This will print name of the directory and number of files in it. wc -l here returns count of files +1 (Including directory)

Sample output:

aa: 4 
bb: 4 
cc: 1 
test2.sh: 1

Upvotes: 0

Tomasz Skorka
Tomasz Skorka

Reputation: 31

Following is a pure-AWK option:

gawk '
    BEGINFILE {
        print "Processing " FILENAME
    };
' *

It may be helpful if you want to use it as part of a bigger AWK script processing multiple files and you want to log which file is currently being processed.

Upvotes: 3

Rashedul Islam
Rashedul Islam

Reputation: 939

This command will print all the file names:

for f in *; do echo "$f"; done

or (even shorter)

printf '%s\n' *

Alternatively, if you like to print specific file types (e.g., .txt), you can try this:

for f in *.txt; do echo "$f"; done

or (even shorter)

printf '%s\n' *.txt

Upvotes: 1

Ashish Kumar
Ashish Kumar

Reputation: 851

ls -l /usr/bin | awk '{ print $NF }' 

:)

Upvotes: 7

xception
xception

Reputation: 4287

find . -type f -maxdepth 1

exclude the -maxdepth part if you want to also do it recursively for subdirectories

Upvotes: 5

BjoernD
BjoernD

Reputation: 4780

/bin/ls does this job for you and you may call it from bash.

$> /bin/ls
[.. List of files..]

Interpreting your question you might be interested in iterating over every single file in this directory. This can be done using bash as well:

for f in `ls`; do
   echo $f;
done

Upvotes: 0

Related Questions