I'm trying to write a script that asks the user if they are sure they want to move a file to a certain directory. The script needs to check if the file already exists in the target directory and if it so, then asks the user if they want to overwrite the existing file with the one they are trying to move.

How should the file test in my if statement look? At the moment I have

if [[ -e /targetdirectory ]] ; then........

but that doesn't seem to be the right solution?

  • 1
    mv -i does the prompt thing for you...
    – thrig
    May 2, 2016 at 16:53

2 Answers 2


The test you are looking for is:

[[ -f /path/to/file ]]

where /path/to/file is the path to the file you are testing to see if it exists. You can place this test in a conditional using (provided DEST_DIR is the destination directory for the file SRC_FILE):

if ! [[ -f DEST_DIR/file ]]; then

where ! returns true is the file does not exist. This will move the SRC_FILE to DEST_DIR if it does not already exist in DEST_DIR.



if [ -e filename ]; then 
# something

with just one square bracket

  • 2 brackets is fine for bash and even helps when there are special characters in a filename, especially if the name is stored in a variable May 2, 2016 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .