format2usb v2

pull/10/head
gotbletu 8 years ago
parent 6637d426fb
commit 340801e0b7

@ -3,13 +3,13 @@ A couple of functions i created to format my drives. It will delete existing par
I could of used just mkfs to format but from my experience linux likes to have at least one partition so that is why we used fdisk to create a partition before we format with mkfs.
* tutorial video: [Link](https://www.youtube.com/watch?v=ypKjq5KIxSk)
* tutorial video: [Link](https://www.youtube.com/watch?v=7txO1cdNJsQ)
* offical website: [Link](https://www.youtube.com/user/gotbletu)
### install requirements
mkfs fdisk
### configuration
### code
add to ~/.bashrc or ~/.zshrc
#-------- Color Code {{{
@ -35,7 +35,8 @@ add to ~/.bashrc or ~/.zshrc
# Format USB Stick/Hard Drive
# It will create a single partition that fills the whole drive space
format2usb-fat32() {
format2usb-ext2() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "\nUsage: $0 <label> <device>"
@ -52,16 +53,20 @@ add to ~/.bashrc or ~/.zshrc
return 1
fi
# show the device info that is going to be formatted
sudo fdisk -l /dev/"$2"
# warning message countdown
echo -e "${Red}WARNING YOU ARE ABOUT TO FORMAT A DRIVE! ${Color_Off}"
echo -e "${Red}You Have 15sec to hit CTRL+C To Cancle ${Color_Off}"
for i in {15..1..1};do echo -n "$i." && sleep 1; done
# list out all drives
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
echo -n -e "${Red}WARNING: You are about to FORMAT a drive. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "... You chose to continue"
else
return 1
fi
# delete existing partition then create new linux partition
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\nb\nw" | sudo fdisk /dev/"$2"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nw" | sudo fdisk /dev/"$2"
# delete partiton x8 using d\n\n
# d delete a partition
@ -73,18 +78,20 @@ add to ~/.bashrc or ~/.zshrc
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# b W95 FAT32
# w write table to disk and exit
# fat32 likes the labels to be in uppercase
label_name=$(echo "$1" | tr '[:lower:]' '[:upper:]')
# format device
sudo mkfs.fat -F 32 -n "$label_name" -I /dev/"$2"1
echo -e "y\n" | sudo mkfs.ext2 -L "$1" /dev/"$2"1
# set permission
mkdir -p /tmp/testmount
sudo mount /dev/"$2"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir /tmp/testmount
}
format2usb-ntfs() {
format2usb-ext3() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "\nUsage: $0 <label> <device>"
@ -101,16 +108,20 @@ add to ~/.bashrc or ~/.zshrc
return 1
fi
# show the device info that is going to be formatted
sudo fdisk -l /dev/"$2"
# list out all drives
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
# warning message countdown
echo -e "${Red}WARNING YOU ARE ABOUT TO FORMAT A DRIVE! ${Color_Off}"
echo -e "${Red}You Have 15sec to hit CTRL+C To Cancle ${Color_Off}"
for i in {15..1..1};do echo -n "$i." && sleep 1; done
echo -n -e "${Red}WARNING: You are about to FORMAT a drive. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "... You chose to continue"
else
return 1
fi
# delete existing partition then create new linux partition
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\n7\nw" | sudo fdisk /dev/"$2"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nw" | sudo fdisk /dev/"$2"
# delete partiton x8 using d\n\n
# d delete a partition
@ -122,15 +133,20 @@ add to ~/.bashrc or ~/.zshrc
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# 7 HPFS/NTFS/exFAT
# w write table to disk and exit
# format device
sudo mkfs.ntfs -f -L "$1" /dev/"$2"1
echo -e "y\n" | sudo mkfs.ext3 -L "$1" /dev/"$2"1
# set permission
mkdir -p /tmp/testmount
sudo mount /dev/"$2"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir /tmp/testmount
}
format2usb-ext2() {
format2usb-ext4() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "\nUsage: $0 <label> <device>"
@ -147,13 +163,17 @@ add to ~/.bashrc or ~/.zshrc
return 1
fi
# show the device info that is going to be formatted
sudo fdisk -l /dev/"$2"
# list out all drives
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
# warning message countdown
echo -e "${Red}WARNING YOU ARE ABOUT TO FORMAT A DRIVE! ${Color_Off}"
echo -e "${Red}You Have 15sec to hit CTRL+C To Cancle ${Color_Off}"
for i in {15..1..1};do echo -n "$i." && sleep 1; done
echo -n -e "${Red}WARNING: You are about to FORMAT a drive. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "... You chose to continue"
else
return 1
fi
# delete existing partition then create new linux partition
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nw" | sudo fdisk /dev/"$2"
@ -171,17 +191,17 @@ add to ~/.bashrc or ~/.zshrc
# w write table to disk and exit
# format device
echo -e "y\n" | sudo mkfs.ext2 -L "$1" /dev/"$2"1
echo -e "y\n" | sudo mkfs.ext4 -L "$1" /dev/"$2"1
# set read/write permission
# set permission
mkdir -p /tmp/testmount
sudo mount /dev/"$2"1 /tmp/testmount
sudo chmod 777 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir /tmp/testmount
}
format2usb-ext3() {
format2usb-fat32() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "\nUsage: $0 <label> <device>"
@ -198,16 +218,20 @@ add to ~/.bashrc or ~/.zshrc
return 1
fi
# show the device info that is going to be formatted
sudo fdisk -l /dev/"$2"
# list out all drives
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
# warning message countdown
echo -e "${Red}WARNING YOU ARE ABOUT TO FORMAT A DRIVE! ${Color_Off}"
echo -e "${Red}You Have 15sec to hit CTRL+C To Cancle ${Color_Off}"
for i in {15..1..1};do echo -n "$i." && sleep 1; done
echo -n -e "${Red}WARNING: You are about to FORMAT a drive. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "... You chose to continue"
else
return 1
fi
# delete existing partition then create new linux partition
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nw" | sudo fdisk /dev/"$2"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\nb\nw" | sudo fdisk /dev/"$2"
# delete partiton x8 using d\n\n
# d delete a partition
@ -219,20 +243,25 @@ add to ~/.bashrc or ~/.zshrc
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# b W95 FAT32
# w write table to disk and exit
# fat32 likes the labels to be in uppercase
label_name=$(echo "$1" | tr '[:lower:]' '[:upper:]')
# format device
echo -e "y\n" | sudo mkfs.ext3 -L "$1" /dev/"$2"1
sudo mkfs.fat -F 32 -n "$label_name" -I /dev/"$2"1
# set read/write permission
# set permission
mkdir -p /tmp/testmount
sudo mount /dev/"$2"1 /tmp/testmount
sudo chmod 777 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir /tmp/testmount
}
format2usb-ext4() {
format2usb-ntfs() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "\nUsage: $0 <label> <device>"
@ -249,16 +278,20 @@ add to ~/.bashrc or ~/.zshrc
return 1
fi
# show the device info that is going to be formatted
sudo fdisk -l /dev/"$2"
# list out all drives
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
# warning message countdown
echo -e "${Red}WARNING YOU ARE ABOUT TO FORMAT A DRIVE! ${Color_Off}"
echo -e "${Red}You Have 15sec to hit CTRL+C To Cancle ${Color_Off}"
for i in {15..1..1};do echo -n "$i." && sleep 1; done
echo -n -e "${Red}WARNING: You are about to FORMAT a drive. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "... You chose to continue"
else
return 1
fi
# delete existing partition then create new linux partition
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nw" | sudo fdisk /dev/"$2"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\n7\nw" | sudo fdisk /dev/"$2"
# delete partiton x8 using d\n\n
# d delete a partition
@ -270,15 +303,17 @@ add to ~/.bashrc or ~/.zshrc
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# 7 HPFS/NTFS/exFAT
# w write table to disk and exit
# format device
echo -e "y\n" | sudo mkfs.ext4 -L "$1" /dev/"$2"1
sudo mkfs.ntfs -f -L "$1" /dev/"$2"1
# set read/write permission
# set permission
mkdir -p /tmp/testmount
sudo mount /dev/"$2"1 /tmp/testmount
sudo chmod 777 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir /tmp/testmount
}

Loading…
Cancel
Save