List disks with fdisk without showing loopback disks created by snap apps

You might’ve noticed that Snap apps creates a loopback disk used for mounting the image file for the app. This clutters the UI of fdisk, df and other disk related tools.

A typical fdisk -l could show you this:

Since most regular drives starts as /dev/sd* you could simply grep the output:

sudo fdisk -l | grep "Disk /dev/sd"

Or even better, use sudo fdisk -l | grep -v "loop". The -v parameter means grep reverts the keyword so anything containing “loop” will not be shown.

Another example: df -h:

Now: df -h | grep -v loop

Note regarding grep: You only need to add quotation marks around the keyword if it’s a string which contains spaces. Single words don’t need quotes.

You can also pipe it several times through grep, like this:

sudo df -h | grep -v loop | grep -v tmp

Or take a look at this Stackoverflow thread for more grep variants

Linux Cheatsheet

Network

ip addr show        => Display network interfaces and IP address
ip address add 192.168.0.1 dev eth0 => Set IP address to 192.168.0.1
ethtool eth0 => Show ethernet status eth0
mii-tool eth0 => Show ethernet status for eth0
whois <domain> => Lookup whois info for domain
dig <domain> => Lookup DNS info for domain
host <domain> => Lookup DNS address for domain
wget fileurl => Download fileurl
netstat -tupl => List all active listening ports

Hardware

dmesg               => List detected hardware and boot messages
cat /proc/cpuinfo => Get CPU info
cat /proc/meminfo => Get memory info
lshw => Display hardware configuration
lsblk => Display block device info
free -h => Display available memory in human readable format
lspci -tv => Display PCI devices
lsusb -tv => Display USB devices
dmidecode => Display hardware info from BIOS
hdparm -tT /dev/sda => Read speed test on disk "sda"
powertop => Battery life info
iotop => Disk IO info
smartctl => Disk status
tlp-stat => Power status and battery info

Print lines of code (amount) for all .php files in current directory

find . -name '*.php' | xargs wc -l

Log something to /var/log/syslog

logger <message>

Show all group memberships for current user

id

Get username for Apache service

ps aux | grep apache

(Relatively) secure permissions on public html folder for Apache/nginx

The goal is to set root as file owner and add the www-data as group (www-data is the group of the user running apache/nginx). Then, for every user which requires write-permission, simply add them to the www-data group.

sudo chown root:www-data /path/to/htdocs/* -R 
sudo chmod 775 /path/to/htdocs/* -R
chmod 775 gives everyone R/W/X except public because they shouldn’t have write permissions

Change www-data with whatever group belonging to the user account which is running apache/nginx.

Count files in directory

ls -lah | wc -l

Create an alias for bash commands and/or custom functions

First you need to open your bash profile config file. If preferred, you can use nano and open it with: nano ~/.bashrc

Example: Add a shorthand command for ls and tail with some extra parameters:

alias l="ls -lahS"
alias t='tail -n 200 $1'

As you can see, the tail command also reads file argument given to the command.

Example: Add a custom function for searching for a string in all files in current directory:

grepfor() {
grep -rnw './' -e "$1"
}

When done, reload your config file by running:
. ~/.bashrc

Search for string in files

grep -rnw '/path/to/dir/' -e 'stringpattern'

List only files with size above 100 MB

find . -type f -size +100M -exec ls -lh {} \;

This command scans the current dir for files above 100 MB and then executes the ls -lh command for those files. You could also say -size -10M to do the same for files below 10 MB in size.

Clean/format USB drives

sudo fdisk -l
sudo mkdosfs /dev/xxx # (i.e sdb1)
sudo mkdir /media/usb
sudo mount /dev/xxx /media/usb

See uptime and average load

uptime

Change timezone

sudo rm /etc/localtime
sudo ln -s /usr/share/zoneinfo/Europe/Oslo /etc/localtime

Replace Europe/Oslo with your preferred timezone

Remove password for local user (make it passwordless)

sudo passwd --delete username

See current users logged on server

users (usernames only)

or

who(includes extra information like logon time and IP address etc)

Show IP addresses for all local network cards (including local loopback)

ifconfig | grep inet | cut -d: -f2 | cut -d" " -f1

or

ifconfig | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'

If you wish to see the IP address for a specific NIC (network interface card), simply append the name after ifconfig, like ifconfig eth0 | ...

Map network share

$ sudo mkdir /mnt/ShareName
$ sudo mount -t cifs -o username=domain\username,password=pw //server/share /mnt/ShareName

List automated ssh attacks to your server from syslog:

$ sudo cat /var/log/* | grep BREAK-IN

This post will be continually updated with useful commands and other notes for Linux environments

Related posts: