Shell script which will check the target file and folder exists and copy it

Hi All,

I am a beginner in this and trying to write a shell script in linux which will :

  1. Ask for a file name and check if its exists.
  2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists.
  3. If target folder exists it will copy that file in to it.

I have written the below code and it's working fine if file and folder exists but not working properly when any of them is not exists.

#!/bin/bash

read -p "Enter file name : " filename
echo "Please wait checking if $filename exists ..."

if [ -f "${filename}" ]

then

        echo "File $filename exist."
        read -p "Enter location to copy $filename : " location

else

        echo "$0: $filename not found."

fi
                if [ -d "${location}" ]

                then

                        echo "Target location found. Initiating file copy to $location."
                        cp -v "$filename" "$location"
                        echo "$filename copied to $location."

                else

                        echo "$0: $location not found."

                fi

Please let me know if i missed something.

Thanks,
Ashish

@Ashish

Moved the if [ -d "${location}" ] inside the first successful if statement, since it is the only place that belongs, otherwise it will execute even when it is established that the file doesn't exist. The only thing I added for you to ponder is the if [ $? -eq 0 ]; . Assuming that cp is successful is not truthful to the following echo .

#!/bin/bash

read -p "Enter file name : " filename
echo "Please wait checking if $filename exists ..."

if [ -f "${filename}" ]
then
    echo "File $filename exist."
    read -p "Enter location to copy $filename : " location
    if [ -d "${location}" ]
    then
        echo "Target location found. Initiating file copy to $location."
        cp -v "$filename" "$location"
        if [ $? -eq 0 ]; then
            echo "$filename copied to $location."
        fi
    else
        echo "$0: $location not found."
    fi
else
    echo "$0: $filename not found."
fi
1 Like

Hi Aia, thanks for the help.

Now i am trying to do :

  1. Ask for a file name and check if its exists.
  2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists, if doesn't exits it will create one with with the $location value.
  3. It will copy that file in to it.

I have made below changes in to above script. Its working but i facing minor issue in it:

If i am trying to copy existing file in to non existing folder it's working fine and throwing below message

/etc/inittab copied to /OOO.

But if i am copying existing file in to existing folder i am not getting above message.

#!/bin/bash

read -p "Enter file name : " filename
echo "Please wait checking if $filename exists ..."

if [ -f "${filename}" ]
then
    echo "File $filename exist."
    read -p "Enter location to copy $filename : " location

    if [ -d "${location}" ]
    then
        echo "Target location found. Initiating file copy to $location."
        cp -v "$filename" "$location"
    else
        echo "$location not found ! Creating..."
        mkdir -p "$location"
        echo "$location folder created. Copying $filename to $location ..."
        cp -v "$filename" "$location"

        if [ $? -eq 0 ]
        then
                echo "$filename copied to $location."
        else
                echo "ERROR while copying $filename to $location. Please check write permission or disk space and try again !!!"
        fi

     fi

else
    echo "$filename not found."
fi

I will really appreciate if you point out needed changes.

Thanks,
Ashish

You have no echo command after the copy to existing folder.
Add a message and you'll get one :wink:

What if the creation of the folder fails?
So you could move the:

        if [ $? -eq 0 ]
        then
                echo "$location created"
        else
                echo "ERROR while creating $location. Please check write permission or disk space and try again !!!"
                exit 1
        fi

To just below mkdir -p "$location"

Hope this helps

1 Like