hometools

Loop Through Files in a Directory (non-recursively)

April 2026
loop-through-files.bash
#!/bin/bash

INPUT_DIR=samples

for file in $INPUT_DIR/*
do 
  if [ -f "$file" ]
  then 
    echo "$file" 
    BASENAME=$(basename -- "$file")
    STEM="${BASENAME%.*}"
    EXTENSION="${BASENAME##*.}"
    echo $BASENAME
    echo $STEM
    echo $EXTENSION
  fi 
done
samples/alfa.txt
alfa.txt
alfa
txt
samples/bravo.txt
bravo.txt
bravo
txt
samples/charlie.txt
charlie.txt
charlie
txt
samples/delta.txt
delta.txt
delta
txt
samples/alfa.txt
alfa.txt
alfa
txt
samples/bravo.txt
bravo.txt
bravo
txt
samples/charlie.txt
charlie.txt
charlie
txt
samples/delta.txt
delta.txt
delta
txt