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
- Search files by content or filename
- Full-text search in a folder and its subfolders:
grep -rn '/path/to/somewhere/' -e 'pattern'
- Fast global search by filename:
updatedb; locate [FILENAME]
- What to do when my disk is almost full?
- How to check disk usage interactively:
ncdu -x /
- Get disk usage of all folders and files in current pwd:
du -sh * | sort -hr
- Get top 20 largest files/folders on the disk:
du -h /* | sort -hr | head -20
- Lower the 5% reserved space to 2% on disk:
tune2fs -m2 /dev/sda3
(-m2 is for 2% space) - Clear nginx logs without reload:
truncate --size 0 /var/log/nginx/error.log
- Purge all rotated nginx logs which is more than 2 days old:
find /var/log/nginx -maxdepth 1 -mtime +1 -name "*.log-*" -delete
- Rename all files with “*.log-20211122*” to “*.log-20211121*”
find . -name '*.log-20211122*' -exec bash -c 'mv $0 ${0/20211122/20211121}' {} \\;
- “z-command”: manipulate gzipped files without extracting them.
zcat
forcat
to view compressed filezgrep
forgrep
to search inside the compressed filezless
forless
,zmore
formore
, to view the file in pageszdiff
fordiff
to see the difference between two compressed files
- 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