← Networking and Remote Servers from Zero
Move files and keep jobs running: scp, rsync, sftp, tmux and nohup

Lesson 6 of 6

Move files and keep jobs running: scp, rsync, sftp, tmux and nohup

Copy files to and from a server, sync folders so only the changes travel, browse with sftp, and keep your work running after you disconnect with tmux and nohup.

The idea in plain words

Copying a file to a server is like sending a parcel by courier, which is scp. Syncing is a moving van that only carries what is new or changed, which is rsync. sftp is a conversation with a file clerk where you browse and ask for files.

tmux is a note that says: keep going, I will be back. And nohup tells a job to ignore the hang-up when you log off.

Commands in this lesson

scp report.pdf ava@203.0.113.10:~/uploads/
Copy a file to a server over SSH. Swap the order to download, and add -r for a folder.
rsync -avz ./site/ ava@203.0.113.10:~/www/
Sync a folder so that only new or changed files are sent. A trailing slash means the contents.
sftp ava@203.0.113.10
Open an interactive session where you browse with ls and cd, upload with put and download with get.
tmux new -s build
Start a session that keeps running if you disconnect. Detach with Ctrl+B then D, and return with tmux attach -t build.
nohup ./backup.sh > backup.log 2>&1 &
Run a command in the background that survives you logging out, and save its output to a log file.

Written notes from the video

Why this episode

A server is only useful if you can get files onto it, and keep things running after you log off. This is Networking and Remote Servers from zero, episode six: the last one. Five commands to copy files, sync folders, and keep your work alive.

Real-life analogies

Copying a file to a server is like sending a parcel by courier. That is scp. Syncing is a moving van that only carries what is new or changed. That is rsync. sftp is a conversation with a file clerk, where you browse and ask for files. tmux is a note that says: keep going, I will be back. And nohup tells a job to ignore the hang-up when you log off.

Command 1: scp

Command one: scp. It copies a file over SSH. Give it the file, then the server and the destination. The colon after the address separates the server from the path. Reverse the order to download. Add -r to copy a whole folder. It is simple and available everywhere, but it sends everything again each time.

Command 2: rsync

Command two: rsync. It copies only what has changed, and shows you what it sent. The flags -avz mean archive, verbose and compress. A trailing slash on the source means: copy the contents of the folder. Without it, you copy the folder itself. Run it again, and only the changes travel. That is why it is a favourite for deployments.

Command 3: sftp

Command three: sftp. It opens an interactive session, where you can browse the server and move files. Inside, use ls and cd to look around, put to upload, and get to download. Type exit when you are done.

Command 4: tmux

Command four: tmux. Normally, if your SSH connection drops, everything you were running dies. tmux keeps a session alive on the server. Start one with tmux new, -s, and a name. Press Control B, then D, to detach. Your work keeps running. Later, log in again, and run tmux attach, -t, and the name, to pick up where you left off.

Command 5: nohup

Command five: nohup. It runs a command that survives you logging out. Add an ampersand at the end to run it in the background, and send the output to a log file. Check progress with tail -f on the log file.

Recap

Recap. scp copies files. rsync syncs only the changes. sftp lets you browse and transfer. tmux keeps a session alive. And nohup keeps a job running after you log out.

Course complete

That completes the course. You can now find a server, secure it, log in, and move work onto it. Add these skills to your Master Profile on ChangeCV, and find more free courses at changecv.com/skills.

Common questions

What is the difference between scp and rsync?

scp sends the files you name every time. rsync compares both sides and sends only what changed, so repeated syncs are much faster. Both use SSH.

Does the trailing slash matter in rsync?

Yes. site/ means copy the contents of the folder, while site means copy the folder itself into the destination. Add -n for a dry run to check first.

What is the difference between tmux and nohup?

tmux keeps an interactive session you can come back to and see. nohup just keeps one command running after you log out and writes its output to a file.

How do I stop a job that I started with nohup?

Find its process ID, which the shell prints when the job starts or which you can look up with pgrep, then run kill with that number.

Is scp still safe to use?

Yes. The scp command still works, and since OpenSSH 9.0 it transfers files with the SFTP protocol behind the scenes. You type the same command.