简体   繁体   中英

mv: cannot stat [DIRECTORY/FILE]: no such file or directory

EDIT: DIR_trash="trash"

I wrote a function to move a file to current directory.

if [ "$1" == "-u" ]
then
    if [ $# == 1 ]
    then
        echo "Something went wrong. Please make sure you're passing the name of the file/directory after '-u'."
    else
        if [ -f $DIR_trash/$2.zip ]
        then
            echo "$2.zip has been found in the trash."
            cd
            cd $DIR_trash
            sed -i "/$2/d" $file7
            mv -i /$DIR_trash/$2.zip .
            unzip $2.zip
            \rm $2.zip
            cd
        else
            echo "$2.zip has not been found in the trash."
        fi
    fi
fi

As you can see, there is a line of code which says:

mv -i /DIR_trash/$2.zip .

So basically I'm trying to move a file that I passed in argument 2 to current directory, from trash. I always run this script from home directory, which does have trash directory. This is what I get when I run this:

错误文件

Whenever I manually write this is in the Konsole (from home direcotry) it does work:

rm -u trash/d1 .

I'm out of ideas. Could anyone please help?

Let's say you run the script with the current directory being /some/where , and with the arguments -u and d1 . I'll also assume that your home directory is /home/ninini . Let's look at where your script looks for files.

 DIR_trash="trash" if [ -f $DIR_trash/$2.zip ]

You check if /some/where/trash/d1.zip exists.

 cd cd $DIR_trash

Assuming both cd commands succeed, the current directory is now /home/ninini/trash .

 mv -i /$DIR_trash/$2.zip .

You're saying to move /trash/d1.zip to the current directory, which is /home/ninini/trash .

Neither the source nor the destination make sense. The source /$DIR_trash doesn't make sense: why would you be looking for a directory called trash under the root directory? And the destination doesn't make sense since you just attempted to change to the trash directory, and now you're attempting to move a file out of the trash directory… into the trash directory.

I can't tell what the correct code is because you didn't say what the script is meant to do. You do say that you want to “to move a file to current directory”; then you must not change the current directory midway through the script! Assuming that the path $DIR_trash/$2.zip from the test command is the correct one, remove the cd commands and write

             mv -i -- "$DIR_trash/$2.zip" .

Note that this moves the file from a directory called trash under the current directory. If this isn't what you wanted, you need to change the definition of DIR_trash . It should probably be an absolute path, perhaps

DIR_trash=~/trash

Note also that your script breaks on files containing whitespace and other special characters. Always put double quotes around variable substitutions: "$VAR" , not $VAR . (Exception: when you know you need some effect that the double quotes prevent, and you understand why it's safe to leave them out.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM