DEV Community

Cover image for Create bootable USB from terminal
Linuxander
Linuxander

Posted on

Create bootable USB from terminal

This is for linux beginners, simple how-to create bootable usb from terminal. DD command takes input and duplicate it, and it's exactly what we need.

Serbian Translate:
Tutorial za linux pocetnike, kako napraviti butabilni USB iz terminala. Koristeci DD komandu, koja pravi duplikat zeljenog fajla.

1. Full Command Explained

$ sudo dd if=Ubuntu.iso of=/dev/sdb bs=8M status=progress && sync
Enter fullscreen mode Exit fullscreen mode
  • sudo - give you root access
  • dd - command to duplicate disk
  • if - name of file you want to write
  • of - name of USB device
  • bs - size of bytes to write at a time
  • status - show status progress during duplication
  • sync - synchronize writes

2. Fill Device With Zero (optional)

If you have some damaged fields on USB, fill it with zero first:

sudo dd if=/dev/zero of=/dev/sdb bs=8M status=progress && sync
Enter fullscreen mode Exit fullscreen mode
  • zero - will delete everything on USB, including partitions
  • Run dd zero BEFORE you write file to USB
  • If you need to make a new partition, take a look at FDISK tutorial

3. Make Process Automatic (for future use)

  • Symlink script in /bin/ folder to make it available within the system
ln -s /path/to/writeusb.sh /bin/writeusb
Enter fullscreen mode Exit fullscreen mode
  • Change download folder to any folder you want
#!/bin/bash

#  Check Root User

  if [ $(id -u) != "0" ]; then
    echo -e "$W"" You must have root access to run this script!\n"$RS" "
    sleep 1
    exit
  fi


#  Banner

  echo -e -----------------------------------
  echo -e    Write USB Script
  echo -e -----------------------------------


#  Define Functions

  function list_usb_devices() {
    lsblk -l | grep sd
}

  function clean_usb() {
    sudo dd if=/dev/zero of=$DEV bs=8M status=progress && sync
}

  function write_usb() {
    sudo dd if=$NAME of=$DEV bs=8M status=progress && sync
}

#  Define Working Directory

  DIR=~/Downloads
  cd "$DIR"
  #DEV="/dev/sdb"   # To define USB device name

#  Start Script

  echo -e "Please find USB device name (example: /dev/sdb)"
    list_usb_devices

  echo -e -n "Please enter USB device name: "
    read DEV        # remove this lines if you hardcoded usb device name   

  clear && sleep 1

  echo -e "Your USB device name is: "$DEV" "
  echo -e "Your Working Folder Is: "$DIR" "

  echo -e -n "Enter a name of file to write: "
    read NAME

  echo -e -n "Do you want to fill USB with zero? (y/n): "
    read ANSWZERO

      if [ "$ANSWZERO" == "y" ] || [ "$ANSWZERO" == "Y" ]; then

        echo -e -n "Do you want to write USB after zero-ing? (y/n): "
          read AFTZERO

        if [ "$AFTZERO" == "y" ] || [ "$AFTZERO" == "Y" ]; then
          clean_usb;
          write_usb && echo -e "Process Finished!"
        else
          clean_usb && echo -e "Process Finished!"
        fi

      else
        echo -e -n "Do you want to write file to USB? (y/n): "
          read WRITE

        if [ "$WRITE" == "y" ] || [ "$WRITE" == "Y" ]; then
          write_usb && echo -e "Process Finished!"
        fi
      fi

Enter fullscreen mode Exit fullscreen mode

Top comments (0)