2009.05_Move It-Distributing Files with Fsniper.pdf

(451 KB) Pobierz
Move It! - Distributing files with Fsniper - Linux Magazine
Distributing files with Fsniper
Move It!
Every day, computers are inundated with hundreds of files. Fsniper welcomes the new arrivals and processes
them according to rules that you define.
By Heike Jurzik
Tetastock, Fotolia.com
Fsniper [1] is a handy tool that watches for new or modified files and executes commands on the basis of file
name criteria. The user configures the rule set that identifies a file and specifies the details of the response,
which can be either a short command or a whole shell script. The tool can also respond to MIME types (e.g.,
audio/mpeg for MP3 or image/jpeg for JPEG files), shell wildcards, or regular expressions.
To install Fsniper, download the source code from the project homepage, then unpack the Fsniper archive and
run the usual three commands:
./configure
make
make install
If you prefer to install prebuilt packages, Debian and RPM packages are available.
The command center for Fsniper is the configuration file in the hidden .config folder below your home
directory. Before you launch Fsniper for the first time, manually create the ~/.config/fsniper/config file. To
reflect your environment, you can copy the example.conf file from the source code folder and modify it.
The setup file contains the watch command, followed by any number of nested blocks enclosed in braces. The
nested blocks associate files with commands. If a file name matches the criteria defined in the block, Fsniper
executes the command. For example,
watch {
~/downloads {
*.pdf {
handler = lp %%
}
}
}
tells Fsniper to monitor the downloads folder below the home directory. When a PDF file arrives, Fsniper
automatically runs the lp command to send the file to the printer. ( %% stands for the file name with the full
path.) For PostScript files, you don't need another line, just use the following regular expression to match both
.pdf and .ps files:
/\.(pdf|ps)$/ { ...
Move It!
1
593110164.002.png 593110164.003.png
Regular expressions are enclosed in slashes, and don't forget to place the backslash in front of the period so
Fsniper will treat the period as a literal character. The parentheses are for the two file extensions, and the
dollar sign denotes the end of line. Fsniper is finicky about the order of the instructions: The program works
through them sequentially from the top down.
As mentioned earlier, Fsniper can also process MIME types. The second block of instructions in Listing 1
waits for torrent files. Fsniper calls the Zenity user interface tool to handle these files and sends a Yes/No
prompt to the user. If the user clicks Cancel , nothing else happens.
Clicking OK moves the file to the ~/torrents folder, where it is picked up by a BitTorrent client and the file
transfer begins. The %f in the text for the dialog box outputs the file name, unlike %% , which outputs the file
name and path. To launch Fsniper, press Alt+F2 to pop up a quick launcher terminal, then type the program
name. After modifying the configuration, you need to terminate the tool - killall fsniper , for example - and
then relaunch.
Fsniper uses the ~/.config/fsniper/log file to store a fairly terse log. For more details, you need to launch
Fsniper with the --verbose command-line option. Then, you can view the log file at the command line with,
say, tail -f . The --log-to-stdout parameter tells Fsniper to output the log directly to the screen.
Listing 1: Monitoring Directories with Fsniper
01 # Sample configuration for Fsniper
02 watch {
03 # monitor '~/downloads/print'
04 ~/downloads/print {
05 # monitor both subdirectories
06 recurse = true
07 # look for '.pdf' and '.ps' type files
08 /\.(pdf|ps)$/ {
09 # send files with full path name to
10 # the standard printer via 'lp':
11 handler = lp %%
12 }
13 }
14
15 # monitor '~/downloads/torrents'
16 ~/downloads/torrents {
17 # watch for Torrent files (MIME type)
18 application/x-bittorrent {
19 # Prompt: 'yes' = move file to '~/torrents'
20 # 'no' = do nothing
21 handler = if zenity --text "Found new Torrent file (%f). Add to Torrent directory?" --question; then mv %% ~/torrents; fi
22 }
23 }
24
25 ~/downloads/music {
26 recurse = true
27 # MIME type: all audio file formats
28 audio/* {
29 # move file to '/home/shares'
30 handler = mv %% /home/shares
31 }
Infinite Options
Fsniper can assist with a wide ranges of common tasks. For example, if you share a home network with other
users, you can tell Fsniper to move any arriving audio files to the network (Listing 1, last block of
instructions), thus sharing new music directly. Or, to transfer files to remote computers, for example, you
could even add special rules that call Rsync or Scp.
Fsniper's handler can handle delays and restart unfinished data transfer operations (e.g., caused by an
unreachable remote computer).
Move It!
2
593110164.004.png 593110164.005.png
To restart an unfinished operation, just insert two lines in front of the first ( watch ) block to tell Fsniper how
long to wait before retrying and how many retries to attempt:
delay_time = 300
delay_repeats = 10
Firefox downloads also need special attention. The Firefox browser creates temporary files with a .part
extension, and it does not write the file name until the download has successfully completed. If you check out
Fsniper's source code directory, you will find a contrib folder with a script that offers a potential workaround
for this issue.
INFO
Move It!
3
593110164.001.png
Zgłoś jeśli naruszono regulamin