How to check if the file exists?

You can check if a file exists in a shell script using the test command or the [ ] syntax. Here’s a simple example in a shell script:

#!/bin/bash

FILENAME="CharacterSpace.java"

if [ -e "$FILENAME" ]; then
    echo "File $FILENAME exists."
else
    echo "File $FILENAME does not exist."
fi

Steps to Use the Script:

  1. Create a new script file:

    nano check_file.sh
  2. Copy and paste the above code into the file.

  3. Save and exit the editor.

  4. Make the script executable:

    chmod +x check_file.sh
  5. Run the script:

    ./check_file.sh

This script will check if CharacterSpace.java exists and print the appropriate message.

0 Comments

no data
Be the first to share your comment!