create a bash script for list
- User
create a bash script for list
- Assistant
Certainly! Below is an example of a simple bash script that lists files and directories in a specified directory. You can create a file with a
.shextension and paste the script into it. Before running the script, don't forget to make it executable usingchmod +x script_name.sh.Here's the script:
#!/bin/bash # List the files and directories in a specified directory # Usage: ./list_files.sh [directory] # Check if a directory is provided as an argument if [ -z "$1" ]; then # If no argument is provided, use the current directory DIRECTORY="." else # Use the provided argument as the directory DIRECTORY="$1" fi # Check if the specified directory exists if [ -d "$DIRECTORY" ]; then echo "Listing files and directories in: $DIRECTORY" # List files and directories ls -la "$DIRECTORY" else echo "Error: $DIRECTORY is not a valid directory." fiHow to Use: