1

I am trying to make a simple script that would move junk files into a bin like directory.

The code i wrote is here:

echo "Which file/s you want to delete?"

read fileName

if [ -d  "/home/user/.waste"]
then
    #moves the file to .waste
    mv $fileName /home/user/.waste
    echo "File deleted."

else
    #creates the directory
    mkdir /home/user/.waste
    mv $fileName /home/user/.waste
    echo "waste bin created and file deleted."
fi

When i run the script while having the directory it keeps going to the else option and does not recognize the value i entered in the if statement.

Sami-M
  • 57
  • 6

1 Answers1

1

I suspect the test is incorrect (needs a space before the closing bracket). if [ -d "DIR" ].

#!/bin/bash

WASTE_DIR="/home/user/.waste"

echo "Which file/s you want to delete?"

read fileName

if [ ! -f "$fileName" ]
then
  echo "Unable to find file "$fileName"
fi

if [ -d "$WASTE_DIR" ]
then
  mv "$fileName" "$WASTE_DIR"
  echo "File moved to waste"
else
  mkdir -p "$WASTE_DIR"
  mv "$fileName" "$WASTE_DIR"
  echo "$WASTE_DIR created and file moved"
fi
KevinO
  • 4,303
  • 4
  • 27
  • 36