Linux equivalent to robocopy?
It’s kind of funny, there are a lot of ways in which Linux command line tools are vastly superior to Windows ones, but one thing I have not found an equivalent to is robocopy. Robocopy is way more versatile than cp
, and I can’t figure out how to do what I want with Linux tools.
A specific use case is that I have two directories with mostly similar files, but one has newer source files and one has content files that the other directory doesn’t have, and possibly newer source files (for a website). I want to copy the files from the latter directory into the former, adding new content files and such, but not overwriting newer files in the destination directory.
I tried to figure out how to do this in Linux for maybe half an hour, decided that I’d probably have to learn bash scripting or something to do what I wanted, and then I realized that I could just use robocopy. I also had some other requirements. I was basically converting a Python (Django) website from an SVN repository to a Git repository, and I wanted to make sure I didn’t have uncommitted changes in SVN. However, I wanted to conditionally exclude the .svn
directories and the .pyc
compiled Python files. The following robocopy command does exactly what I want:
robocopy source destination /XO /E /XD .svn /XF *.pyc
Is there any equivalent to Robocopy in Linux? I looked into rsync
briefly, but it seemed like I’d have to set up an rsync server before attempting to sync the folders.
It sounds like rsync is definitely what you are after. You do not need to set up an 'rsync server' to copy files from one machine to another. Rsync supports copying files over SFTP (SSH File Transfer) which most linux boxes have enabled already (if not manually disabled).
See Lifehacker's Mirror files across systems with rsync for more details:
Whether you want to backup your data, distribute files securely or mirror your working documents over the internet from the office to home, between computers on your local network, or from your computer to your web server, rsync can get the job done. Today we'll use rsync to mirror folders between a Mac and PC over a secure connection at the command line.
Rsync is free (as in speech) and cross platform, meaning it syncs files between operating systems (Windows/Cygwin, Mac OS, Linux); it works over ssh so it's encrypted and secure; unlike FTP it's incremental, so only the parts of changed files are transferred, not whole files, which makes it go like Speedy Gonzalez; and the fact that it's command line makes it scriptable and easily automated.
rsync will copy files from one directory to another directory on the same machine like robocopy. Here is the rsync command that is equivalent to your robocopy command:
rsync -auv --exclude '.svn' --exclude '*.pyc' source destination
This will recursively copy the source to the destination and exclude older files as well as '.svn' and '*.pyc' directories/files.