Skip to content

Instantly share code, notes, and snippets.

@stevekm
Last active December 4, 2023 15:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stevekm/41c7a8da8cd35b461089e8377ce5032d to your computer and use it in GitHub Desktop.
Save stevekm/41c7a8da8cd35b461089e8377ce5032d to your computer and use it in GitHub Desktop.
handy ssh, rsync, scp commands

Here are some handy commands for connecting to servers and copying files about. These are all for Linux terminal / bash shell

Do cool things with ssh; log in & run command, find files in dir

# log into server
ssh username@server.edu -Y 
# -X      Enables X11 forwarding. <- use this to enable X11 graphical windows, but this will eventually time out & you'll lose the X11 connection
# -Y      Enables trusted X11 forwarding. <- this one preserves your X11 connection while you're logged in

# run a command on the remote server
ssh username@server.edu find /path/to/server/source -maxdepth 1 -type f

# run a lot of commands on the remote server and write the output to a file, using a heredoc
# also demo variable expansion in heredoc
my_var="foo"
ssh username@server.edu > server_info.txt << EOF
    echo "$my_var"
    sys_info="\$(uname -a)"
    echo "\$sys_info"
EOF

Use rsync to copy files from a server to your local machine

rsync -avzheRn --progress --max-size=500K -e "ssh -p 501" username@server.edu:/path/to/server/source/ /path/to/local/destination/
# rsync /source/ /destination/

# "ssh -p 22" = connect via ssh, port 22
# -n, --dry-run               show what would have been transferred
# -a, --archive               archive mode; same as -rlptgoD (no -H), <- preserves timestamps
# -v, --verbose               increase verbosity
# -z, --compress              compress file data during the transfer
# -h, --human-readable        output numbers in a human-readable format
# -e, --rsh=COMMAND           specify the remote shell to use
# -R, --relative              use relative path names
# --progress              show progress during transfer
# -L, --copy-links            transform symlink into referent file/dir <- very good if you have symlinks and want the original file

Copy files and dirs with scp

scp -prP 501 username@server.edu:/path/to/server/source/\{dir1,dir2\} /path/to/local/destination/
# -P      port
# -p      Preserves modification times, access times, and modes from the original file.
# -r      Recursively copy entire directories.  Note that scp follows symbolic links encountered in the tree traversal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment