Loop through a folder and list the files
I have a folder named'sample' and it contains three files I want to write a shell script which will read these files inside the sample folder and post them using curl in an http site
I wrote the following for the listing of the files inside the folder
for dir in sample/*; do
echo $dir;
done
But the following result comes to me
sample/log
sample/clk
sample/demo
It is attaching the parent folder in it. I want the output as follows (without the parent folder name)
log
clk
demo
How should you do it?
Best Answer
Use basename
to strip the leading path off of the files.
for file in sample/*; do
echo "$(basename "$file")"
done
But why not?
( cd sample; ls )