13

I'm moving some files around, and I want to make sure that the mv command I've typed is correct before I go ahead and run it.

If I was using apt-get, I could use the -s flag to perform a simulation that would actually do anything.

Does mv have a similar function, that would simulate the moving of the files but not actually do anything?

  • 2
    In man mv I can see only the -i-flag, which asks before overwriting – Kev Inski Jun 28 '16 at 8:51
  • 2
    mv command doesn't have simulate thing , but I can write a function that does a check like that. How's that solution ? – Sergiy Kolodyazhnyy Jun 28 '16 at 8:53
  • 2
    And what do you expect the simulation to look like? SImply printing one line per modified file, telling e.g. "Renamed a.txt to b,txt" or "Moved /home/you/a.txt to /home/you/Documents/a.txt"? – Byte Commander Jun 28 '16 at 8:53
  • @ByteCommander Something like that, yeah. I'm just paranoid about making a mistake when moving my private ssl certificates around to organise them. – starbeamrainbowlabs Jun 28 '16 at 9:06
  • 4
    The -n option to mv will ensure you can't overwrite any files by mistake, not an answer but always useful to know. – Arronical Jun 28 '16 at 9:26
2

This script should do the trick. It can handle multiple source files/directories, too. Use it the same way you would use mv - mvsim source... dest. Note that it does not pay attention to options, nor does it filter them out (it just treats them as filenames) and it might not work well with symlinks.

#!/bin/bash

if [ $# -lt 2 ]; then
    echo "Too few arguments given; at least 2 arguments are needed."
    exit 1
fi

lastArg="${@:$#}"

i=1
for param in "$@"; do
    if [ ! -e "$param" -a $i -lt $# ]; then
        echo "Error: $param does not exist."
        exit 1
    elif [ "$param" = "$lastArg" -a $i -lt $# ]; then
        echo "Error: $param is the same file/directory as the destination."
        exit 1
    fi
    ((i++))
done

if [ $# -eq 2 ]; then # special case for 2 arguments to make output look better
    if [ -d "$1" ]; then
        if [ -d "$2" ]; then
            echo "Moves directory $1 (and anything inside it) into directory $2"
            exit 0
        elif [ ! -e "$2" ]; then
            echo "Renames directory $1 to $2"
            exit 0
        else
            echo "Error: $2 is not a directory; mv cannot overwrite a non-directory with a directory."
            exit 1
        fi
    else
        if [ -d "$2" ]; then
            echo "Moves file $1 into directory $2"
        elif [ -e "$2" ]; then
            echo "Renames file $1 to $2, replacing file $2"
        else
            echo "Renames file $1 to $2"
        fi
        exit 0
    fi
elif [ ! -e "$lastArg" ]; then
    echo "Error: $lastArg does not exist."
    exit 1
elif [ ! -d "$lastArg" ]; then
    echo "Error: $lastArg is not a directory; mv cannot merge multiple files into one."
    exit 1
fi

argsLeft=$#
echo "Moves..."
for param in  "$@"; do
    if [ $argsLeft -eq 1 ]; then
        echo "...Into the directory $param" # has to be a directory because -f $lastArg was dealt with earlier
        exit 0
    fi
    if [ -d "$param" ]; then
        if [ ! -d "$lastArg" ]; then
            echo "Error: $lastArg is not a directory; mv cannot overwrite a non-directory with a directory."
            exit 1
        fi
        if [ $argsLeft -eq $# ]; then
            echo "The directory ${param} (and anything inside it)..."
        else
            echo "And the directory ${param} (and anything inside it)..."
        fi
    else
        if [ $argsLeft -eq $# ]; then
            echo "The file ${param}..."
        else
            echo "And the file ${param}..."
        fi
    fi
    ((argsLeft--))
done

Some examples:

$ ls
dir1  dir2  file1  file2  file3  mvsim
$ ./mvsim file1 file2
Renames file file1 to file2, replacing file file2
$ ./mvsim file1 newfile
Renames file file1 to newfile
$ ./mvsim file1 dir1
Moves file file1 into the directory dir1
$ ./mvsim file1 file2 file3 dir1
Moves...
The file file1...
And the file file2...
And the file file3...
...Into the directory dir1
$ ./mvsim file1 file2 dir1 dir2
Moves...
The file file1...
And the file file2...
And the directory dir1 (and anything inside it)...
...Into the directory dir2
$ ./mvsim file1 file2 file3 # error - file3 isn't a directory
Error: file3 is not a directory; mv cannot merge multiple files into one.
$ ./mvsim -f -i file1 dir1 # options aren't parsed or filtered out
Error: -f does not exist.
  • Thanks! I'm accepting this as the answer over the script that Serg wrote because it covers more than 2 arguments. maybe looks good too, but I feel that this is the safer option at this time. – starbeamrainbowlabs Jul 1 '16 at 8:35
10

Function below is for verbosely checking mv syntax. Note , that it only works for 2 arguments, SOURCE and DESTINATION, and doesn't check for -t flag.

The function is to be placed into ~/.bashrc . To use it immediately , open new terminal or run source ~/.bashrc

mv_check()
{
    # Function for checking syntax of mv command 
    # sort of verbose dry run
    # NOTE !!! this doesn't support the -t flag
    # maybe it will in future (?)

    # check number of arguments  
    if [ $# -ne 2   ]; then
        echo "<<< ERROR: must have 2 arguments , but $# given "
        return 1
    fi

    # check if source item exist
    if ! readlink -e "$1" > /dev/null 
    then
        echo "<<< ERROR: " "$item" " doesn't exist"
        return 1
    fi

    # check where file goes

    if [ -d "$2"  ]
    then
        echo "Moving " "$1" " into " "$2" " directory"
    else
        echo "Renaming "  "$1" " to " "$2" 
    fi

}

Here's some test runs:

$> mv_check  TEST_FILE1  bin/python                                                                                      
Moving  TEST_FILE1  into  bin/python  directory
$> mv_check  TEST_FILE1  TEST_FILE2                                                                                      
Renaming  TEST_FILE1  to  TEST_FILE2
$> mv_check  TEST_FILE1  TEST_FILE 2                                                                                     
<<< ERROR: must have 2 arguments , but 3 given 
$> mv_check  TEST_FILE1  TEST_FILE\ 2                                                                                    
Renaming  TEST_FILE1  to  TEST_FILE 2
$> mv_check  TEST_FILE1  "TEST_FILE 2"                                                                                   
Renaming  TEST_FILE1  to  TEST_FILE 2
$> mv_check  TEST_FILE1                                                                                                  
<<< ERROR: must have 2 arguments , but 1 given 
  • 1
    You should add a y/n to go ahead and call the actual mv. ;) – chaskes Jun 28 '16 at 15:59
6

There is a programm on github called maybe which may be what you are looking for.

According to their project description, maybe

... allows you to run a command and see what it does to your files without actually doing it! After reviewing the operations listed, you can then decide whether you really want these things to happen or not.

So it will also show you what other programs will do to your files, not only mv.

maybe needs Python to run, but that should not be a problem. It is easy to install or build it using Python's package manager pip.

The installation process and the usage of the program are both described on the project's homepage. Unfortunatly I have no access to a Linux System at the moment, so I can not provide you with any examples on the program's usage.

  • How good it all sounds, as per the documentation: "never use maybe to run untrusted code"! – grooveplex Jun 28 '16 at 15:02
  • @grooveplex Don't you trust the implementation of mv on your system? – maddin45 Jun 28 '16 at 15:12
  • yes, I do, but it was meant as an heads-up – grooveplex Jun 28 '16 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.