Generic shell scripting tricks

Some might be POSIX/portable, some not. I am too old for that crap.

<< Back to wiki homepage

Table of contents:

Get last argument given

echo "${@: -1}"


Watch files created in a folder

inotifywait -m ${YOUR_FOLDER} -e create -e moved_to

And if you'd like to process them e.g. cat or copy etc.

inotifywait -m ${YOUR_FOLDER} -e create -e moved_to |  while read directory action file; do cat ${YOUR_FOLDER}/$file ; done


Check if stdin is empty or not

if [ ! -t 0 ]; then
    # there is stuff in stdin

Catch and use stdin

2 ways:

while IFS= read -r line; do
    printf '%s\n' "$line" >> /whatever/you/want/to/do

or let cat collect it for you:

stuff="`printf '%s\n' "$data"`"


Find the folders which does not contain any specific file

find ${base_dir} -mindepth 1 -maxdepth 1 -type d '!' -exec test -e "{}/whatever_file.txt" ';' -print

Of course adjust the levels etc.