Validate Configuration Files or Scripts in Linux

1. Sudo Command

You can check the sudo configuration file syntax using the visudo command, which supports a --check or -c command line option to only validate a file without an edit. The -f option displays the error message and opens the file for editing:

$ sudo visudo -c
/etc/sudoers: parsed OK
/etc/sudoers.d/README: parsed OK
$ visudo -h
visudo - safely edit the sudoers file

usage: visudo [-chqsV] [[-f] sudoers ]

  -c, --check              check-only mode
  -f, --file=sudoers       specify sudoers file location
  -h, --help               display help message and exit
  -q, --quiet              less verbose (quiet) syntax error messages
  -s, --strict             strict syntax checking
  -V, --version            display version information and exit
$ sudo visudo -V
visudo version 1.8.31
visudo grammar version 46

jq – commandline JSON processor

What are JSON and CSV formats?

JSON is an acronym for JavaScript Object Notation. Often used in web apps, but it is not limited to JavaScript. One can use any programming language such as Perl, Python or CLI tools such as jq.

CSV is an acronym for Comma-separated values. It is a text file you can use in spreadsheet apps, programming languages and many other apps.

Installing jq

Since jq is available in most common repos, I will use that one for my needs. Here is how to install jq on a Debian or Ubuntu Linux using the apt command/apt-get command:

$ sudo apt install jq

RHEL/Fedora/CentOS/Alma/Rocky Linux user try the dnf command:

$ sudo dnf install jq

Alpine Linux user try the apk command:

$ sudo apk add jq

SUSE or OpenSUSE Linux user try the zypper command:

$ sudo zypper in jq

macOS / OS X user install homebrew and then use the brew command:

$ brew install jq


$ jq -h
jq - commandline JSON processor [version 1.6]

Usage:  jq [options] <jq filter> [file...]
        jq [options] --args <jq filter> [strings...]
        jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.

The simplest filter is ., which copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).

For more advanced filters see the jq(1) manpage ("man jq")


        $ echo '{"foo": 0}' | jq .
                "foo": 0

Some of the options include:
  -c               compact instead of pretty-printed output;
  -n               use `null` as the single input value;
  -e               set the exit status code based on the output;
  -s               read (slurp) all inputs into an array; apply filter to it;
  -r               output raw strings, not JSON texts;
  -R               read raw strings, not JSON texts;
  -C               colorize JSON;
  -M               monochrome (don't colorize JSON);
  -S               sort keys of objects on output;
  --tab            use tabs for indentation;
  --arg a v        set variable $a to value <v>;
  --argjson a v    set variable $a to JSON value <v>;
  --slurpfile a f  set variable $a to an array of JSON texts read from <f>;
  --rawfile a f    set variable $a to a string consisting of the contents of <f>;
  --args           remaining arguments are string arguments, not files;
  --jsonargs       remaining arguments are JSON arguments, not files;
  --               terminates argument processing;

Named arguments are also available as $ARGS.named[], while
positional arguments are available as $ARGS.positional[].

See the manpage for more options.

ตัวอย่างไฟล์ df.json

    "fs": "/dev/mapper/vgubuntu-root",
    "type": "ext4",
    "size": "915G",
    "used": "135G",
    "avail": "734G",
    "usedpercentage": "16%",
    "mounted": "/"
    "fs": "/dev/nvme0n1p2",
    "type": "ext4",
    "size": "1.4G",
    "used": "378M",
    "avail": "939M",
    "usedpercentage": "29%",
    "mounted": "/boot"
    "fs": "/dev/nvme0n1p1",
    "type": "vfat",
    "size": "511M",
    "used": "30M",
    "avail": "482M",
    "usedpercentage": "6%",
    "mounted": "/boot/efi"

convert JSON to CSV

$ cat df.json | jq -r '.[]| join(",")'
$ cat df.json | jq -r '.[]| join(",")' > df.csv
$ cat df.csv


We use the df command to find total disk space and available space on a Linux / Unix file system.

The df is an acronym for disk free.

$ df --help
Usage: df [OPTION]... [FILE]...
Show information about the file system on which each FILE resides,
or all file systems by default.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all             include pseudo, duplicate, inaccessible file systems
  -B, --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,
                           '-BM' prints sizes in units of 1,048,576 bytes;
                           see SIZE format below
  -h, --human-readable  print sizes in powers of 1024 (e.g., 1023M)
  -H, --si              print sizes in powers of 1000 (e.g., 1.1G)
  -i, --inodes          list inode information instead of block usage
  -k                    like --block-size=1K
  -l, --local           limit listing to local file systems
      --no-sync         do not invoke sync before getting usage info (default)
      --output[=FIELD_LIST]  use the output format defined by FIELD_LIST,
                               or print all fields if FIELD_LIST is omitted.
  -P, --portability     use the POSIX output format
      --sync            invoke sync before getting usage info
      --total           elide all entries insignificant to available space,
                          and produce a grand total
  -t, --type=TYPE       limit listing to file systems of type TYPE
  -T, --print-type      print file system type
  -x, --exclude-type=TYPE   limit listing to file systems not of type TYPE
  -v                    (ignored)
      --help     display this help and exit
      --version  output version information and exit

Display values are in units of the first available SIZE from --block-size,
and the DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

FIELD_LIST is a comma-separated list of columns to be included.  Valid
field names are: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent',
'size', 'used', 'avail', 'pcent', 'file' and 'target' (see info page).

GNU coreutils online help: <>
Report df translation bugs to <>
Full documentation at: <>
or available locally via: info '(coreutils) df invocation'

df หรือ df -k หน่วยเป็น K

$ df
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sdc       263174212   1114200 248621856   1% /
none             8119736        28   8119708   1% /mnt/wslg
none             8119736         4   8119732   1% /mnt/wsl
tools          981111804 490438048 490673756  50% /init
none             8117652         0   8117652   0% /dev
none             8119736         0   8119736   0% /run
none             8119736         0   8119736   0% /run/lock
none             8119736         0   8119736   0% /run/shm
none             8119736         0   8119736   0% /run/user
tmpfs            8119736         0   8119736   0% /sys/fs/cgroup
drivers        981111804 490438048 490673756  50% /usr/lib/wsl/drivers
lib            981111804 490438048 490673756  50% /usr/lib/wsl/lib
none             8119736        76   8119660   1% /mnt/wslg/versions.txt
none             8119736        76   8119660   1% /mnt/wslg/doc
drvfs          981111804 490438048 490673756  50% /mnt/c

df -m หน่วยเป็น M

$ df -m
Filesystem     1M-blocks   Used Available Use% Mounted on
/dev/sdc          257007   1089    242795   1% /
none                7930      1      7930   1% /mnt/wslg
none                7930      1      7930   1% /mnt/wsl
tools             958117 478944    479174  50% /init
none                7928      0      7928   0% /dev
none                7930      0      7930   0% /run
none                7930      0      7930   0% /run/lock
none                7930      0      7930   0% /run/shm
none                7930      0      7930   0% /run/user
tmpfs               7930      0      7930   0% /sys/fs/cgroup
drivers           958117 478944    479174  50% /usr/lib/wsl/drivers
lib               958117 478944    479174  50% /usr/lib/wsl/lib
none                7930      1      7930   1% /mnt/wslg/versions.txt
none                7930      1      7930   1% /mnt/wslg/doc
drvfs             958117 478944    479174  50% /mnt/c

df -h human-readable

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc        251G  1.1G  238G   1% /
none            7.8G   28K  7.8G   1% /mnt/wslg
none            7.8G  4.0K  7.8G   1% /mnt/wsl
tools           936G  468G  468G  50% /init
none            7.8G     0  7.8G   0% /dev
none            7.8G     0  7.8G   0% /run
none            7.8G     0  7.8G   0% /run/lock
none            7.8G     0  7.8G   0% /run/shm
none            7.8G     0  7.8G   0% /run/user
tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
drivers         936G  468G  468G  50% /usr/lib/wsl/drivers
lib             936G  468G  468G  50% /usr/lib/wsl/lib
none            7.8G   76K  7.8G   1% /mnt/wslg/versions.txt
none            7.8G   76K  7.8G   1% /mnt/wslg/doc
drvfs           936G  468G  468G  50% /mnt/c


3.6 base64: Transform data into printable data

base64 transforms data read from a file, or standard input, into (or from) base64 encoded form. The base64 encoded form uses printable ASCII characters to represent binary data. Synopses:

base64 [option]… [file]
base64 --decode [option]… [file]

Example#1: Encoding text data

$ echo  '' | base64

Example#2: Decoding text data

$ echo 'bGludXhoaW50LmNvbQo=' | base64 --decode

Example#3: Encoding text file

$ base64 sample.txt
$ base64 sample.txt > encodedData.txt
$ cat encodedData.txt

Example#4: Decoding text file

$ base64 -d encodedData.txt
$ base64 --decode encodedData.txt > originalData.txt
$ cat originalData.txt


6.5 md5sum: Print or check MD5 digests

md5sum computes a 128-bit checksum (or fingerprint or message-digest) for each specified file.

Syntax :

md5sum [OPTION]... [FILE]...
$ md5sum index.png
4191736479ed247941e1bf20830618ad  index.png

Command usage examples with options :

Example 1: Store the MD5 checksum in file and then verify it.

$ md5sum /home/mandeep/test/test.cpp > checkmd5.md5

It will store the MD5 checksum for test.cpp in file checkmd5.md5

$ md5sum -c checkmd5.md5

It will verify the contents of file

Output :

/home/mandeep/test/test.cpp: OK

After changing the contents of file checkmd5.md5, the output will be :

/home/mandeep/test/test.cpp: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

Set Default Editor to nano

Ubuntu 20.04

เมื่อสั่ง crontab -e บน Ubuntu เป็นครั้งแรก จะมีให้เลือกว่าจะเปิดด้วย editor อะไร (และจะกำหนดเป็น default editor)

ถ้าต้องการเปลี่ยน default editor ใหม่ใช้คำสั่ง

$ select-editor

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.tiny
  3. /bin/ed

Choose 1-3 [1]:

CentOS 7

เมื่อสั่ง crontab -e บน CentOS จะเปิดด้วย vi

ถ้าต้องการเปลี่ยน default editor (เฉพาะ user) ให้แก้ไขไฟล์ ~/.bash_profile

export EDITOR="nano"

จากนั้นใช้คำสั่ง source

$ source .bash_profile

สั่ง crontab -e ก็จะเปิดด้วย nano ละ


lsb_release – print distribution-specific information

lsb_release [options]

The lsb_release command provides certain LSB (Linux Standard Base) and distribution-
specific information.
If no options are given, the -v option is assumed.

~$ lsb_release -d
Description:    Ubuntu 20.04.4 LTS
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal
$ lsb_release -h
Usage: lsb_release [options]

  -h, --help         show this help message and exit
  -v, --version      show LSB modules this system supports
  -i, --id           show distributor ID
  -d, --description  show description of this distribution
  -r, --release      show release number of this distribution
  -c, --codename     show code name of this distribution
  -a, --all          show all of the above information
  -s, --short        show requested information in short format

CentOS 7

$ lsb_release
bash: lsb_release: command not found...


hostnamectl – Control the system hostname

hostnamectl [OPTIONS…] {COMMAND}

hostnamectl may be used to query and change the system hostname and related settings.

CentOS 7

$ hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 4a4569389d534c75b42a67970a52946b
           Boot ID: 70e54525ec054d998fd7c76764860ae3
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.71.1.el7.x86_64
      Architecture: x86-64

Ubuntu 20.04

$ hostnamectl
   Static hostname: ubuntu
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 120a1da3bb7e48fa9a5c6cbf20d6dd1c
           Boot ID: 853400643e464d75a4122a22ca300450
    Virtualization: vmware
  Operating System: Ubuntu 20.04.4 LTS
            Kernel: Linux 5.13.0-52-generic
      Architecture: x86-64


$ hostnamectl
System has not been booted with systemd as init system (PID 1).
Can't operate.
Failed to create bus connection: Host is down


21.7 uptime: Print system uptime and load

uptime prints the current time, the system’s uptime, the number of logged-in users and the current load average.

$ uptime
 10:28:31 up 10 days, 20 min,  1 user,  load average: 0.40, 0.13, 0.46
$ uptime --help

 uptime [options]

 -p, --pretty   show uptime in pretty format
 -h, --help     display this help and exit
 -s, --since    system up since
 -V, --version  output version information and exit

For more details see uptime(1).
$ uptime -p
up 1 weeks, 2 days, 19 minutes

$ uptime -s
2022-07-05 10:01:02

$ uptime -V
uptime from procps-ng 3.3.10