Nicolas's workshop

Linux Utils

October 11, 2020

Miscelaneous bash tips

The following command displays the status code from the last command:

echo $?

In the following command, output of a echo command is directed to standard input so that a second command viz more manages it like a file:

echo 'toto' | more -

tree

tree --noreport .

vi

Vi command for eliminating all occurences of colon ":" is the following:

:1,$s/://g

Here is the vi command for replacing all occurences of "old" with "new":

:1,$s/old/new/g

Vi command for searching string "xyz":

/xyz

find

File search:

find ./fvsa/ -name "pvsve*"

grep

Search string 'yourDir' in dir yourdir:

grep -nr 'yourString*' yourdir

sed

Sed is a stream editor for filtering and transforming text.

sed -i 's/word1/word2/g' inputfile

The -i option requests editing in place. s stands for substitute. g stands for global replacement.

A few tips with watch:

Memory usage:

watch -n 5 free -m

Realtime clock in a term:

watch -n 1 date

Modify filenames with rename

Delete 4 first characters in all file names in dir:

rename 's/.{4}(.*)/$1/' *

Delete 5 last characters in all file names in dir:

rename 's/(.*).{5}/$1/' *

wc

Display total number of files in 'folder':

ls folder | wc -l

Disk usage

sudo ncdu -rx /
sudo du -shc /*

Displaying size occupied by Documents directory:

cd ~
sudo du -sh Documents

Managing JDKs on Debian

update-java-alternatives -l
sudo update-java-alternatives -s java-1.8.0-openjdk-amd64

Or in a more interactive way:

update-alternatives --config java

Managing screens

xrandr
xrandr --addmode HDMI-1 2560x1080

Serving static content using http-server

The following command starts http-server and serves all of the static content (e.g. geojson files) available in the current directory:

http-server --cors='*' -p 5252

Generating random passwords

Install pwgen package and run the following command to get a randow password with 12 characters including one special character at least:

pwgen 12 1 -y

Managing permissions

Command to state that owner and group of directory mydir have full permission to access the directory and its content such as read, write and execute whereas others will have read and execute permission:

chmod -R 775 mydir

Probing system on service management tool

The following command can be use to check whether the service management tool is service or systemctl:

ps --no-headers -o comm 1

A systemd result indicates that systemd (systemctl) is the service management tool, while init indicates that it is System V Init (service).

Managing DNS

sudo apt install bind9-host
host -t NS google.com

Configuring Postfix on a debian server

First hostname has to be configured. Display current hostname with hostnamectl. Then configure new hostname with:

hostnamectl set-hostname muydomain.com

Verify it by running the hostnamectl command again.

Here is the link to configure postfix.

Configuring mailutils on debian

See parameters that may be configured with `mail --config-help

Now to configure them, create a file /etc/mailutils.conf if it does not already exist.

To configure for instance the domain name, complement /etc/mailutils.conf with

address {
  email-domain somedomain.com;
};

Previous: Generate and deploy a Java app on the cloud with Jhipster

Next: Kafka