Skip to content

Instantly share code, notes, and snippets.

@deepaknverma
Created October 17, 2014 04:01
Show Gist options
  • Save deepaknverma/a16d52d0ee4147fabe1d to your computer and use it in GitHub Desktop.
Save deepaknverma/a16d52d0ee4147fabe1d to your computer and use it in GitHub Desktop.
Ubuntu essential commands for disk mount
If you have migrated recently from Windows to Unix like operating system, you will be bit surprised, when you put any CD Rom or Pen Drive in your system, why doesn’t it get detected. So, why doesn’t it get detected? Because it is not mounted.
As you all know, Linux is working with Files. Device, Drive, Partition everything is File in here. I have told this earlier, working with device is as easy as you write into File and read from it.
Ubuntu and other latest release are capable to auto mount your drive/device/CD/DVD-ROM when you connect.
There are dedicated drives in Linux File System hierarchy to mount external devices/drives/CD-ROMs. They are /media/ and /mnt/ drives. So to mount into any of these two drives, just do,
#mount <what_to_mount> <where_to_mount>
So you got to know <where_to_mount> is either /media/ or /mnt/. But how to find what to mount?
Once you connect any external device, let’s say Pen Drive, if it didn’t auto mounted, then do this. In Terminal, type in,
#mount
it gives you sort of display, in my case it’s, before connecting pen drive mount command gave,
#mount
/dev/sda1 on / type ext4 (rw,errors=remount-ro,commit=0)
proc on /proc type proc (rw,noexec,nosuid,nodev)
none on /sys type sysfs (rw,noexec,nosuid,nodev)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
none on /dev type devtmpfs (rw,mode=0755)
none on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
none on /dev/shm type tmpfs (rw,nosuid,nodev)
none on /var/run type tmpfs (rw,nosuid,mode=0755)
none on /var/lock type tmpfs (rw,noexec,nosuid,nodev)
/dev/sdb1 on /work type ext4 (rw,commit=0)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)
After connecting Pen Drive, the output gave is,
#mount
/dev/sda1 on / type ext4 (rw,errors=remount-ro,commit=0)
proc on /proc type proc (rw,noexec,nosuid,nodev)
none on /sys type sysfs (rw,noexec,nosuid,nodev)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
none on /dev type devtmpfs (rw,mode=0755)
none on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
none on /dev/shm type tmpfs (rw,nosuid,nodev)
none on /var/run type tmpfs (rw,nosuid,mode=0755)
none on /var/lock type tmpfs (rw,noexec,nosuid,nodev)
/dev/sdb1 on /work type ext4 (rw,commit=0)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)
/dev/sdc1 on /media/ust type ext3 (rw,nosuid,nodev,uhelper=udisks)
A new line added for /dev/sdc1 saying it is already mounted on /media/ust and it’s file system type is Ext3.
Any ways it is auto mounted, and you can directly #cd /media/ust/ and access your drive contents.
To manually to execute mount command, if I have to mount /dev/sdc1 in /mnt/ do it like,
#mount /dev/sdc1 /mnt/
How do you connect both the Pen Drives and mount it? You need not worry, it automatically get mounts once you connect. To make it manually, let me say it like this,
Create two directories in your /media/ (— give any name for directory doesn’t matter), let’s say
#mkdir /media/usb1/
and
#mkdir /media/usb2/
Next mount your first Pen Drive into /media/usb1/ and second into /media/usb2/,
#mount /dev/sdc1 /media/usb1/
#mount /dev/sdd1 /media/usb2/
Note: Linux hard disk device names follow a specific standard. SCSI disks all start with letter sd and IDE disks with hd. After this comes a letter that identifies the unit number of the disk, so for example, the first disk would be a, the second would be b, the third would be c, and so on (similarly C: drive D: drives in your Windows partition). Finally, a two-digit number defines the partition number. Using this convention the fifth partition on the fourth IDE drive would be /dev/hdd5.
Suppose you have connected your drive and it is not auto mounted, then first execute and check with
#fdisk -l
command to know, what drives are available in your system. The fdisk (for “fixed disk”) is a commonly used name for a command-line utility that provides disk partitioning functions.
Before we discuss more on fdisk, lets see two of other CLIs.
To know about your drives detail, use df command.
#df
or for the same out to get in big bytes, use,
#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.4G 3.8G 3.3G 54% /
none 242M 652K 241M 1% /dev
none 249M 24K 249M 1% /dev/shm
none 249M 96K 248M 1% /var/run
none 249M 0 249M 0% /var/lock
/dev/sdb1 16G 7.3G 7.8G 49% /work
To find size of any file, use du command,
#du -h /media/usb1/my_file.pdf
About fdisk tool.We going to format it and create a partition in it !! through command line interface.
#fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a0552
Device Boot Start End Blocks Id System
/dev/sda1 * 1 980 7863296 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 980 1045 522241 5 Extended
/dev/sda5 980 1045 522240 82 Linux swap / Solaris
Disk /dev/sdb: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x806c7b17
Device Boot Start End Blocks Id System
/dev/sdb1 1 2088 16771828+ 83 Linux
Disk /dev/sdc: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdc doesn't contain a valid partition table
As you can see above, /dev/sdc doesn't contain any partition. To make a partition in /dev/sdc/ execute below set of commands one by one,
#fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xbadb10e9.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help):m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help):n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4):1
First cylinder (1-2088, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2088, default 2088):
Using default value 2088
Command (m for help):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
I have initiated it with
#fdisk /dev/sdc; please notice I have given drive name as argument to fdisk.
You should not give partition as input argument to fdisk command.
For example, my another drive /dev/sdb already has one partition called /dev/sdb1.
So to make one more partition say /dev/sdb2 in /dev/sdb
#fdisk /dev/sdb
I have received set of outputs,
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xbadb10e9.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help):
it is asking me to give input argument for operation to be carried out and also it is saying to give "m" as argument for help. So I gave m as input. So the output I received is help list of command arguments available for me,
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help):
As you can see into the output it is saying argument n is for creating new partition. So gave n as my input. It gave me there are two types of partition possible one is Primary partition and second is extended partition, which one you opt for,
Command action
e extended
p primary partition (1-4)
I have chosen partition as primary by giving p as my input. Next it has asked me to enter partition number and I have entered 1 as my partition number. Next step is curious, it has asked me to enter disk cylinder start point,
First cylinder (1-2088. default 1):
where I have just pressed Enter; so it has taken default cylinder as 1 for my partition.
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2088, default 2088):
Using default value 2088
Command (m for help):
Next command is where your operation will going to be actually performed. The command w will going to Write the changes you have actually made in previous executions, and until unless you execute w argument in fdisk command what ever you have executed will not be effective. Once I gave w, it written all the changes I have executed into partition table and hence the new partition is created.
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks
Let's check how the new partition that is created in /dev/sdc drive,
#fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a0552
Device Boot Start End Blocks Id System
/dev/sda1 * 1 980 7863296 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 980 1045 522241 5 Extended
/dev/sda5 980 1045 522240 82 Linux swap / Solaris
Disk /dev/sdb: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x806c7b17
Device Boot Start End Blocks Id System
/dev/sdb1 1 2088 16771828+ 83 Linux
Disk /dev/sdc: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier:0x807c8b43
Device Boot Start End Blocks Id System
/dev/sdc1 1 2088 16771828+ 83 Linux
There is a partition /dev/sdc1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment