My Personal Linux Cheat-sheet: Files & Storage

date
May 28, 2022
slug
cheatsheet-file
status
Published
tags
Cheatsheet
summary
Some of my go-to commands when working as a Platform SRE.
type
Post
  1. Search files by content or filename
    1. Full-text search in a folder and its subfolders: grep -rn '/path/to/somewhere/' -e 'pattern'
    2. Fast global search by filename: updatedb; locate [FILENAME]
  1. What to do when my disk is almost full?
    1. How to check disk usage interactively: ncdu -x /
    2. Get disk usage of all folders and files in current pwd: du -sh * | sort -hr
    3. Get top 20 largest files/folders on the disk: du -h /* | sort -hr | head -20
    4. Lower the 5% reserved space to 2% on disk: tune2fs -m2 /dev/sda3 (-m2 is for 2% space)
    5. Clear nginx logs without reload: truncate --size 0 /var/log/nginx/error.log
    6. Purge all rotated nginx logs which is more than 2 days old: find /var/log/nginx -maxdepth 1 -mtime +1 -name "*.log-*" -delete
  1. Rename all files with “*.log-20211122*” to “*.log-20211121*” find . -name '*.log-20211122*' -exec bash -c 'mv $0 ${0/20211122/20211121}' {} \\;
  1. “z-command”: manipulate gzipped files without extracting them.
    1. zcat for cat to view compressed file
    2. zgrep for grep to search inside the compressed file
    3. zless for less, zmore for more, to view the file in pages
    4. zdiff for diff to see the difference between two compressed files
  1. Generate a random file with specified size: head -c 100M </dev/urandom >my_random_file
… and a bonus section below!

Format a new disk in Linux

Create partition table & create partition

We assume the new disk is identified as /dev/vdb.
$ parted -l	  # get disk logical name: /dev/vdb
$ parted /dev/vdb mklabel gpt
$ parted -a optimal /dev/vdb mkpart
partition name? (empty)
file system? ext4
start? 0%
end? 100%

Format this partition

Be cautious! The partition name is /dev/vdb1, NOT /dev/vdb!!
$ mkfs.ext4 /dev/vdb1		# format disk

Mount this partition

$ lsblk -o NAME,FSTYPE,UUID	  # check partition UUID
$ nano /etc/fstab
add this as the last line in the file, remember to replace 9c9d...6dee:
UUID=9c9dee37-122b-4a78-9a85-968c72816dee /data ext4 defaults 1 2
Then run:
$ mkdir /data
$ mount -a	# apply /etc/fstab
 

© Henry Johnson 2021 - 2024

Licensed under CC BY-SA 4.0.

Any and all opinions listed here are my own and not representative of my employers.