AshKeys

Confessions and Confusions of a Freelance Fullstacker.

Ashok Mannolu Arunachalam, How toLinuxBashShellTerminal
Back

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.

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

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

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