LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   points about the 'mv' command (https://www.linuxquestions.org/questions/linux-newbie-8/points-about-the-mv-command-889717/)

AndrewJS 07-03-2011 03:43 PM

points about the 'mv' command
 
I want to copy a file from an old directory to a new directory and ideally I would like that the 'mv' command itself could create the directory for me in one step...
That is to say that the directory would not exist until 'mv' has been called as in:

Code:

mv olddirectory/file.dat newdirectory/file.dat
so mv would create the directory 'newdirectory' in the above call....

I am not sure if this is possible however, that is to say I think it may be necessary that the directory 'newdirectory' already exists before I make the above call to mv, as when I make try the above call when 'newdirectory' doesn't exist I get the following error:
Quote:

mv: cannot move `test1/4215/4215-001R.fit' to `test2/test.fit': No such file or directory
so to sum up, my question is:
is there some paramater for 'mv' that I can change so that it will create the new directory as well as copy the file and if not, are there other commands that might do this?

markush 07-03-2011 04:04 PM

Hello AndrewJS,

mv doesn't rename directories! (Edit: it can rename but not rename and move simultaneously)

I'd recommend for such often used commands like mv, read the manpage.
Code:

man mv
mv moves files or whole direcory-trees to another place in your filesystem. If you want to move your file.dat to a new directory, this directory has to be created before moving the file. Read the manpage for the mkdir command, this command has some interesting options which will help you (together with the mv) command to do what you've described.

Markus

theNbomr 07-03-2011 06:08 PM

You can use mv to rename directories, but only if they are the most specific part of the filespec.

Perhaps this bit of shell script provides somewhat of a solution, as well as an illumination of the problem that mv simply avoids (this has undergone only cursory testing. If you use it for real, test thoroughly, and then remove the 'echo' commands to 'go live')
Code:

#! /bin/bash
#
#        mv4sure.sh
#
#        Use me thusly: 
#
#                $ source mv4sure.sh
#                $ alias mv mv4sure
#                $ mv yadda/yadda/horty /blah/blah/florty


function mv4sure(){
mvSrc=$1
mvDest=$2

srcType="UNDEFINED"
destType="UNDEFINED"

        if [ -f $mvSrc ]; then

                echo "Source $mvSrc is a file"
                srcType="FILE"

        elif [ -d $mvSrc ]; then

                echo "Source $mvSrc is a dir"
                srcType="DIR"
        else
                echo "No such source"
                exit 1
        fi


        if [ -f $mvDest ]; then

                echo "Dest $mvDest is a file"
                destType="FILE"

        elif [ -d $mvDest ]; then

                echo "Dest $mvDest is a dir"
                destType="DIR"
        else
                echo "Dest $mvDest doesn't exist. This will get confusing."

        fi



        if  [ $srcType == 'FILE' -a $destType == 'FILE' ]; then
                    echo mv $mvSrc $mvDest

        elif [ $srcType == 'FILE' -a $destType == 'DIR' ]; then
                    echo mv $mvSrc $mvDest

        elif [ $srcType == 'DIR' -a $destType == 'DIR' ]; then
                    echo mv $mvSrc $mvDest

        elif [ $srcType == 'DIR' -a $destType == 'FILE' ]; then
                    echo "Huh? You can't move a DIR to a FILE"

        elif [ $destType == 'UNDEFINED' ]; then
                if [ $srcType == 'DIR' ]; then
                        #
                        #  Assume a non-existing destination is a directory....
                        #
                        echo mkdir -p $mvDest
                        echo mv $mvSrc $mvDest
                elif [ $srcType == 'FILE' ]; then
                        #
                        #  But, if the source is a FILE, then should
                        #  we make the full directory and move the file there,
                        #  or does the destination specify a directory
                        #  component and a filename component?
                        #
                        echo "I really don't know what to do here...."
                       
                        destDir=${mvDest%/*}
                        srcFile=${mvSrc##*/}
                        destFile=${mvDest##*/}
                        echo "I could make $destDir and move the "
                        echo "file '$srcFile' there with the name '$destFile'"
                        echo "or I could make $mvDest as a directory, and move the file there"
                fi
        fi
}

--- rod.

Soadyheid 07-03-2011 06:20 PM

As far as I know,
Code:

mkdir
is the command to make a new directory. Then you can use
Code:

mv
to move a file from it's original directory to the one you've just made.

You could probably write a script to allow you to have one command to create the directory and move the file, you'd still have to supply the new directory and file names, but if you're only doing it once, why bother?

Play Bonny! :hattip:


All times are GMT -5. The time now is 03:06 PM.