Linux open file on create

Executing a bash script upon file creation

I am looking write a small bash script to, when launched, watch a directory for any newly created files. If a new file appears, I want its presence to trigger a second script to run. I see this being used to trigger the compression recently digitized video, and add it to a log of ingested footage. Currently my code looks like this:

#!/bin/sh ##VIDSTAT is a global variable coming from a parent script. ##proj is the ingestion directory coming from a parent script proj=$1 dir="/home/$USER/data/movies/$proj" dirlist=$(ls $dir) while < $VIDSTAT -eq 1 >: do for mov in $dirlist do if [ "$(( $(date +"%s") - $(stat -c "%Y" $mov) ))" -lt "5" ] then ~/bin/compressNlog.sh $mov fi done done 

Is there an easier/cleaner/less memory intensive way to do this? EDIT I will be changing the ingestion directory per capture session. I have adjusted the code accordingly

Be careful. The script could run before the application is finished writing the file, and it will compress an incomplete file.

3 Answers 3

How about incron? It triggering Commands On File/Directory Changes.

sudo apt-get install incron 

Where can be a directory (meaning the directory and/or the files directly in that directory (not files in subdirectories of that directory!) are watched) or a file.

can be one of the following:

IN_ACCESS File was accessed (read) (*) IN_ATTRIB Metadata changed (permissions, timestamps, extended attributes, etc.) (*) IN_CLOSE_WRITE File opened for writing was closed (*) IN_CLOSE_NOWRITE File not opened for writing was closed (*) IN_CREATE File/directory created in watched directory (*) IN_DELETE File/directory deleted from watched directory (*) IN_DELETE_SELF Watched file/directory was itself deleted IN_MODIFY File was modified (*) IN_MOVE_SELF Watched file/directory was itself moved IN_MOVED_FROM File moved out of watched directory (*) IN_MOVED_TO File moved into watched directory (*) IN_OPEN File was opened (*) 

is the command that should be run when the event occurs. The following wildards may be used inside the command specification:

$$ dollar sign $@ watched filesystem path (see above) $# event-related file name $% event flags (textually) $& event flags (numerically) 

If you watch a directory, then $@ holds the directory path and $# the file that triggered the event. If you watch a file, then $@ holds the complete path to the file and $# is empty.

Читайте также:  Unzip files in linux terminal

Working Example:

$sudo echo spatel > /etc/incron.allow $sudo echo root > /etc/incron.allow 
$sudo /etc/init.d/incrond start 
$incrontab -e /home/spatel IN_CLOSE_WRITE touch /tmp/incrontest-$# 
$ls -l /tmp/*alpha* -rw-r--r-- 1 spatel spatel 0 Feb 4 12:32 /tmp/incrontest-alpha 

Notes: In Ubuntu you need to activate inotify at boot time. Please add following line in Grub menu.lst file:

kernel /boot/vmlinuz-2.6.26-1-686 root=/dev/sda1 ro inotify=yes 

Источник

Create and open file with one command?

I want to create a text file and open it in a text editor with a single command. I know touch file.txt creates a file and open file.txt opens the file, but is there a way to do this with a single command?

Which text editor gives you difficulty? Is there an error message? The editors with which I am acquainted ( ed , nano , vi , vim , emacs , teco , LibreOffice , . ) all regard a non-existent file as empty, and create the file upon exit (or when one writes the buffer). If your editor doesn’t do that, it may be a bug.

Of note, I believe this is rather unique to MacOS; Linux does not have an open(1) man page, nor is it recognized as a command in Linux.

7 Answers 7

You could use touch myfile.txt; open myfile.txt . If this is something you’ll be doing frequently, you could create an alias for it.

How would you use an alias to create a file, and then open it? I don’t think that would be the most robust (if even possible) way of handling this. If so, an example would definitely be awesome for people to see.

alias touch_open=’touch myfile.txt; open myfile.txt’ would work to create the Alias. Alternatively, could write a small function that would take in a parameter, ie. function touch_open < touch "$@"; open "$@"; >and then run that as touch_open myfile.txt

Читайте также:  Linux сервер или сервер windows

If you are looking for some logic for this, ie. create a file and open if possible you could use something like this:

touch testFile && vi testFile 

Here testFile is created with touch. The && part says if the creation was successful then open it with vi.

Also, you could just create a new file with something like vi, while opening it at the same time (this assumes you save the file after editing it).

That entirely depends on your text editor. Most will allow you to simply open a non-existent file, and assume you want it created on save.

You could also use a function if that is somehow not possible for you:

cropen() < if ! [ "$1" ]; then echo "need a file!" >&2 return 1 fi : > "$1" && open "$1" > 

You can call it as cropen file .

For vim you need to know the hot-keys.

i - Insert text in the file esc - return to the command-input-mode :w - Write (save) :q - Quit (exit) 

If you want a GUI type: (Works for ubuntu and other distros)

Like everyone is telling you, this depends on your editor. And if you don’t like the way your editor handles this, that can probably be configured, as well.

With Emacs, emacs new_file.txt (for a nonexistent new_file.txt in the current directory) will bring up Emacs and display a buffer that refers to a new_file.txt . However, until you save, there will be no such file. If you don’t do anything and quit, no file will have been created.

While this is certainly one way, I think an even better way is to always have an instance of Emacs running. So, whenever you feel the need for a new file, go to Emacs, hit C-x C-f (or M-x find-file RET ), then write the path to the nonexistent file, and you’re at the same point as after the CLI command (above, in the second paragraph).

Читайте также:  Умный дом linux сервер умного дома

Источник

Оцените статью
Adblock
detector