Distributing files with Fsniper

Move It!

© Tetastock, Fotolia.com

© Tetastock, Fotolia.com

Article from Issue 102/2009
Author(s):

Every day, computers are inundated with hundreds of files. Fsniper welcomes the new arrivals and processes them according to rules that you define.

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)$/ { ...

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

# Sample configuration for Fsniper
watch  {
  # monitor '~/downloads/print'
  ~/downloads/print  {
    # monitor both subdirectories
    recurse = true
    # look for '.pdf' and '.ps' type files
    /\.(pdf|ps)$/  {
      # send files with full path name to
      # the standard printer via 'lp':
      handler = lp %%
    }
  }
  # monitor '~/downloads/torrents'
  ~/downloads/torrents  {
    # watch for Torrent files (MIME type)
    application/x-bittorrent  {
      # Prompt:  'yes' = move file to '~/torrents'
      # 'no' = do nothing
      handler = if zenity --text "Found new Torrent file (%f). Add to Torrent directory?" --question; then mv %% ~/torrents; fi
    }
  }
  ~/downloads/music  {
    recurse = true
    # MIME type: all audio file formats
    audio/*  {
      # move file to '/home/shares'
      handler = mv %% /home/shares
    }

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).

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.

Infos

  1. Fsniper download: http://projects.l3ib.org/trac/fsniper

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News