Home > Linux > Useful Linux Commands

Useful Linux Commands

Check the battery’s status via the terminal

$ upower -i /org/freedesktop/UPower/devices/battery_BAT0

Time the machine has been running

$ uptime

Display disk usage information

$ df -k

Display information about the CPU

$ lscpu

List network interfaces

$ lspci | egrep -i 'network|ethernet'

Display information about your kernel release

$ uname -a

Display System Hardware information

$ sudo lshw

Reboot the system

$ sudo reboot

Shutdown the system at a specific time

$ sudo shutdown -h 19:05 "Scheduled to shut down the system at 19:05"

Start/Stop/Restart a service

$ sudo systemctl start
$ sudo systemctl stop
$ sudo systemctl restart

Checking a service status

$ sudo systemctl status

Enabling/Disabling a service to run at boot time

$ sudo systemctl enable
$ sudo systemctl disable

Steps to Increasing the size of the swapfile

1. Turn off all swap processes
$ sudo swapoff -a

2. Resize the swap to 4GB
$ sudo dd if=/dev/zero of=/swapfile bs=1G count=4

3. Make the file usable as a swap
$ sudo mkswap /swapfile

4. Activate the swap file
$ sudo swapon /swapfile

Find duplicate files based on the MD5 hash

$ find -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 33 | cut -c 35-

Convert camelCase

$ sed -e 's/(.*)/\L\1/' input.txt > output.txt

Find all files which are accessed N days back

$ find / -atime 2

Find modifies files in the last N minutes

$ find / -mmin -1

Mount a temporary RAM partition

$mount -t tmpfs tmpfs /mnt -o size=2048m

Kill process by name

$pkill -x chrome

Create a complete compressed image of a disk

$dd if=/dev/sda | gzip -c > /path/to/my.disk.image.gz bs=1M

Restore the system:

$gunzip -c /path/to/my.disk.image.gz | dd of=/dev/sda

Limit the CPU usage for a process (say at 25%)

$sudo cpulimit -p -l 25

Find all files larger than 5M and less than 10M

$find / -type f -size +50M -size -10M

Use the rsync command with a progress bar to copy

$rsync -rv --progress

Check whether your computer is 32-bit or 64-bit

$getconf LONG_BIT

Show the working directory of a process

$pwdx

Set password protection to a file using vim

$vim -x file.txt

Monitor memory usage in a human-readable way

$watch vmstat -sSM