Here's a Linux bash script that performs the following tasks:

  1. Prompts the user to enter a directory path.
  2. Checks if the directory exists and if not, creates it.
  3. Prompts the user to enter a file name.
  4. Checks if the file exists in the directory and if not, creates it.
  5. Appends the current date and time to the file.
  6. Displays the contents of the file.

#!/bin/bash

# Prompt user to enter directory path
echo "Enter a directory path:"
read directory

# Check if directory exists and create it if not
if [ ! -d "$directory" ]; then
  mkdir "$directory"
  echo "Directory created: $directory"
fi

# Prompt user to enter file name
echo "Enter a file name:"
read filename

# Check if file exists and create it if not
filepath="$directory/$filename"
if [ ! -f "$filepath" ]; then
  touch "$filepath"
  echo "File created: $filepath"
fi

# Append current date and time to file
date_time=$(date "+%Y-%m-%d %H:%M:%S")
echo "$date_time" >> "$filepath"

# Display contents of file
echo "Contents of $filepath:"
cat "$filepath"

When you run this script, it will prompt you to enter a directory path and a file name. If the directory and file do not exist, the script will create them. Then, it will append the current date and time to the file and display its contents.

To run the script, save it in a file with a .sh extension (e.g. file_handling.sh), navigate to the directory where the file is saved, and enter the following command in the terminal:

bash file_handling.sh

This will execute the script and guide you through the process of creating a directory, a file, and appending data to the file.

Here's a breakdown of what each line in the script does:

#!/bin/bash

This line specifies the shell interpreter to use for running the script. In this case, it's bash.

echo "Enter a directory path:"
read directory

These lines prompt the user to enter a directory path and store the input in the variable directory.

if [ ! -d "$directory" ]; then
  mkdir "$directory"
  echo "Directory created: $directory"
fi

This block of code checks if the directory specified by the user exists. If it doesn't exist, it creates the directory using the mkdir command and prints a message to the console indicating that the directory was created.

bashCopy codeecho "Enter a file name:"
read filename

These lines prompt the user to enter a file name and store the input in the variable filename.

filepath="$directory/$filename"
if [ ! -f "$filepath" ]; then
  touch "$filepath"
  echo "File created: $filepath"
fi

This block of code creates a file in the specified directory if it does not already exist. It uses the touch command to create the file and prints a message to the console indicating that the file was created.

date_time=$(date "+%Y-%m-%d %H:%M:%S")
echo "$date_time" >> "$filepath"

This block of code gets the current date and time using the date command and formats it as YYYY-MM-DD HH:MM:SS. It then appends the date and time to the file specified by the user.

echo "Contents of $filepath:"
cat "$filepath"

Finally, this block of code displays the contents of the file to the console, along with a message indicating which file was displayed.

Overall, this script is a simple example of how to handle file I/O in bash, including checking for file and directory existence, creating new files and directories, and appending data to files.