Linux / Unix Shell Scripting: Create Filename By Day Of The Week

You can create a filename based on the day of the week in a shell script using the date command. The date command can be used to display the current date and time, and you can use the %A format specifier to display the full name of the day of the week.

Here’s an example of how to create a filename based on the day of the week:

#!/bin/bash

# Get the full name of the day of the week
day=$(date +%A)

# Create a filename based on the day of the week
filename="data-$day.txt"

# Write some data to the file
echo "Hello, World!" > $filename

# Print the filename
echo "File created: $filename"

In this example, the date +%A command is used to get the full name of the day of the week, which is stored in the day variable. Then, the filename variable is set to a string with the format "data-day-of-the-week.txt". Finally, the script writes the string “Hello, World!” to a file with the generated filename and prints the filename to the terminal.

Note: In this example, the file is created in the current working directory. If you want to create the file in a specific directory, you can modify the filename variable accordingly.

Leave a Comment