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

| improve this question | | | | |

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"
    local TMPFILE=$(mktemp)
    mv "$1" $TMPFILE
    mv "$2" "$1"
    mv $TMPFILE "$2"

either logout/login or run:

source .bashrc

To swap two files run:

swap x y

where x and y are your file-names


| improve this answer | | | | |
  • I'd put && between those three mv commands, otherwise there's a risk of destroying one of the files if you run out of space. On a side note, I'd lowercase TMPFILE to tmpfile; it's not an environment variable. – geirha Apr 8 '12 at 21:31
  • How do I undo the action? – AGamePlayer Feb 11 '14 at 15:59
  • @AwQiruiGuo - just do the reverse - edit .bashrc and remove the function then rerun source or logout/login. – fossfreedom Feb 11 '14 at 16:04
  • @fossfreedom Thanks. Actually I noticed that if I restart the termnial, the swap command would be gone. – AGamePlayer Feb 11 '14 at 16:09

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.

| improve this answer | | | | |
  • why? I was actually thinking of using the bash function solution – brillout Oct 21 '11 at 0:11
  • 3
    Shell scripting is a huge part of unix systems. You learn far more by learning to write scripts than those types of solutions. Shell scripting and Perl are the two things you should really be required to learn if you're serious about unix. tldp.org/LDP/Bash-Beginners-Guide/html – Brandon Weaver Oct 21 '11 at 0:58

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 ]
    echo "Usage: `basename $0` file1 file2."

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.

| improve this answer | | | | |
  • You forgot to quote the variable expansions. – geirha Apr 8 '12 at 21:23

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:

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

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 ???).

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

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.

| improve this answer | | | | |
  • You forgot to quote the variable expansions. Also, don't append to PATH in ~/.bashrc. ~/.profile already appends "$HOME/bin" to PATH. – geirha Apr 8 '12 at 21:24

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.