0% found this document useful (0 votes)
8 views1 page

Shell Script For Directory

This Bash script renames all text files in a specified directory by appending today's date to their filenames. It checks if the directory exists and processes each text file if it is a regular file. The script requires a directory argument and provides usage instructions if none is given.

Uploaded by

saiakkina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Shell Script For Directory

This Bash script renames all text files in a specified directory by appending today's date to their filenames. It checks if the directory exists and processes each text file if it is a regular file. The script requires a directory argument and provides usage instructions if none is given.

Uploaded by

saiakkina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

```bash

#!/bin/bash

# Function to rename text files in a directory with today's date


rename_files() {
local directory="$1"
local today=$(date +"%Y%m%d")

# Check if the directory exists


if [ -d "$directory" ]; then
# Loop through all text files in the directory
for file in "$directory"/*.txt; do
# Check if the file is a regular file
if [ -f "$file" ]; then
# Get the filename without the extension
filename=$(basename "$file" .txt)
# Rename the file with today's date appended
mv "$file" "$directory/$filename-$today.txt"
fi
done
echo "Text files in $directory renamed with today's date."
else
echo "Directory $directory does not exist."
fi
}

# Check if directory argument is provided


if [ $# -ne 1 ]; then
echo "Usage: $0 <directory>"
exit 1
fi

# Call the function with the provided directory argument


rename_files "$1"
```

Save the script to a file (e.g., `rename_files.sh`), make it executable with the
command `chmod +x rename_files.sh`, and then execute it with `./rename_files.sh
<directory>`, where `<directory>` is the directory path containing the text files
you want to rename with today's date appended to their filenames.

You might also like