Useful Linux commands and tricks. Mostly for Ubuntu-based distributions.

Command-line utils

Search text in files

Search text in files with grep:

# Search in the working directory
# -r means "recursive"
grep -r "your text"

# Search in the given directory, i.e. /etc/
grep -r "your text" /etc/

Check free disk space

Display disks and available free space:

# -h displays sizes in a more human-readable format: MBs, GBs, etc.
df -h

Check size of directory

# -h displays sizes in a more human-readable format: MBs, GBs, etc.
# -s give summary; by default it prints out information for all dirs
du /path/to/your/dir/ -s -h

# Without dir calculates size of the current working dir
du -s -h

Check device memory

Check free and used memory, both RAM and SWAP:

# -h displays sizes in a more human-readable format: MBs, GBs, etc.
free -h

Administration

Clean-up apt

Clean up unnecessary packages, including intermediary. Do it rarely, mostly when /boot/ is overfilled with obsolete Linux kernels and related files.

sudo apt autoremove

SWAP-file

Guide for creating and changing SWAP file in Ubuntu.