0

How would one go about making a script to first edit a newly found file within a specific directory, and then upload it through incron/python? I'm a bit confused as how to specify the filename as a string in the python script.

incrontab -e :

/var/test IN_CREATE /var/pythoncode/code.py

python code:

s = open("confused.txt").read()
s = s.replace("string1", "string2")
f = open("confused.txt", 'w')
f.write(s)
f.close()

Essentially, I am trying to have the incron service find any new file that is found within /var/test folder , and then execute python code to look for and replace a string within the new file found in /var/test. However, I am uncertain how to approach the "confused.txt" filename string, since each file found with the /var/test will have a dynamic name.

icehac
  • 147
  • 5
  • 14

1 Answers1

0

Here's a workaround assuming you are well verse with scripting -

Step 1 - Change the files in a directory (change all and get names with $FILE in ls).

Step 2 - Store the last file name in a variable $LAST_FILE

Step 3 - Run a WHILE loop to find files newer than $LAST_FILE -

find /var/test/ -newer $LAST_FILE

OR

find /var/test/ -type f -newer $LAST_FILE

OR

find /var/test/ -type f -iname "*.txt" -newer $LAST_FILE

This will list the files that are newer than last file you edited. So, parse this list into your "change file" script.

pwn.star
  • 85
  • 1
  • 1
  • 8