1

Is there any "super" mv command out there which displays files info (size, date, etc.) when a destination file is to be overwritten with a source one ? This is, when moving files around and two files happen to have the same name.

When this happens I need first to cancel the 'mv' command, 'ls' the destination folder, have a look at the file size, timestamp, etc., then compare it to the source one to see which one I want to keep, then resume the mv operation if I want to overwrite, otherwise just 'rm' the source one.

It would be great to get a similar behaviour as when this happens under a X session or Windows, so a decision can be taken without canceling the ongoing command.

  • 1
    You can write a script yourself to achieve your goal. – Weijun Zhou Mar 11 at 22:38
  • You run mv commands without looking at the target before actually running the command? – Andrew Henle Mar 11 at 22:42
  • Yes, a script can do that. – Weijun Zhou Mar 11 at 22:43
  • If you only care about timestamps and overwrite only if newer, there is mv -u. – Weijun Zhou Mar 11 at 22:59
  • @WeijunZhou: of course it can be done with a script, but sometimes they are already done and there is no need to reinvent the wheel every time – Almendrico Mar 12 at 10:47
0

Use a very simple script wrapper, called for instance smv (for "super-move").

The script accepts exactly 2 arguments, the way the cmd mv would. Make sure that it is executable, e.g. with chmod 751 smv, and ensure that the path corresponding to its location is in yr PATH variable.

#!/usr/bin/bash

if ! [ $# -eq 2 ]; then
    printf "%s\n" "Usage: Include exactly 2 fully qualified file names as arguments, as in:" \
    "   $ smv /path/to/source /path/to/destination" "Abort."
elif [ -e "$2" ]; then
    (printf "\n%s\n\n" "Destination file exists."  && /usr/bin/stat "$2" ) 2>&1
    printf "\n" 
    read -p "Clobber it ? (Y/n):  " answer
    case "$answer" in
        N|n) echo "$1 not moved; $2 preserved. Abort.";  exit(11) ;;
        Y|y) \mv -f "$1" "$2" ;;
    esac
else
    printf "\n%s\n" "Destination file does not exist."
    \mv -i "$1" "$2"
    printf "%s\n"  "$1 was moved to $2."
fi

HTH

  • Thanks, serves as a start to make a better display of stat command and get what I want ! – Almendrico Mar 28 at 18:14
0

The -i or --interactive option to mv makes it prompt before overwriting. You can then easily copy the filename to another terminal and look at its metadata there.

  • I have that aliased already to my mv, i.e. "alias mv='mv -i' ". I should have mentioned this in my question – Almendrico Mar 12 at 10:46

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.