Useful Linux Tips and Tricks

find . | grep -c . - simple way to count number of files and directories below current point, you can add "-type f" to list just files from the find command or "-type d" for just directories

Last Reboot

Interestingly there are three ways to see when a box was last rebooted, try all three and see which you prefer:

  • uptime - this tells you how long the box has been "up"
  • who -b - states the date and time the system last started up
  • last reboot - lists the date and time of all reboots

I like "who -b" for a quick answer and "last reboot" for more information.

Locale and Time Zone

Sometimes you need to know date, time locale and time zone information and thus the following commands can help:
$ date
Thu May 11 11:47:03 BST 2017
$ cat /etc/sysconfig/clock
ZONE="Europe/London"
$ locale | grep LANG
LANG=en_US.UTF-8

From the above you can see this machine is in the UK time zone but has a US locale and is also currently in British Summer Time or Daylight Saving for the UK.

Network Tools

I have not tried this myself but socat I believe will act as a proxy, can be loaded with a certificate and thus can display data going to/from a system.

Converting file Line Endings

So, Windows files typically end with a Carriage Return, Line Feed or CRLF but Linux/Unix files end with just an LF. Sometimes you end up with a Windows file on Unix and you just need to make it Linux friendly. So, the following will edit the file, so maybe make a backup first!
sed -i $'s/\r$//' filename.ext
Some machines may have dos2unix or d2u available but sed is always available.

Processor Information

So, starting with the principle, we should look at Threads, Cores and Sockets, and multiply all three to get the number of CPUs. As is common with Linux there are several ways to get CPU information and you don't need root privilege for all of them either, here are some common and easy options:

  • lscpu - display a nice summary
  • lscpu -p - gives "parsable" output so is easier to use in scripts
  • lscpu | egrep '^Thread|^Core|^Socket|^CPU\(' - extract the numbers for CPU counting
  • nproc --all - just outputs a number, the CPU count
  • cat /proc/cpuinfo - full details on each CPU
  • grep 'processor' /proc/cpuinfo - extract one line for each processor

It is worth noting that "nproc" is part of coreutils and that egrep is the same as "grep -E" and supports extended regular expressions.

Resize Partition

Whilst working with a Linux VM, Ubuntu, running in Hyper-V I ran out of disk space. I could stop the VM and remove all the checkpoints and increase the disk size, however I then needed to get Linux to use this. I wanted to extend the main/primary partition, /dev/sda1. To do this I first installed GParted -- A free application for graphically managing disk device partitions with the following command:
sudo apt install gparted
Then I executed it with sudo gparted and used the visual interface to increase the partition size and voila!