Skip to content

Instantly share code, notes, and snippets.

@chaicko
Last active February 22, 2017 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaicko/a0b02183060bd3e8fec3b25855b265d5 to your computer and use it in GitHub Desktop.
Save chaicko/a0b02183060bd3e8fec3b25855b265d5 to your computer and use it in GitHub Desktop.
Useful shell commands to do specific tasks

Mounting a remote Filesystem locally

Sometimes you need to mount a remote file system locally in order to share files, do some work or deploy a program remotelly. There are several protocols to do this like NFS, Samba, SSHFS and others. Here are some recipes.

Using SSHFS

Provided you have a user and password (or ssh keys loaded in the remote host) and an ssh server is running on the remote host, you can mount the remote filesystem through an SSH connection.

First, install sshd:

sudo apt install sshfs

Then just create the local mount point (directory) for the remote file system and use sshfs to mount. Given your user is myuser in the remote host, the remote host is called remote-host, the directory on the remote host that you want to mount is /home/myuser and the local directory where you want to mount is ~/remote-dir, type the following:

mkdir ~/remote-dir
sshfs myuser@remote-host:/home/myuser ~/remote-dir

That's it, but keep in mind that you'll have the same permissions of your remote user on the mounted filesystem so you can accidentally delete things. If you want to mount read only, use the -o ro option, like this:

sshfs myuser@remote-host:/home/myuser ~/remote-dir -o ro

You can pass several options, check the sshfs manual for more info.

Writing compressed image files to block devices (SD cards, etc)

Lets say you have a binary image imgfile.img compressed in some format (.zip, .xz, .tar.gz) and you want to write it to /dev/sdc, which is block device (an SD card most likely). This is how you do it in different forms, depending on the compressed file.

If image file is .img.xz

Use xzcat with a pipe to dd:

xzcat imgfile.img.xz | sudo dd bs=2M of=/dev/sdc status=progress  

If image file is a .tar.gz

Use tar with the special option --to-command:

tar -xf imgfile.img.tar.gz --to-command="sudo dd bs=2M of=/dev/sdc status=progress"

Alternatively, you can pass options xfO to tar in order to send the ouput to stdout and then you can pipe to dd:

tar -xfO imgfile.img.tar.gz | sudo dd bs=2M of=/dev/sdc status=progress

If image file is a .zip

Use unzip with a pipe to dd:

unzip -p imgfile.zip | sudo dd bs=2M of=/dev/sdc status=progress 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment