EN
Bash - list files and directories
6 points
In this short article, we would like to show how to list regular files and directories located under indicated path using Bash.
Note: presented solution lets to list visible and hidden files and directories because of
.*
and*
used in thefor
loop.
print_directory.sh
file:
xxxxxxxxxx
1
2
3
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
4
5
cd "$CURRENT_DIR"
6
7
for i in .* *
8
do
9
if [ -d "$i" ] || [ -f "$i" ]
10
then
11
[ "$i" = "." ] && continue
12
[ "$i" = ".." ] && continue
13
14
echo "$i"
15
fi
16
done
Example output:
xxxxxxxxxx
1
.git
2
README.txt
3
documents
4
pictures
5
print_directory.sh
Directory structure:
xxxxxxxxxx
1
/c/files
2
|
3
+-- .git/
4
+-- documents/
5
+-- pictures/
6
| |
7
| +-- pic_1.jpg
8
| +-- pic_2.jpg
9
|
10
+-- print_directory.sh
11
+-- README.txt