Move Files Without Breaking Stuff

Move Files Without Breaking Stuff

Linux or Mac OS X only: The Command-Line Fu web site writes up a quick-and-dirty trick to move files and create a symlink in a single command—so you can move files without breaking anything.

The principle is simple: if you want to move a file or folder to another location without breaking anything, you can simply create a symlink from the original location to the new location. For instance, if you wanted to move SomeFile from the default location to the /other/location/ folder, you would move the file and then use the ln command to create a symlink:

ln -s SomeFile /other/location/SomeFile

The Command-Line Fu site simplifies it down into a single command—just add the following to your bashrc file to create an entirely new command that performs the same steps:

function lmv(){ [ -e $1 -a -e $2 ] && mv $1 $2 && ln -s $2/$(basename $1) $(dirname $1); }

Once you've enabled the new command, you can use it to move a file or folder and symlink at the same time with the following syntax:

lmv SomeFile /another/folder

The only issue with this command is that it's still a little buggy—trying to use it for files with spaces doesn't work, you can't rename the file while moving, and it breaks if you use a trailing slash—if you've got the geek skills to make the command better, or can think of a better solution, we're all ears in the comments.