Usage of fdisk in linux

Source: Internet
Author: User
Tags hex code

Fdsik can divide disks into Several partitions, and can also specify and format the partition file system for each partition, such as linux, fat32, linux swap, fat16, and other Unix file systems.

Fdisk-l view all the hard disks and Their partitions of the machine;

You can also use fdisk-l/dev/xxx to view the partition of a hard disk.

Fdisk/dev/xxx starts the session for partitioning a device. Press m to view all commands.

Commonly used:

D delete a partition note: this operation deletes a partition;

L list known partition types Note: l lists the partition types for us to set the corresponding partition types;

M print this menu Note: m lists help information;

N add a new partition Note: add a partition;

P print the partition table Note: p lists partition tables;

Q quit without saving changes Note: Do not save and exit;

T change a partition's system id Note: t changes the partition type;

W write table to disk and exit note: write the Partition table to the hard disk and exit;

Example of deleting a partition using the d command of fdisk:

Command (m for help): p Note: List partition information;

Disk/dev/sda: 1035 MB, 1035730944 bytes

256 heads, 63 sectors/track, 125 cylinders

Units = cylinders of 16128*512 = 8257536 bytes

Device Boot Start End Blocks Id System

/Dev/sda1 1 25 201568 + c W95 FAT32 (LBA)

/Dev/sda2 26 125 806400 5 Extended

/Dev/sda5 26 50 201568 + 83 Linux

/Dev/sda6 51 76 200781 83 Linux

Command (m for help): d Note: run the delete partition to specify;

Partition number (1-6): 6 Note: To delete sda6, enter 6 here;

Command (m for help): p Note: Check the hard disk partition and check whether the partition is deleted?

Disk/dev/sda: 1035 MB, 1035730944 bytes

256 heads, 63 sectors/track, 125 cylinders

Units = cylinders of 16128*512 = 8257536 bytes

Device Boot Start End Blocks Id System

/Dev/sda1 1 25 201568 + c W95 FAT32 (LBA)

/Dev/sda2 26 125 806400 5 Extended

/Dev/sda5 26 50 201568 + 83 Linux

Command (m for help ):

Warning be careful when deleting a partition. Check the sequence number of the partition. If you delete an extended partition, all logical partitions under the extended partition will be deleted. Therefore, be careful when performing operations; if you know that your operation is wrong, please do not panic, do not save q and exit; Remember to remember !!!! When the partition operation is incorrect, do not enter w to save and quit !!!

Example of adding a partition using the n command of fdisk;

Command (m for help): p

Disk/dev/sda: 1035 MB, 1035730944 bytes

256 heads, 63 sectors/track, 125 cylinders

Units = cylinders of 16128*512 = 8257536 bytes

Device Boot Start End Blocks Id System

/Dev/sda1 1 25 201568 + c W95 FAT32 (LBA)

/Dev/sda2 26 125 806400 5 Extended

/Dev/sda5 26 50 201568 + 83 Linux

Command (m for help): n Note: Add a partition;

Command action

L logical (5 or over) Note: to add a logical partition, the partition number must be greater than 5. Why is it greater than 5 because sda5 already exists;

P primary partition (1-4) Note: Add a primary partition; the number ranges from 1 to 4; but sda1 and sda2 are occupied, so they can only start from 3;

P

Partition number (1-4): 3

No free sectors available Note: Why is the failure failed?

Note: I tried to add a primary partition. It seems that it failed. Why did it fail? Because we can see that the primary partition and the extended partition use up the entire disk, look at the End value of the extended partition, and then look at the p output information with 125 cylinders; it is best to look at the previous part; it is mentioned there;

Therefore, we can only add logical partitions;

Command (m for help): n

Command action

L logical (5 or over)

P primary partition (1-4)

L note: Enter l here to enter the partition logic partition stage;

First cylinder (51-125, default 51): Note: This is the Start value of the partition. Press enter here. If you enter a non-default number, this will cause a waste of space;

Using default value 51

Last cylinder or + size or + sizeM or + sizeK (51-125, default 125): + 200 M Note: This defines the partition size, + 200 M indicates that the cylinder size is 200 M. You can also specify the value of End based on the cylinder size indicated by p. Let's look back at how it is calculated. We can use the + M method to add it, which is more intuitive. If you want to add a 10 Gb partition, enter + 10000 M;

Command (m for help ):

Example of specifying the partition type using the fdisk t command

Command (m for help): t Note: use t to specify the partition type;

Partition number (1-6): 6 Note: Which Partition type should be changed? I specified 6, that is, sda6.

Hex code (type L to list codes): L note: Enter L here to view the id of the partition type;

Hex code (type L to list codes): B Note: If I want this partition to be of the W95 FAT32 type, I can see through L that B indicates yes, So I entered B;

Changed system type of partition 6 to B (W95 FAT32) Note: The system information is Changed successfully. If yes, check it with p;

Command (m for help): p

Disk/dev/sda: 1035 MB, 1035730944 bytes

256 heads, 63 sectors/track, 125 cylinders

Units = cylinders of 16128*512 = 8257536 bytes

Device Boot Start End Blocks Id System

/Dev/sda1 1 25 201568 + c W95 FAT32 (LBA)

/Dev/sda2 26 125 806400 5 Extended

/Dev/sda5 26 50 201568 + 83 Linux

/Dev/sda6 51 75 201568 + B W95 FAT32

Format and load partitions;

A prompt is given first. Use mkfs. bfs mkfs. ext2 mkfs. jfs mkfs. msdos mkfs. vfat mkfs. cramfs mkfs. ext3 mkfs. minix mkfs. reiserfs mkfs. xfs and other commands to format the partition. For example, if I want to format sda6 as the ext3 file system, enter;

[Root @ localhost beinan] # mkfs. ext3/dev/sda6

If I want to load sda6 to the current system to access files, there should be a mount command, but first you must create a mount directory; for example,/mnt/sda6;

[Root @ localhost beinan] # mkdir/mnt/sda6

[Root @ localhost beinan] # mount/dev/sda6/mnt/sda6

[Root @ localhost beinan] # df-lh

Filesystem capacity in use available % mount point

/Dev/hda8 11G 8.4G 2.0G 81%/

/Dev/shm 236 M 0 236 M 0%/dev/shm

/Dev/hda10 16G 6.9G 8.3G 46%/mnt/hda10

/Dev/sda6 191 M 5.6 M 176 M 4%/mnt/sda6

In this way, we can access the/mnt/sda6 directory and then access the file.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.