Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: cp and mv command - nuanced instructions

  1. #1
    Join Date
    Aug 2011
    Beans
    26

    cp and mv command - nuanced instructions

    Hello and thank you for reading this.

    I have consulted several resources about the cp command, such as: http://www.computerhope.com/unix/ucp.htm. I still can't find my answer.

    What I would like to do is to move files from one folder to another, replacing the existing contents ONLY IF the time signatures are identical. Otherwise I would like to move the file but to keep both copies (name.file and name.file(2) ..or something equivalent). Any suggestions for how I might accomplish this?

    Thanks again!

  2. #2
    Join Date
    Feb 2014
    Beans
    7

    Re: cp and mv command - nuanced instructions

    Quote Originally Posted by aaron.kyle View Post
    Hello and thank you for reading this.

    I have consulted several resources about the cp command, such as: http://www.computerhope.com/unix/ucp.htm. I still can't find my answer.

    What I would like to do is to move files from one folder to another, replacing the existing contents ONLY IF the time signatures are identical. Otherwise I would like to move the file but to keep both copies (name.file and name.file(2) ..or something equivalent). Any suggestions for how I might accomplish this?

    Thanks again!
    So just to clarify, you want to overwrite the files with the same time signature but keep both copies if they are not identically timed? I know you can do the later, im not sure about the first. I will check on this now and get back to you shortly. Please correct me if I'm misunderstanding something thought!

    ~Zeus

  3. #3
    Join Date
    Feb 2014
    Beans
    7

    Re: cp and mv command - nuanced instructions

    Quote Originally Posted by aaron.kyle View Post
    Hello and thank you for reading this.

    I have consulted several resources about the cp command, such as: http://www.computerhope.com/unix/ucp.htm. I still can't find my answer.

    What I would like to do is to move files from one folder to another, replacing the existing contents ONLY IF the time signatures are identical. Otherwise I would like to move the file but to keep both copies (name.file and name.file(2) ..or something equivalent). Any suggestions for how I might accomplish this?

    Thanks again!
    From what I could try/find/etc the best method, which I'm sure isn't the best since I'm still learning myself, would be to navigate to the directory you want and do ls -l to view a long version list of the contents.
    Then run:
    cp -a -i -u directoryOfFilesToMove/* directoryFilesAreMovedTo

    example: if i wanted to move all files from StupidTest to StupidTestDir I would use:

    cp -a -i -u StupidTest/* StupidTestDir

    This copies all file attributes (incl ownerships and permissions) to the new directory (-a archive), prompts for overwrites if there are files with the same name (-i interactive), and copies only the files that either don't exist or are newer than the existing files in the destination directory (-u update).
    Using -u kind of defeats the purpose of using -i or even viewing the contents of the directory; however, if you wanted to manually check against the update you could (kind of unnecessary though).

    To answer the question regarding overwriting files if the timestamps match: I added the -i in there that way you can control the files you didn't want to overwrite, which is also why i suggested using ls -l from within the directories you are transferring files to and from to see their time stamps. Thought I'm sure another user more adept in shell-foo will have a more streamlined approach.

    Please let me know if this answers your question or if I further confused things. Hopefully it helped.

    ~Zeus


    **edit: added the example I intended to use the first time I posted
    **edit#2: added what I actually edited (sorry still kind of new to helping others)
    ***edit#3: added the "answer" to his question
    Last edited by fromite; February 17th, 2014 at 11:23 PM.

  4. #4
    Join Date
    Jun 2013
    Location
    Somewhere on Earth.
    Beans
    468
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: cp and mv command - nuanced instructions

    If by time signatures mean the time it was created, then these two scripts will work.

    This can be done via the test/[ command to determine whether or not to replace the file. (The command can be called 'test' or '[' <--that's a bracket)

    Open the text editor and put in the following (if you want a text interface):

    Quote Originally Posted by Jonathan Precise
    Code:
    #! /bin/bash
    # Uses the cp and mv commands
    
    #clear # Clear the console window (disabled)
    echo "" 1>&2 # Blank line (echoed to stderr)
    echo -n "Where is the original file located? " # Text saying "Where is the original file located? "
    read TPOTOF # Read from stdin where the path of the original file is, and save the path to '$TPOTOF'
    echo "" # Blank line
    echo -n "Where is the newdirectory located? " # Text saying "Where is the original file located? "
    read TPOTNF # Read from stdin where the path of the original file is, and save the path to '$TPOTOF'
    if [ -z $TPOTNF ] || [ -z $TPOTOF ] # If one of the values are [null], then
    then
        echo "E:One or more of the values you have entered are empty" # State the error
        exit 1 # Exit with error code 1
    elif [ ! -f $TPOTOF ] || [ ! -d $TPOTNF ] #Or if one of the files do not exist, then
    then
        echo "E:One or more of the files you specified do not exist." # State the error
        exit 2 # Exit with error code 2
    fi
    TPOTNF="$TPOTNF/$TPOTOF"
    if [ -f $TPOTNF ]
    then
        if test $TPOTOF -nt $TPOTNF || test $TPOTOF -ot $TPOTNF
        then
            echo "Time stamps are different. Will not overwrite file."
            cp $TPOTOF `echo "new-"$TPOTNF`
        else
            echo -n "W: Time stamps are the same. Are you sure you want to overwrite? This can not be undone. [y/N]: "
            read YesNo
            case $YesNo in
                y|Y|yes|Yes|yEs|yeS|YEs|YeS|yES|YES)
                    echo "Ok. Doing as you wished."; sleep 1
                    rm -rf $TPOTNF ;
                    cp $TPOTOF $TPOTNF ;
                    echo 'Done!'
                    ;;
                n|N|no|No|nO|NO|*)
                    echo "Ok. Will not replace file.";
                    ;;
            esac
        fi
    else
        echo "The file does not exist in the remote directory, so it will just be copied."
        cp $TPOTOF $TPOTNF
    fi
    echo "Will now exit" ; sleep 3
    exit 0
    Or for a graphical interface:

    Quote Originally Posted by Jonathan Precise
    Code:
    #! /bin/bash
    # Uses the cp and mv commands
    
    #clear # Clear the console window (disabled)
    TPOTOF=`zenity --entry --title="Made with zenity" --text="Where is the original file located? "`
    TPOTNF=`zenity --entry --title="Made with zenity" --text="Where is the newdirectory located? "`
    if [ -z $TPOTNF ] || [ -z $TPOTOF ]
    then
        zenity --error --title="Error | Made with zenity" --text="E:One or more of the values you have entered are empty" --no-wrap
        exit 1
    elif [ ! -f $TPOTOF ] || [ ! -d $TPOTNF ] #Or if one of the files do not exist, then
    then
        zenity --error --title="Error | Made with zenity" --text="E:One or more of the files you specified do not exist." --no-wrap
        exit 2
    fi
    TPOTNF="$TPOTNF/$TPOTOF"
    if [ -f $TPOTNF ]
    then
        if test $TPOTOF -nt $TPOTNF || test $TPOTOF -ot $TPOTNF
        then
            zenity --info --title="Made with zenity" --text="Time stamps are different. Will not overwrite file."
            cp $TPOTOF `echo "new-"$TPOTNF`
        else
            zenity --question --title="Warning | Made with zenity" "W: Time stamps are the same. Are you sure you want to overwrite? This can not be undone."
            if [ $? != 0 ]
            then
                zenity --info --title="Made with zenity" --text="Ok. Doing as you wished."; sleep 1
                rm -rf $TPOTNF ;
                cp $TPOTOF $TPOTNF ;
                zenity --info --title="Mission Complete\! | Made with zenity" --text='Done!'
            else
                zenity --info --title="Made with zenity" --text="Ok. Will not replace file.";
            fi
        fi
    else
        zenity --info --title="Made with zenity" --text="The file does not exist in the remote directory, so it will just be copied."
        cp $TPOTOF $TPOTNF
    fi
    echo "Will now exit" ; sleep 3
    exit 0
    Then make the saved file executable, and execute the script.

    Hope it's of help.
    Avatar credit: http://www.omgubuntu.co.uk/2010/07/d...-spread-ubuntu
    Note: I shall not respond to any PMs/threads for the rest of this 2014, as I will be very busy. Sorry for any inconvenience

  5. #5
    Join Date
    Feb 2014
    Beans
    7

    Re: cp and mv command - nuanced instructions

    And that, ladies and gentlemen, is script-foo. Definitely made me appear as the noob I am

    Well done!

    P.s. What languange is that? Seems like a short-hand version of C with nix commands thrown in

  6. #6
    Join Date
    Jun 2013
    Location
    Somewhere on Earth.
    Beans
    468
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: cp and mv command - nuanced instructions

    Quote Originally Posted by fromite View Post
    What languange is that? Seems like a short-hand version of C with nix commands thrown in
    It's using the BASH shell (Bourne Again SHell)
    Avatar credit: http://www.omgubuntu.co.uk/2010/07/d...-spread-ubuntu
    Note: I shall not respond to any PMs/threads for the rest of this 2014, as I will be very busy. Sorry for any inconvenience

  7. #7
    Join Date
    Feb 2014
    Beans
    7

    Re: cp and mv command - nuanced instructions

    Quote Originally Posted by Jonathan Precise View Post
    It's using the BASH shell (Bourne Again SHell)
    Know of any good educational references?

  8. #8
    Join Date
    Sep 2010
    Beans
    898

    Re: cp and mv command - nuanced instructions

    Quote Originally Posted by fromite View Post
    Know of any good educational references?
    http://manpages.ubuntu.com/manpages/...n1/bash.1.html
    http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

  9. #9
    Join Date
    Feb 2014
    Location
    North India
    Beans
    15
    Distro
    Ubuntu Development Release

    Re: cp and mv command - nuanced instructions

    Hi, A very good tutorial for Shell Scripts. http://linuxcommand.org/writing_shell_scripts.php

    Scripting is the power of Linux!!!!!!!

    Good Luck.
    Code:
    Doing what you should is Freedom :)
    But Liking what you do is Happiness!!!!

  10. #10
    Join Date
    Feb 2014
    Beans
    7

    Re: cp and mv command - nuanced instructions

    Thanks guys, perciate it!

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •