Executing a bash script upon file creation
How about incron? It triggering Commands On File/Directory Changes.
sudo apt-get install incron
Example:
<path> <mask> <command>
Where <path>
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.
<mask>
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 (*)
<command>
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.
Working Example:
$sudo echo spatel > /etc/incron.allow
$sudo echo root > /etc/incron.allow
Start Daemon:
$sudo /etc/init.d/incrond start
Edit incrontab
file
$incrontab -e
/home/spatel IN_CLOSE_WRITE touch /tmp/incrontest-$#
Test it
$touch /home/spatel/alpha
Result:
$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
How to create a shell script which can create a .java file on executing?
Here-docs will expand variables, so
NAME=$1
cat << EOF > $NAME.java
class $NAME{
public static void main(String[] args){
for(int i=0;i<10;i++){
System.out.println("Hello World !!!");
}
}
EOF
How to get rid of creation of =0 file while running a shell script?
The error is in while [ $num_dir>=0 ]
The condition redirects the "output" of $num_dir
to the file =0
.
You need
while [ $num_dir -ge 0 ]
Or the Bash-specific
while [[ $num_dir >= 0 ]]
shell script to create multiple files, incrementing from last file upon next execution
Based on my comment above and your description, you can write a script that will create 10 numbered files (by default) each time it is run, starting with the next available number. As mentioned, rather than just use a raw-unpadded number, it's better for general sorting and listing to use zero-padded numbers, e.g. 001
, 002
, ...
If you just use 1
, 2
, ... then you end up with odd sorting when you reach each power of 10
. Consider the first 12 files numbered 1...12
without padding. a general listing sort would produce:
file1
file11
file12
file2
file3
file4
...
Where 11
and 12
are sorted before 2
. Adding leading zeros with printf -v
avoids the problem.
Taking that into account, and allowing the user to change the prefix (first part of the file name) by giving it as an argument, and also change the number of new files to create by passing the count as the 2nd argument, you could do something like:
#!/bin/bash
prefix="${1:-file_}" ## beginning of filename
number=1 ## start number to look for
ext="txt" ## file extension to add
newcount="${2:-10}" ## count of new files to create
printf -v num "%03d" "$number" ## create 3-digit start number
fname="$prefix$num.$ext" ## form first filename
while [ -e "$fname" ]; do ## while filename exists
number=$((number + 1)) ## increment number
printf -v num "%03d" "$number" ## form 3-digit number
fname="$prefix$num.$ext" ## form filename
done
while ((newcount--)); do ## loop newcount times
touch "$fname" ## create filename
((! newcount)) && break; ## newcount 0, break (optional)
number=$((number + 1)) ## increment number
printf -v num "%03d" "$number" ## form 3-digit number
fname="$prefix$num.$ext" ## form filename
done
Running the script without arguments will create the first 10 files, file_001.txt
- file_010.txt
. Run a second time, it would create 10 more files file_011.txt
to file_020.txt
.
To create a new group of 5 files with the prefix of list_
, you would do:
bash scriptname list_ 5
Which would result in the 5 files list_001.txt
to list_005.txt
. Running again with the same options would create list_006.txt
to list_010.txt
.
Since the scheme above with 3 digits is limited to 1000
files max (if you include 000
), there isn't a big need to get the number from the last file written (bash can count to 1000
quite fast). However, if you used 7-digits, for 10 million files, then you would want to parse the last number with ls -1 | tail -n 1
(or version sort and choose the last file). Something like the following would do:
number=$(ls -1 "$prefix"* | tail -n 1 | grep -o '[1-9][0-9]*')
(note: that is ls -(one)
not ls -(ell)
)
Let me know if that is what you are looking for.
How to create and run a bash file?
As you have two different OS setup, I will split my answer in two parts.
First: Windows
Windows does not have a Bash interpreter, nor the afconvert
program the code above is trying tu run. Your best bet will be to use Cygwin to install a Unix console on your Windows. Also I don't know, where you could get afconvert
from.
OSX
OSX does have a console and the afconvert
software (at least my OSX does). You can simply drop the file in a folder and give it a name ending in .sh
. Then you should be able to run it.
How do I execute a bash script in Terminal?
$prompt: /path/to/script
and hit enter. Note you need to make sure the script has execute permissions.
Bash Script to execute linux file
for i in `seq 0 10`
do
./bomb filename$i.txt
done
The above code will run from filename0 to filename10
Related Topics
How to Find Out What Group a Given User Has
Postgresql Database Default Location on Linux
How to Untar a Tar.Bz File in Unix
What's the Difference Between Tempfile and Mktemp
Count Occurrences of a Char in Plain Text File
Randomly Pick Lines from a File Without Slurping It with Unix
Attach to a Processes Output for Viewing
Fastest Way to Tell If Two Files Have the Same Contents in Unix/Linux
Bash: Add String to the End of the File Without Line Break
Stripping Single and Double Quotes in a String Using Bash/Standard Linux Commands Only
Understanding Bash Short-Circuiting
Can't Su to User Jenkins After Installing Jenkins
Crontab Day of the Week Syntax
Doesn't Sh Support Process Substitution <(...)
In Order to Write Pci Ethernet Driver. How to Implement Mmap in the Pci Ethernet Driver
Whiptail: How to Redirect Output to Environment Variable
How to Programmatically Create Videos
Run a Shell Script from Docker-Compose Command, Inside the Container