How to format usb drive from bash script?


Running Fedora 14 with bash shell.

I have a USB Drive connected that I need to format from the command line.

The drive is always located at /media/OCH

I want to format it to be ext4 format with the same name as before "OCH".

Can you please provide me with the bash command to do this from inside a shell script?

Who is Participating?
eappiahConnect With a Mentor Commented:
mkfs.ext4 /dev/sdx -L OCH

Where x is your USB drive. might be sda , sdb ,sdc.. or hda,hdb,hdc
if's it already mounted type mount to find out.
SqueezeOJAuthor Commented:
Hi eappiah,

I'll try your solution tomorrow.

Diogo UchoasConnect With a Mentor Network and Security AnalystCommented:
There are several mkfs commands. Depends on what filesystem you want the format.
First, umount the device.
umount /dev/sdX

Open in new window

For fat32:
mkfs.vfat /dev/sdX

Open in new window

For ext3:
mkfs.ext3 /dev/sdX

Open in new window

For cramfs:
mkfs.cramfs /dev/sdX

Open in new window

and etc...
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

SqueezeOJAuthor Commented:
Something's not working the way I was hoping...

If I run
sudo mkfs.ext4 /dev/sdb1 -L OCH

Open in new window

then I get error:
/dev/sdb1 is mounted; will not make filesystem here!

Open in new window

 (This occurs as root or as a privileged user.)

What am I missing?
You need to unmount it first.

Cannot format that is mounted.

"sudo umount /dev/sdb1"
SqueezeOJAuthor Commented:
That makes sense - but then how do I mount it?
SqueezeOJAuthor Commented:
Let me clarify....

1. umount...
2. mkfs.ext4...
3 re-mount

How do I re-mount?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.