Home Linux Shell Script that list the all files in a directory

Shell Script that list the all files in a directory

by Anup Maurya
8 minutes read

Shell Script that takes directory as input from the user and then print all the files present in a directory.

Shell Script that list the all files in a directory

#!/bin/bash

echo "Enter the directory path:"
read directory_path

if [ -d "$directory_path" ]
then
  cd "$directory_path"
  echo "List of all files in directory $directory_path:"
  ls
else
  echo "$directory_path is not a valid directory path."
fi

Save this script in a file with a .sh extension, such as main.sh. Then, navigate to the directory where you saved the script in your terminal, and execute the script by typing ./main.sh or bash main.sh.

The script will prompt you to enter the directory path that you want to list the files for. Once you enter the directory path, the script will check if it is a valid directory path.

If it is, the script will navigate to that directory and display a list of all the files in that directory. If it is not a valid directory path, the script will display an error message.

related posts

Leave a Comment

Enable Notifications OK No thanks