Answers for "bash for in directory"

4

bash for file in directory

# Basic syntax:
for file in /directory/*
do
	echo $file
done
# Where:
#	- the echo command is run on each file found using the search pattern 
#		(here, all files in the /directory/ folder)

# Example usage:
# Say you have the directory "/my/favorite/files" with the following files:
test.txt
my.txt
code.png

for file in /my/favorite/files/*.txt; do
	echo $file
done
--> test.txt
--> my.txt
# Note, code.png isn't printed because it doesn't match the search pattern
Posted by: Guest on January-17-2022
0

Bash dir in loop

mkdir /tmp/A /tmp/B /tmp/C "/tmp/ dir with spaces"
for file in /tmp/*/ ; do file="${file%/}"; echo "${file##*/}"; done
Posted by: Guest on December-08-2021

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language