Certainly! Here’s a simple shell script that takes a directory path as an argument and lists all the files in that directory using the ls
command:
#!/bin/bash
# Check if a directory path is provided as an argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <directory_path>"
exit 1
fi
directory_path="$1"
# Check if the directory exists
if [ ! -d "$directory_path" ]; then
echo "Directory not found: $directory_path"
exit 1
fi
# List all files in the directory
echo "Files in $directory_path:"
ls -p "$directory_path" | grep -v / # Exclude directories from the list
Save this script to a file (e.g., list_files.sh
), make it executable using chmod +x list_files.sh
, and then run it with the directory path as an argument:
./list_files.sh /path/to/directory
Replace /path/to/directory
with the actual directory path you want to list files for. The script will provide you with a list of files in the specified directory.