Geody Labs


# Main Index: Debian Linux Magic Spells Cheat Sheet (one liners, how to, tips and tricks)

# Disk and Device Operations

Show available devices:
fdisk -l
fdisk -l --bytes # show size in bytes rather than human readable format
lsblk # list information about all available devices
lsblk -f # 
lsblk -a # list information about all available devices including empty devices and RAM disk devices
lsblk -b # list information about all available devices, show size in bytes rather than human readable format
blkid # show information about available block devices

Show device partitions:
fdisk -l /dev/DEVICE

Create and edit disk partitions:
apt-get install parted
parted # starts the partition utility
parted commands:
print # show current partitions
mkpart primary START END # create a new primary partition
mkpart extended START END # create a new extended partition
mkpart logical START END # create a new logical partition (within the range of an extended one)
resize PARTITON_N NEW_START NEW_END # resize an existing partition
rm PARTITION_N # remove a partition
quit # quit parted

Choose what partitions should be automatically mounted at system startup:
jed /etc/fstab # edit static file system information

Make a file system (format) a disk (partition):
mkfs.ext3 /dev/hda1 # Format hda1 as an ext3 partition
mkfs -V -t ext3 /dev/hda1 # Format hda1 as an ext3 partition (Verbose output)
mkfs -t msdos  /dev/hda2 # Format hda1 as a MS-DOS partition
mkfs -t msdos /dev/fd0 # Format a floppy disk using the MS-DOS file system
mkswap /dev/hda2 # Format hda2 as the swap partition

dd if=/dev/zero of=/dev/foo1 bs=512 count=1 # If you created or changed a DOS partition, you can use dd to zero the first 512 bytes

Show mounted devices:
mount

Mount a device:
mount DEVICE # example: mount /dev/cdrom
mount -t FILESYSTEM DEVICE MOUNTPOINT # Mount a DEVICE at MOUNTPOINT as a given FILESYSTEM; example: mount -t ext3 /dev/sda1 /test

Mount an ISO image of a CD:
mount -o loop cd.iso /mnt/DIR_NAME/

Unmount a device:
umount DEVICE # example: umount /dev/cdrom 

Unmount and eject a device (like a CD-ROM):
eject # eject default device
eject -d # show default device
eject -r # eject CD-ROM

Force changed blocks cached in RAM to disk:
sync

SMART (Self-Monitoring, Analysis and Reporting Technology) Devices monitoring:
apt install smartmontools
smartctl -V # Show smartctl version and configure arguments
service smartd start ; chkconfig smartd on # Enable Service
smartctl -s on /dev/sda # Enable SMART on /dev/sda (use lsblk and fdisk -l to see all devices in your system). Some devices such as Virtual Disks do not support SMART
smartctl -i /dev/sda # Print SMART information about /dev/sda
smartctl --all /dev/sda # Print SMART information and data about /dev/sda
smartctl -t short /dev/sda # Short Test (it usually teakes minutes to complete)
smartctl -t long /dev/sda # Long Test (it usually teakes hours to complete)
smartctl -H /dev/sda # Check if the hard drive passed or failed last test

Create a RAMDisk:
mkdir -p /media/ramdisk
mount -t tmpfs -o size=2048M tmpfs /media/ramdisk # 2GB RAMDisk, it can use swap partition if needed, and doesn't preallocate disk space.
mount -t ramfs -o size=1024M ramfs /media/ramdisk # 1GB RAMDisk, it only uses physical RAM, not the swap partition, and preallocates disk space. Make sure you have enough spare RAM.
umount /media/ramdisk # unmount RAMDisk

Access a XFS partition:
# You typically need this to read disks from NAS devices, via USB.
mkdir /ramdisk/xfs1 # If you are using a Linux boot disk, like Knoppix, you'll probably need to mount the XFS partition in the RAMDisk
fdisk -l # Use fdisk to locate the XFS partition
mount -t xfs /dev/XFS_PARTITION /ramdisk/xfs1 # use the path to XFS partition you've found using fdisk instead of /dev/XFS_PARTITION
cp -rp /ramdisk/xfs1/* /DESTINATION # copy all files from the XFS formatted partition to another device ( /DESTINATION )

Check and repair a file system:
fsck
fsck -f -y FILE_SYSTEM # Note: Generally the device containing the file system to check can be /dev/hda0 on a single hard drive system or /dev/md0 on a RAID system); -y automatically reply yes (y) to all questions. Note that if it asks to connect to lost+found more than once, you may have some damaged files.

List RAID arrays:
mdadm --examine --scan

Check if a RAID array is up and working properly:
mdadm --detail /dev/md0 # assuming your RAID volume is /dev/md0

Check if two devices are in a RAID array:
mdadm --examine --scan /dev/sda1
mdadm --examine --scan /dev/sdb1
# Must return same MD array and UUID. Path to actual devices in your system may differ from /dev/sda1 and /dev/sdb1 of this example

Copy partitions from a device to another one:
sfdisk -d /dev/sda | sfdisk /dev/sdb # copy partitions from sda to sdb

Raw copy:
dd if=/dev/fd0 of=/dev/fd1 # copy a floppy disk (using two floppy disk drives)
dd if=/dev/hda0 of=/dev/hda1 # copy a hard disk (or partition) into another one
dd if=/dev/hda | gzip -9v | dd of=/mnt/hdb/hda.img # back up the whole hard disk as a gzipped file (you may need to mount a partition of another hard disk as hdb: mount /dev/hdb2 /mnt/hdb )
dd of=/mnt/hdb/hda.img | gzip | if=/dev/hda # restore the gzipped image of a hard disk
dd if=/dev/fd0 of=/tmp/disk.bak # copy the whole floppy disk as a file
dd if=/dev/hda of=/tmp/mbr.bin count=1 bs=512 # back up MBR (boot loader and partition table)

Show free disk space: 
df -T # Show disk usage for all filesystems
df -Th # Show disk usage for all filesystems in human readable format(in Megabytes, Gigabytes...)
df -Th / # Show disk usage for the filesystem mounted on root in human readable format (Megabytes, Gigabytes...)
df -h / | xargs | awk '{print "Disk ("$8"): Size: " $9 " Used: " $11 " Free: " $10}'; # Show disk usage for the filesystem mounted on root in a easily human readable format
df -P -h | awk '0+$5 >= 90 {print "FS: "$1" ("$6") Size: "$2" Used: "$3" (\033[1;31m"$5"\033[0m) Free: "$4" (\033[1;31m"(100-$5)"%\033[0m)";}' # Only show near full file systems

Summarize disk usage:
# du shows size in KBytes by default (switch: -k), if you want to see the size in bytes, use the switch -b
du PATH/FILE # disk usage for specified file
du -bs PATH/DIRECTORY | awk '{print $1}' | tr -d '\n' # Total disk usage in bytes of the DIRECTORY in the given PATH
du -s PATH # disk usage for the whole directory (recursively), total size
du -s --exclude=*.mp3 PATH # disk usage for the whole directory (recursively), total size, excluding all files ending with .mp3
du PATH # disk usage for the whole directory (recursively)
du -a PATH # disk usage for the whole directory (recursively), showing disk usage for all files and directories, not just directories
du -S PATH # disk usage for the whole directory (recursively, but size is separated for each directory)
du -a PATH | sort -nr | less # show directories and files sorted by size for the given PATH and its subdirectories
du -a PATH | sort -nr | head -n 25 # show the 25 largest directories and files within the given PATH . Use root ( / ) as PATH to see the largest directories and files in the whole disk
find PATH -type f -exec file -b '{}' \; -printf '%s\n' | awk -F , 'NR%2 {i=$1} NR%2==0 {a[i]+=$1} END {for (i in a) printf("%12u %s\n",a[i],i)}' | sort -nr # show the size occupied by files within a given PATH sorted by file type (slow)
du -cs PATH/FILE* # show size of files matching a pattern
du -cs PATH/FILE* | tail --lines=1 | awk '{print $1}' # show total size of files matching a pattern
du -cs /var/log/apache2/*.gz | grep "$(printf '\t')total" # show total size of apache 2 compressed log files
du -csh PATH/FILE* # show size of files matching a pattern in human readable format
du -csh PATH/FILE* | tail --lines=1 | awk '{print $1}' # show total size of files matching a pattern in human readable format
du -csh /var/log/apache2/*.gz | grep "$(printf '\t')total" # show total size of apache 2 compressed log files in human readable format

Disk Quotas

apt-get install quota quotatool quota # display disk usage and limits quotacheck # scan a file system for disk usage quotactl # set disk quotas

Operations with Disks and Devices on a remote system

Mirror a disk over the network

If you have two computers with identical hardware you can mirror a system into the other machine through the network This is especially useful with clusters / virtual servers Start both machines from a live CD (like Debian Live - https://www.debian.org/CD/live/ ) Connect both machine with a cross cable through their eth1 interfaces and set them this way: root@source_pc# ifconfig eth1 192.168.0.1 root@destin_pc# ifconfig eth1 192.168.0.2 Then execute these commands on the machines to start transfers: root@source_pc# dd if=/dev/sda bs=128M | nc _w2 192.168.0.2 9000 root@destin_pc# nc _l _p 9000 | dd of=/dev/sda Depending from the size of the disk it may need many hours




Please DONATE to support the development of Free and Open Source Software (PayPal, Credit Card, Bitcoin, Ether)

Page issued on 25-Sep-2022 04:57 GMT
Copyright (c) 2022 Geody - Legal notices: copyright, privacy policy, disclaimer