Write a bash script to monitoring directories and files

February 18, 2021

Comments

inotify is a tool in Linux which is used to report when a file system event occurs. Using inotify, you can monitor individual files or directories.

Prerequisites

Make sure you have the inotify tool installed on your system.

How to do it

Create a script called inotify_example.sh:

inotify_example.sh

#! /bin/bash 
folder=~/Desktop/abc 
cdate=$(date +"%Y-%m-%d-%H:%M") 
inotifywait -m -q -e create -r --format '%:e %w%f' $folder | while read file 
do 
	mv ~/Desktop/abc/output.txt ~/Desktop/Old_abc/${cdate}-output.txt 
done

How it works

The inotifywait command is mostly used in shell scripting. The main purpose of the inotify tool is to monitor the directories and new files. It also monitors the changes in the files.

Satish Kumar

Satish Kumar

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.

Related Posts

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

Using Bash to monitor battery life and optimize it

In this article, we will learn about the TLP Linux tool. TLP is a command-line tool; it is used for power management and will optimize the battery life.PrerequisitesBesides having a Terminal open, you need to ensure that you have TLP installed on your system.How to do...

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

ten − 9 =

News & Updates

Join Our Newsletter