script to swap two files

one thing that always drove me crazy is the need to do

 mv file1 tmp
 mv file2 file1
 mv tmp file2

in oder to swap 2 filenames.

what do you guys use to swap 2 files? Are there any standard script for swaping? something like sw file1 file2 would be nice


Suggestion would be to add to bash a function that will swap two files:

nano .bashrc

paste in at the bottom of the file:

function swap()         
{
  if [ $# -ne 2 ]; then
    echo "Usage: swap file1 file2"
  else
    local TMPFILE=$(mktemp)
    mv "$1" $TMPFILE
    mv "$2" "$1"
    mv $TMPFILE "$2"
  fi
}

either logout/login or run:

source .bashrc

To swap two files run:

swap x y

where x and y are your file-names

source


I would suggest going with a bash script instead of playing with bash.conf.

If you're getting into Unix systems you'll need to learn shell scripting at some point.


This script does 2 things:

  • checks if you didn't by mistake use wrong number of arguments
  • checks if both arguments are files (don't mistype and use a directory or something).

If it fails any of those 2 checks it will tell you how to run the script, else you will have your files swapped.

#! /bin/sh

if [ ! $# = 2 -o ! -f $1 -o ! -f $2 ]
then
    echo "Usage: `basename $0` file1 file2."
    exit
fi

mv $1 cop_$1
mv $2 $1
mv cop_$1 $2

Save it in your home, use chmod +x <scriptname>.sh to make it executable, move it to your /usr/bin/ with the command sudo cp <scriptname>.sh /usr/bin/.

Now you can use <nameofthescrip> file1 file2 to swap files.


I agree with Bruno, the shell script is more the norm for adding command type functionality. I offer additional suggestions:

1- for the Usage, do not include the period at the end of the message. You are trying to show syntax and the message "Usage: swap file1 file2." is possibly misleading, it is not a sentence and should just say: "Usage: swap file1 file2"

2- Placing the file in /use/bin makes it a system command. While your personal Ubuntu install may tolerate this, when you decided to re-install from scratch you capture your home directory, not /usr/bin. I suggest you create $HOME/bin, add that to your search path, and place all your scripts in that directory. Your bin directory is your personal trove of additional commands.

3- If you place the function in bashrc then it is only available in a bash shell, if you want swap available regardless of shell, then a shell script in a bin directory is more universal and standard.

4- In the script I suggest that the exit after the Usage output be changed to "exit 1" and after the last line (third mv command) add "exit 0". The exit code indicates success or failure. If you ever write other scripts that use swap, they can check the exit code to see if there was success or failure.

5- Instead of cop_$1 which assumes the file cop_ does not exist, I suggest the mktemp function, using the --tmpdir option it creates the temp file in the current directory. This guarantees no conflicting file names.

Putting this all together I offer the following:

#!/bin/sh 
if [ ! $# = 2 ] ; then
    echo "Usage: `basename $0` file1 file2"
    echo "Two file names are required."
    exit 1
fi
if [ ! -f $1 -o ! -f $2 ] ; then
    echo "Usage: `basename $0` file1 file2"
    echo "Only file names are allowed."
    exit 1
fi

TEMP=`mktemp --tmpdir=.`

mv $1 ${TEMP}
mv $2 $1
mv ${TEMP} $2
exit 0

Now put this in the bin directory under your home directory. Make sure your PATH variable picks up this home directory echo $PATH. If it does not, the PATH is set in your shell initialization resources (.bashrc or .cshrc or ???).

cd
mkdir bin
nano swap
< now paste the above script into the file, save, and exit>
chmod 755 swap
nano $HOME/.bashrc
< now add the following 2 lines at the end of the file, save, and exit>
# user specific pathing
PATH="$HOME/bin:$PATH"

Now you either start a new bash shell, or reprocess the .bashrc file:

source .bashrc

New login or shells should be automatic.

Thats it. Test with just a swap command and you should see the usage text.