Skip to main content

Disk

fdisk, gdisk, parted, mkfs, mkswap, lsblk

CommandShort Description
lsblk -afall devices + empty ones with infomration
sudo blkid -o listList devices in a machine-readable format
blkid /dev/sda1Retrieve information about /dev/sda1
sudo fdisk -lList partitions on all disks
sudo fdisk -l /dev/sdbList partitions on /dev/sdb
sudo fdisk /dev/sdaLaunch fdisk for /dev/sda
gdiskGPT disk partitioning utility
sudo gdisk /dev/sdaLaunch gdisk for /dev/sda
partedDisk partitioning tool with more features
sudo parted /dev/sdaLaunch parted for /dev/sda
mkfs.ext4 /dev/sdb1Create an ext4 file system on /dev/sdb1
mkfs -t ntfs /dev/sdd1Create a VFAT File System on /dev/sdd1
sudo mkswap /dev/sdc1Create swap on /dev/sdc1

df, du, fsck, tune2fs, ntfsfix

CommandShort Description
du -sh /path/to/dirDisplay disk usage summary for a directory
du -hd 1 /path/to/dirDisplay disk usage for each subdirectory in the specified directory
df -hDisplay disk usage for all mounted filesystems in readable format
df -hiTDisplay disk space usage with inodes and file system type
sudo fsck /dev/sda1Check and repair the file system on /dev/sda1
sudo fsck -y /dev/sdb2Automatically repair the file system on /dev/sdb2
sudo fsck -C /dev/sdc1Display progress while checking and repairing /dev/sdc1
sudo fsck.ext4 -yvfBcD /dev/sda4Check and repair /dev/sda4
tune2fs -l /dev/sda1Display detailed filesystem information for /dev/sda1
ntfsfix -b -d /dev/sda1Diagnose and fix NTFS issues on a partition

mount, umount

CommandShort Description
mountlist of currently mounted file systems
mount /dev/sda1 /mntMounts the partition /dev/sda1 to the /mnt directory
mount /dev/sda1Mounts a device defined in /etc/fstab
mount -o loop image.iso /mnt/isoMounts an ISO image file as a loop device to the /mnt/iso directory
umount /mntUnmounts the file system mounted at /mnt
umount -l /mntLazy unmount, detaches the file system only when it's no longer in use
mount UUID=1...AB /mnt/uuidMounts file system using its UUID to the /mnt/uuid

ln

CommandShort Description
ln -s source_file link_nameCreate a symbolic link to a file or directory
ln source_file hard_link_nameCreate a hard link to a file

rsync

OptionDescription
-aArchive mode (recursive, permissions, etc.)
-rSync recursively
-eSpecify the remote shell to use
-vVerbose output
--progressShow progress during transfer
-zCompress data during transfer
-PKeep partially transferred files
--deleteDelete extraneous files on the receiving side
-APreserve ACLs (Access Control Lists)
-XPreserve extended attributes
-HPreserve hard links
-nPerform a dry run
# Backup local directory to another location
rsync -avz /path/to/source /path/to/destination

# Backup local directory to remote server
rsync -avz /path/to/local/directory user@remote.server:/path/to/remote/directory

# Backup local directory to remote server, deleting files on remote server if they are deleted locally
rsync -avz --delete /path/to/local/directory user@remote.server:/path/to/remote/directory

# Create a full backup of the system to an external drive
sudo rsync -aAXHv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/var/*","/media/*","/usr/*","/lib/*","/lib64/","/lost+found","/swapfile",".npm*",".npm/*","node_modules*","node_modules/*","mesa_shader_cache*","steamapps*","Data*","Steam*","/home/mlibre/media/*"} / /run/media/mlibre/D/Linux/backup/

# Backup to a server
rsync -e "ssh -p 2222" source/ user@host:/destination/

# Download a large file from a remote server using rsync with resume capability
rsync -LvzP -e "ssh -p 8756" pro@65.21.54.29:/home/pro/largeFile.zip file.zip

dd

CommandShort Description
ddCopy and convert files
dd if=in of=out bs=block_sizeCopy data from 'in' to 'out' with specified block size
dd if=/dev/zero of=zerofile bs=1M count=100Create a 100MB zero-filled file
dd if=in of=out bs=512 count=1 skip=2Copy 512 bytes from 'in' to 'out' starting byte 1024
dd if=/dev/random of=randomfile bs=1M count=1Generate a 1MB file filled with random data
dd if=in | gzip > backup.dd.gzipCompress 'in' data using 'gzip', save as 'backup.dd.gzip'
dd if=ubuntu.iso of=/dev/sdX bs=4M status=progressWrite 'ubuntu.iso' to a USB drive ('/dev/sdX')

ddrescue

ddrescue is a program that can be used to recover damaged hard disks. like flash memories, sd cards and ...

It can be used to recover data from a hard disk that has been corrupted by a disk error, or from a hard disk that has been damaged by a software error.

sudo ddrescue -f -n /dev/sdb sd-card-copy.img r.log 

Backup using locate

sudo tar -czvf backup.tar.gz $(locate x-ui)
sudo tar -czvf backup.tar.gz $(locate x-ui) /root/
sudo tar -czvf backup.tar.gz $(locate x-ui) /root/cert /root/cert.crt /root/private.key /root/.acme.sh

sudo tar xvf backup.tar.gz -C / # Extract in root directory

Automatic mount using fstab

sudo nano /etc/fstab
UUID=0CCB69562B33DFDD /home/mlibre/media/D ntfs-3g uid=1000,gid=1000,rw,auto,user,exec,umask=000 0 0