Confessions and Confusions of Ashok M A. Personal and Professional Blog.

Ashok Mannolu Arunachalam, How toLinuxBashShellTerminal

How to get a list of files in a directory using Bash

Recently, I have been doing a lot of shell scripting to automate some time consuming manual processes. For instance, read configuration using xslt transformation stylesheets. >_O Though it was painful to my eyes, once done I felt bit proud of myself learning something new. (Yes, XSLT you go to my Skills List ^_^)

As part of that process, I had to iterate files in a configuration folder for transformation into .json files. We shall come to that later. Now, let's see the shell script to list out the files in a folder.

for f in /home/ashokma/ashkeys/config/*;
if [[ ! -d ${f} ]]; # Not if it is sub-directory.
echo "File Name with full path - ${f}"
echo -e "File Name with extension - $(basename ${f})"

You can strip the extension away from the file using this snippet:

$ echo -e "File Name with extension - $(basename $(echo "$f" | cut -f 1 -d '.'))"