Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Quick Copy To and Move To scripts

  1. #1
    Join Date
    Nov 2005
    Beans
    729
    Distro
    Ubuntu 6.06

    Cool

    This how-to will show you how to have a fast way to copy or move a file from one directory to another in nautilus. I wrote these scripts because I had a "files" directory with 20 files in it and I needed each of the 20 files to go to 20 different locations. Rather than right-click a file, choose cut, surf to the target directory, right-click the, choose paste, go back to the source directory and repeat all of that for the remaining 19 files, I wrote some scripts that perform these steps much faster.

    Nautilus script explanation
    A nautilus script is just an executable shell script (usually bash) that is placed in a special scripts directory so that the Nautilus graphical shell can find it. This is a really neat function of Nautilus, because it allows you to extend the functionality the the file browser to do just about anything. Scripts are invoked by selecting a file or group of files, and right-clicking with the mouse, to bring up a 'Context' menu. One of the options of this menu is the 'Scripts' submenu (this option only appears once you have at least one script in the scripts directory), which allows you to select a script to invoke on the selected files.

    For a script to appear on the script menu, it must:
    1. be placed in your scripts directory (~/.gnome2/nautilus-scripts), and
    2. be executable (with chmod a+x filename)

    If you place an executable script in your scripts directory, its name will not necessarily appear on the scripts menu immediately. You first must visit the scripts directory with Nautilus - which can be done using the last option in the scripts menu. Once the directory is visited, Nautilus will know about which scripts you have, and you will be able to use them.

    Now, on to the Copy To and Move To scripts.

    The Copy To script:
    Place the following code in a new file, give it a filename of copyto and make it executable with chmod a+x copyto. Move the new file to the ~/.gnome2/nautilus-scripts/ directory, open nautilus and you'll find a new right-click menu item entitled "Scripts" with a submenu that has an item that quickly copies a file to any directory you desire.

    Code:
    #! /bin/bash
    location=`zenity --file-selection --directory --title="Select a directory"`
    for arg
    do
    if [ -e $location/$arg ];then
       zenity --question --title="Conflict While Copying" --text="File "$location/$arg" already exists. Would you like to replace it?"
       case "$?" in
          1  )  exit 1 ;;
          0  )  cp $arg $location ;;
       esac
    else
       cp $arg $location
    fi
    done
    The Move To script:
    Place the following code in a new file, give it a filename of moveto and make it executable with chmod a+x moveto. Move the new file to the ~/.gnome2/nautilus-scripts/ directory, open nautilus and you'll find a new right-click menu item entitled "Scripts" with a submenu that has an item that quickly moves a file to any directory you desire.

    Code:
    #! /bin/bash
    location=`zenity --file-selection --directory --title="Select a directory"`
    for arg
    do
    if [ -e $location/$arg ];then
       zenity --question --title="Conflict While Moving" --text="File "$location/$arg" already exists. Would you like to replace it?"
       case "$?" in
          1  )  exit 1 ;;
          0  )  mv $arg $location ;;
       esac
    else
       mv $arg $location
    fi
    done
    As you can see, nautilus scripts allow you to extend nautilus to do almost anything. I have put my entire Gnome Applications menu in my nautilus/desktop right-click menu so I don't even use my gnome menu much anymore.

    For instance, you can put the below code into a new text file, give it a name of terminal or something like that, make it executable and move it to ~/.gnome2/nautilus-scripts/Terminals or ~/.gnome2/nautilus-scripts/Apps or whatever suits you, and you'll have a right-click menu item to open gnome-terminal. This can be done with any app, whether you need to use sudo or not.

    Code:
    #!/bin/sh
    gnome-terminal
    Basically, the "gnome-terminal" part of the above code is simply the cli command to start gnome terminal. You can use the gnome menu editor to find the proper commands for all the apps in the Applications menu.

    hope this how-to is of help to the community

    More information, scripts and tutorials for nautilus scripts can be found here.

  2. #2
    Join Date
    Oct 2005
    Location
    Aldan, PA
    Beans
    552

    Re: Quick Copy To and Move To scripts

    Apparently most people read only the Desktop support forums. The same thread is a hit there!
    Photography by Indians
    I'm registered Linux User # 406032

  3. #3
    Join Date
    May 2005
    Location
    London
    Beans
    50

    Re: Quick Copy To and Move To scripts

    This is a great idea! I changed your code in order to handle files with space and also made it recursive and made it to show a progress bar while it's copying. The same can be implemented for "Move to" script I suppose.

    Here is the code:
    Code:
    #! /bin/bash
    location=`zenity --file-selection --directory --title="Select a directory"`
    for arg
    do
    if [ -e "$location/$arg" ]; then
       zenity --question --title="Conflict While Copying" --text="File $location/$arg already exists. Would you like to replace it?"
       if [ "$?" = 1 ]; then
            exit 1
       fi
    fi
       cp -R "$arg" "$location" &
       ORIG_SIZE=`du -bs "$arg"|awk '{print $1}'`
       CP_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
    
       (
            while [ $CP_SIZE -ne $ORIG_SIZE ]; do
            expr `expr $CP_SIZE \* 100` / $ORIG_SIZE
            CP_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
            done
            echo 100
       )| zenity --progress --auto-close --text "Copying \"$arg\"..."
    
    done

  4. #4
    Join Date
    Nov 2005
    Beans
    729
    Distro
    Ubuntu 6.06

    Re: Quick Copy To and Move To scripts

    Quote Originally Posted by moment
    This is a great idea! I changed your code in order to handle files with space and also made it recursive and made it to show a progress bar while it's copying. The same can be implemented for "Move to" script I suppose.

    Here is the code:
    Code:
    #! /bin/bash
    location=`zenity --file-selection --directory --title="Select a directory"`
    for arg
    do
    if [ -e "$location/$arg" ]; then
       zenity --question --title="Conflict While Copying" --text="File $location/$arg already exists. Would you like to replace it?"
       if [ "$?" = 1 ]; then
            exit 1
       fi
    fi
       cp -R "$arg" "$location" &
       ORIG_SIZE=`du -bs "$arg"|awk '{print $1}'`
       CP_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
    
       (
            while [ $CP_SIZE -ne $ORIG_SIZE ]; do
            expr `expr $CP_SIZE \* 100` / $ORIG_SIZE
            CP_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
            done
            echo 100
       )| zenity --progress --auto-close --text "Copying \"$arg\"..."
    
    done
    Wow, thank you very much

  5. #5
    Join Date
    Aug 2005
    Beans
    202

    Re: Quick Copy To and Move To scripts

    Awesome! These are very useful scripts. These should be implemented in future versions of nautilus by default--imo.

  6. #6
    Join Date
    Aug 2006
    Location
    Canberra, Australia
    Beans
    89
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Quick Copy To and Move To scripts

    Quote Originally Posted by moment View Post
    This is a great idea! I changed your code in order to handle files with space and also made it recursive and made it to show a progress bar while it's copying. The same can be implemented for "Move to" script I suppose.

    Here is the code:
    Code:
    #! /bin/bash
    location=`zenity --file-selection --directory --title="Select a directory"`
    for arg
    do
    if [ -e "$location/$arg" ]; then
       zenity --question --title="Conflict While Copying" --text="File $location/$arg already exists. Would you like to replace it?"
       if [ "$?" = 1 ]; then
            exit 1
       fi
    fi
       cp -R "$arg" "$location" &
       ORIG_SIZE=`du -bs "$arg"|awk '{print $1}'`
       CP_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
    
       (
            while [ $CP_SIZE -ne $ORIG_SIZE ]; do
            expr `expr $CP_SIZE \* 100` / $ORIG_SIZE
            CP_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
            done
            echo 100
       )| zenity --progress --auto-close --text "Copying \"$arg\"..."
    
    done
    I just found these scripts today (2 years after you posted it). I wrote a script myself some months ago to achieve this but mine was rather more long-winded (amateur programmer!). My version displayed the date, time, size of the files to be replaced to help the user make an informed decision, but that's easy to add to the script above.

    So I thought I'd try the script above and I feel there is one error in the code. If you select multiple files and are prompted whether or not to over-write an existing file, pressing "Cancel" exits the entire operation. I think it should just skip that one file and move on to the next. You can make the latter happen by replacing the "exit 1" with "continue"

    Of course, if you were copying 20 files and they all existed at the destination already, you would be prompted 20 times about whether or not to overwrite. It would be nice if the user could be given the choice to "Overwrite, Overwrite All, Skip, Skip All" or similar but right now I'm not sure if zenity dialogs have that capability.

    cheers
    Tony
    --
    Linux Mint 14, Linux user 456436

  7. #7
    Join Date
    May 2007
    Beans
    46
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Quick Copy To and Move To scripts

    These are absolutely fantastic, I agree they should be there from the word go. Not knowing zenity at all, how would the progress section be changed to display moving progress?

  8. #8
    Join Date
    Dec 2006
    Location
    Cali
    Beans
    11
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Quick Copy To and Move To scripts

    Thanks moment, I was looking for something like this for organizing a bunch of my pictures. I wanted to move files I selected to a folder that did not exist. I borrowed your code and modified it to prompt for a new folder name.

    Code:
    #! /bin/bash
    # Create a folder and move files to that folder
    location=`zenity --entry --text "Enter folder name" --entry-text "New Folder"`
    mkdir $location
    for arg
    do
    if [ -e "$location/$arg" ]; then
    zenity --question --title="Conflict While Copying" --text="File $location/$arg already exists. Would you like to replace it?"
    if [ "$?" = 1 ]; then
    exit 1
    fi
    fi
    mv -f "$arg" "$location" &
    ORIG_SIZE=`du -bs "$arg"|awk '{print $1}'`
    MV_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
    
    (
    while [ $MV_SIZE -ne $ORIG_SIZE ]; do
    expr `expr $MV_SIZE \* 100` / $ORIG_SIZE
    MV_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
    done
    echo 100
    )| zenity --progress --auto-close --text "Copying \"$arg\"..."
    
    done

    I wish I was better at codeing and could make it creat a folder from the filename excluding the numbers on the end.

    ex.
    pic01.jpg
    pic10.jpg
    pic100.jpg

    when all those are selected a script that would make a folder called pic and move the files to it...

  9. #9
    Join Date
    Dec 2010
    Beans
    3

    Re: Quick Copy To and Move To scripts

    In response to the guy that wanted overwrite, skip and cancel, I studied the zenity code and came up with following after testing for about 2 hours to make sure it worked properly. This is for the moveTo script, I implemented this on Maverick Meerkat Ubuntu 10.10.

    Code:
    #! /bin/bash
     location=`zenity --file-selection --directory --title="Select a directory"`
     for arg
     do
    	 if [ -e "$location/$arg" ]; then
    	 	#zenity --question --title="Conflict While Moving" --text="File $location/$arg already exists. Would you like to replace it?"
    		ans=$(zenity --list --title="Conflict While Moving" --text="File $location/$arg already exists. What would you liked to do?" --radiolist --column "Pick" --column "Option" TRUE "Overwrite" FALSE "Skip" FALSE "Cancel")
    	        case "$ans" in
    			"Skip" ) 
    				#zenity --info --title="skip executed" --text="$ans, hopefully that said Skip"
    				zenity --info --title="Skip" --timeout=5 --text="Skipping moving $arg to $location!"
                                    continue;;
    			"Cancel" ) 
    				#zenity --info --title="cancel executed" --text="$ans, hopefully that said Cancel"	
    				zenity --info --title="Cancel" --timeout=5 --text="Canceling entire moving operation!"
    				exit;; #this seems so brutal, is there a better way to do this?
    			# We do not have to test for Overwite as that is default action with 'mv -f' below
    		esac
    	 fi
     	 mv -f "$arg" "$location" &
    	 ORIG_SIZE=`du -bs "$arg"|awk '{print $1}'`
    	 MV_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
    
    	 (
    		 while [ $MV_SIZE -ne $ORIG_SIZE ]; do
    			 expr `expr $CP_SIZE \* 100` / $ORIG_SIZE
    			 MV_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
    		 done
    		 echo 100
    	 )| zenity --progress --auto-close --text "Moving \"$arg\"..."
    
     done
    Last edited by lambroger; April 28th, 2011 at 12:54 AM. Reason: added info dialogs so skipping and canceling didn't feel odd when you performed them

  10. #10
    Join Date
    Dec 2010
    Beans
    3

    Re: Quick Copy To and Move To scripts

    Since coding moveTo was easy to do as far as overwriting and skipping are concerned, I have implemented this for CopyTo as well, along with information dialog boxes for skipping and canceling.

    Code:
    #! /bin/bash
     location=`zenity --file-selection --directory --title="Select a directory"`
     for arg
     do
    	 if [ -e "$location/$arg" ]; then
    		ans=$(zenity --list --title="Conflict While Copying" --text="File $location/$arg already exists. What would you liked to do?" --radiolist --column "Pick" --column "Option" TRUE "Overwrite" FALSE "Skip" FALSE "Cancel")
    	        case "$ans" in
    			"Skip" ) 
    				#zenity --info --title="skip executed" --text="$ans, hopefully that said Skip"
    				zenity --info --title="Skip" --timeout=5 --text="Skipping copying $arg to $location!"
    				continue;;
    			"Cancel" ) 
    				#zenity --info --title="cancel executed" --text="$ans, hopefully that said Cancel"
    				zenity --info --title="Cancel" --timeout=5 --text="Canceling entire operation!"	
    				exit;; #this seems so brutal, is there a better way to do this?
    			# We do not have to test for Overwite as that is default action with 'cp -Rf' below
    		esac
    	 fi
    	 cp -Rf "$arg" "$location" &
    	 ORIG_SIZE=`du -bs "$arg"|awk '{print $1}'`
    	 CP_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
    
    	 (
    		 while [ $CP_SIZE -ne $ORIG_SIZE ]; do
    			 expr `expr $CP_SIZE \* 100` / $ORIG_SIZE
    			 CP_SIZE=`du -bs "$location/$arg"|awk '{print $1}'`
    		 done
    		 echo 100
    	 )| zenity --progress --auto-close --text "Copying \"$arg\"..."
    
     done

Page 1 of 2 12 LastLast

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
  •