قالب وردپرس درنا توس
Home / Tips and Tricks / How to run a Linux command when changing a file set – CloudSavvy IT

How to run a Linux command when changing a file set – CloudSavvy IT



Domino effect.
Shutterstock / Gecko Studio

Learn how to run Linux commands when a file in a monitored set of files changes and when new files are created. We show you how to use this flexible tool.

The command

You can set commands to run at specific times in Linux using cron. It is simple, reliable and flexible. But what if you need to have commands executed every time a file is modified, or if a new file is added to a directory? It is also possible, and there is more than one way to do it. The entr command is a refreshingly simple way to achieve that function.

Some systems, such as the Hugo static site generator, have a feature that automatically rebuilds your site if a file changes or a new file is added to the site. The small function makes a significant difference to your workflow. With Hugo, you can have your development version of the website in your browser when editing the files that make up your website. Every time you save your changes or create a file, the website is rebuilt in milliseconds and the changes are pushed to your browser. Immediate feedback is fantastic.

The entr command provides that type of capacity to any set of files. The command that is started when a change is detected is arbitrary. It can be a standard Linux command, an alias, a shell function or a script.

Install entr

To install entr Use this command on Ubuntu:

sudo apt-get install entr

sudo apt-get install entr in a terminal window

To install entr on Fedora, write:

sudo dnf install entr

sudo dnf install entr in a terminal window

In Manjaro, the command is:

sudo pacman -Syu entr

sudo pacman -Syu entr in a terminal window

A simple example

We use the touch command to create a text file called “example.txt” and tell entr to monitor that file.

The entr the command accepts filenames through which it should monitor STDIN and accepts the command to be run as command line parameters.

The easiest way to send a file name to entr is to use ls to list it and stir it in entr. When the file changes, entr will be launched wc command to count lines, words and characters in the file.

The /_ parameter is a placeholder for the filename of the file that was sent to entr. If a group of files is sent to entr, For example *.txt , /_ parameter would be replaced by the last file to be changed.

touch example.txt
ls example.txt | entr wc /_

ls example.txt |  entr wc / _ in a terminal window

Each time the changes are saved in the file, entr launches wc . To stay entr, hit q, or Ctrl+c .

RELATED: What are stdin, stdout and stderr on Linux?

Practical uses

Now that we’ve seen entr in action and you understand the principle, let’s look at some more useful scenarios.

We do entr monitor the files in the current directory. If any of the files change, we want it entr to fire make for us to rebuild our project. We will chain another command, make test, to run some tests on the new building. AND operator && only starts the second command if the first is completed without problems.

The -s The (shell) option ensures that the interpreter used is the one defined in the SHELL environment variable. This ensures entr receives the output code from the first make command.

ls | entr -s 'make && make test'

Use ls command to send the list of all file names in the directory to entr is a bit clumsy. You may have files in the directory that are part of the project but do not actually build files. Maybe it’s design notes or a to-do list. You do not need to have your project rebuilt every time you change one of these non-code files.

We can refine the command so that it only monitors the appropriate source code files. This command monitors the C source code and the header files.

ls *.[ch] | entr -s 'make && make test'

Of course, we are not limited to launch make. You may have a custom build script that you prefer to use.

ls *.[ch] | entr script.sh

Utilize Git

The entr the command is not Git-ware, but we can still use some of Git’s functions to smooth our workflow.

The git ls-file the command will list the files contained in the Gits index and working source tree, taking into account the exceptions defined in your “.gitignore” file. It is a complete list of the files we are interested in, so let’s use it as a source for the files to monitor.

git ls-files | entr script.sh

The ls-files command accepts command line options. These can be used to further filter or enhance the file list returned by Git:

  • -c: View cached files.
  • -d: View deleted files.
  • -m: View modified files.
  • -The: View untracked files.

The --exclude-standard the alternative is a short way of saying Git to ignore files that match the entry in “.git / info / exclude”, local “.gitignore” and global “.gitignore” files.

git ls-files -cdmo --exclude-standard | entr 'make && make test'

It will capture many eventualities, but it will not be able to create a new file. And the new file is not in Git. We can still handle that situation with the help of a little Bash script and a function of entr.

The -d (directory) options cause entr to monitor the files in a directory and exit if a new file is added. By packaging spring entr command ia while/do loop, whole entr the command line is restarted automatically, and the new file will be picked up and acted upon. Files and directories whose names begin with a period “.Ignored.

while true; do

  { git ls-files; git ls-files . --exclude-standard --others; } | entr -d ./script.sh

done

Restart servers and interpreters

You may be working with an interpreted language like Ruby. Each time your program file changes, you must stop and restart the Ruby interpreter. You can handle these types of use cases with -r (restart) option.

To monitor a program file named “hello_world.rb”, and to stop and restart the Ruby interpreter each time the program file changes, use this command:

ls hello_world.rb | entr -r ruby hello_world.rb

Each time the program file is edited and saved, the Ruby interpreter restarts and the program reloads.

ls hello_world.rb |  entr -r ruby ​​hello_world.rb in a terminal window

The program originally contained:

puts "Hello, World!"

It was edited to say:

puts "Hello, CloudSavvyIT!"

When this was saved, entr restarted the Ruby interpreter and reloaded the program.

The file was edited once more to add the word “reader” and saved to the hard disk.

puts "Hello, CloudSavvyIT readers!"

You can see the three outputs in the screenshot. If you press spacebar, entr will restart the command if you have modified the program file or not.

Not just for programmers

Since the command launched by entr can be anything, including shell scripts, the application entr automating processes is virtually unlimited.

Maybe you have a directory of files that have been dropped into it with secure FTP, or rsync or curl. You can have these files automatically copied elsewhere, or compressed or searched by grabs for the word “error”.

This makes it a great tool for both system administrators and developers.


Source link