DEV Community

Kazuya Matsumoto
Kazuya Matsumoto

Posted on

List all only files under directory with shell script

Thanks to @BenSincla, this article become very simple!

For example, you have directory like this.

$ tree A
A
├── B
│   └── b.txt
├── C
│   └── c.txt
└── a.txt

2 directories, 3 files
Enter fullscreen mode Exit fullscreen mode

Relative path

find ./A -type f
Enter fullscreen mode Exit fullscreen mode

Absolute path

find ./A -type f | \
while read file; do
  echo $file | sed -e "s|^.|$PWD|g"
done
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
moopet profile image
Ben Sinclair

This seems like a long-winded way of doing find -type f A ?

Collapse
 
fennecdjay profile image
Jérémie Astor

When searching for a particluar extension, I use (in zsh)

ls **/*.gw

Just replace by what you need.

Collapse
 
kazuwombat profile image
Kazuya Matsumoto

You are right!.
I rewrite the article. Thank you!

Collapse
 
moopet profile image
Ben Sinclair

I mean it's a nice little explanation of how to string together commands to do something new :)