unix swiss army knife

Abstract

Some useful command lines:

# show battery's status
upower -i /org/freedesktop/UPower/devices/battery_BAT0
# time the machine has been running
uptime
# file system structure explanation
man hier
# disk usage
df -k
# information about CPU
lscpu
# display calendar
cal
# bandwidth usage
iftop
# network interfaces
lspci | egrep -i 'network|ethernet'
# info about kernel release
uname -a
# system hardware information
sudo lshw
sudo lshw -html > systemInfo.html
# shutdown at a specific time
sudo shutdown -h 19:05 "Some message..."
# stop / start / restart systemctl
sudo systemctl start serviceName  
sudo systemctl stop serviceName  
sudo systemctl restart serviceName
# check service status
sudo systemctl status serviceName
# enable / disable service to run at boot time
sudo systemctl enable serviceName  
sudo systemctl disable serviceName
 
# increase swap size
# 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
# if: input file, of output file, bs: block size
# 3. Make the file usable as a swap
sudo mkswap /swapfile
# 4. Activate the swap file
sudo swapon /swapfile

Not long ago, I wrote a post with 25 one-command-liners that allow you to do complex things with just one line without using the graphical interface. Now, I will show you a list of commands to turn you into the perfect Linux mechanic.

Note: All commands have been tested on ubuntu 20.04. To execute some commands, you will need administrator permissions.

System information

Check the battery’s status via the terminal

This command outputs a lot of status and statistical information about the battery.

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

In my case, I’m using ubuntu on a virtualized machine, so it doesn’t show relevant stats, but if you try it on a native installation, it will show you a lot of helpful information.

Screen with the result displayed on the command line.

Time the machine has been running

The uptime command returns information about how long the system has been running since it was started up the last time.

$ uptime

Screen with the result displayed on the command line.

Show File system structure

The man hier command shows the description of the file system hierarchy.

$ man hier

Screen with the result displayed on the command line.

Display disk usage information

This command shows the disk usage information.

$ df -k

Screen with the result displayed on the command line.

Display information about the CPU

This command shows the CPU usage information.

$ lscpu

Screen with the result displayed on the command line.

Display a calendar with the current day highlighted

This command displays a simple calendar.

$ cal

Screen with the result displayed on the command line.

Display Bandwidth usage

I love this utility; it is handy to know what is happening on your computer and where it is connecting to.

$ apt-get install iftop  
$ iftop

Screen with the result displayed on the command line.

List network interfaces

This command shows the network interfaces with the “ethernet” name.

$ lspci | egrep -i 'network|ethernet'

Screen with the result displayed on the command line.

Display information about your kernel release

$ uname -a

Screen with the result displayed on the command line.

Display System Hardware information

This command display a lot of information about your Hardware.

$ sudo lshw

Screen with the result displayed on the command line.

If you prefer the short version in HTML format:

$ sudo lshw -html > systemInfo.html

Screen with the result displayed on the browser.

A trick, in the command console, you can display the result in the language of your choice with the following commands:

$ export LANG=en_US.UTF-8  
$ export LANGUAGE=en

System admin

Reboot the system

$ sudo reboot

Shutdown the system at a specific time

$ sudo shutdown -h 19:05 "Some message..."

Start/Stop/Restart a service

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

Checking a service status

$ sudo systemctl status serviceName

Enabling/Disabling a service to run at boot time

$ sudo systemctl enable serviceName  
$ sudo systemctl disable serviceName

Increasing the size of the swapfile

The primary function of swap space is to substitute disk space for RAM when RAM fills up and more space is needed. With this command, you can set the size of the swap file.

  1. Turn off all swap processes
$ sudo swapoff -a
  1. Resize the swap to 4GB
$ sudo dd if=/dev/zero of=/swapfile bs=1G count=4

if: input file, of output file, bs: block size

  1. Make the file usable as a swap
$ sudo mkswap /swapfile
  1. Activate the swap file
$ sudo swapon /swapfile

Schedule jobs with Crontab Command

Cron is a regular background process manager (daemon) that runs processes at regular intervals (for example, every minute, day, week, or month). The processes to be run and the time they should run is specified in the crontab file.

  1. List schedule jobs
$ crontab -l//00 * * * * nameOfTheCommandToExecute

Cron job every hour.

  1. Edit the crontab
$ crontab -e

Screen with the result displayed on the command line.

Create a temporary aliase

An easy way to save time is to create an aliased for the most used commands.

#alias [name]="[command]"  
$ alias dir="ls -la"

Screen with the result displayed on the command line.

Media

Mute volume

$ amixer set Master muteclear  

Screen with the result displayed on the command line.

Find duplicate files based on the MD5 hash

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

Screen with the result displayed on the command line.

Convert camelCase

With this command, you can convert the contents of a file to lowercase.

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

Screen with the result displayed on the command line.

Find files greater between X and Y

Find all files which are accessed N days back

$ find / -atime 2

Screen with the result displayed on the command line.

Find modifies files in the last N minutes

$ find / -mmin -1

Screen with the result displayed on the command line.

Various

With this utility, you can convert text to json format.

$ sudo apt-get install jshon  
$ echo '{"name": "Kesk","surname":"norem"}' | jshon 

Screen with the result displayed on the command line.

Extra

To finish, nothing better than a couple of games to play from the command line. Yes, you can play them from the command line.

Terminal games

Greed

$ sudo apt-get install greed  
$ greed

The pacman game in the bash shell.

Use the arrow keys to eat as many digits as possible in any direction.

Resource page for greed 4.2

Eat as much as you can before munching yourself into a corner. When you move in a direction, you erase N grid squares…

www.catb.org

Pacman

$ sudo apt-get install pacman4console  
$ pacman4console

The Pacman game in the bash shell.

https://github.com/YoctoForBeaglebone/pacman4console

If you have arrived here, I invite you to look at these other 25 Awesome Linux Command One-Liners, where you will find combinations of Linux commands that I am sure will surprise you.

If you liked this post, please consider subscribing to Medium through my profile.

Thank you!