Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a shell command when a file is added

I have a folder named images on my linux box. This folder is connected to a website and the admin of the site has the ability to add pictures to this site. However, when a picture is added, I want a command to run resizing all of the pictures a directory.

In short, I want to know how I can make the server run a specific command when a new file is added to a specific location.

like image 809
Cripto Avatar asked Jun 08 '12 22:06

Cripto


People also ask

How do you run the command every time a file is modified in Linux?

entr. entr is a simple and excellent command-line utility for running arbitrary commands when any modifications occur in a given directory. “entr” stands for Event Notify Test Runner.

What is $_ in shell script?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails. $@


1 Answers

Using ghotis work

Here is what I did to get the users free space:

#!/bin/bash

tail -F -n 1 /var/log/vsftpd.log | while read line; do
  if echo "$line" | grep -q 'OK LOGIN:'; then
    pid=$(sed 's/.*\[\([^]]*\)\].*/\1/g' <<< "$line")
    #the operator '<<<' doesnt exist in dash so use bash
    if [[ $pid != *"pid"* ]]; then
      echo -e "Disk 1: Contains Games:\n" > /home/vftp/"$pid"/FreeSpace.txt; df -h /media/Disk1/ >> /home/vftp/"$pid"/FreeSpace.txt
      echo -e "\r\n\r\nIn order to read this properly you need to use a text editor that can read *nix format files" >> /home/vftp/"$pid"/FreeSpace.txt
    fi
echo "checked"
#  awk '{ sub("\r$", ""); print }' /home/vftp/"$pid"/FreeSpace.txt > /home/vftp/"$pid"/FreeSpace.txt
  fi
done
like image 191
Kevin Avatar answered Sep 27 '22 19:09

Kevin