Distributing files with Fsniper
Move It!

© Tetastock, Fotolia.com
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
- Fsniper download: http://projects.l3ib.org/trac/fsniper
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
News
-
Mageia 9 Beta 2 is Ready for Testing
The latest beta of the popular Mageia distribution now includes the latest kernel and plenty of updated applications.
-
KDE Plasma 6 Looks to Bring Basic HDR Support
The KWin piece of KDE Plasma now has HDR support and color management geared for the 6.0 release.
-
Bodhi Linux 7.0 Beta Ready for Testing
The latest iteration of the Bohdi Linux distribution is now available for those who want to experience what's in store and for testing purposes.
-
Changes Coming to Ubuntu PPA Usage
The way you manage Personal Package Archives will be changing with the release of Ubuntu 23.10.
-
AlmaLinux 9.2 Now Available for Download
AlmaLinux has been released and provides a free alternative to upstream Red Hat Enterprise Linux.
-
An Immutable Version of Fedora Is Under Consideration
For anyone who's a fan of using immutable versions of Linux, the Fedora team is currently considering adding a new spin called Fedora Onyx.
-
New Release of Br OS Includes ChatGPT Integration
Br OS 23.04 is now available and is geared specifically toward web content creation.
-
Command-Line Only Peropesis 2.1 Available Now
The latest iteration of Peropesis has been released with plenty of updates and introduces new software development tools.
-
TUXEDO Computers Announces InfinityBook Pro 14
With the new generation of their popular InfinityBook Pro 14, TUXEDO upgrades its ultra-mobile, powerful business laptop with some impressive specs.
-
Linux Kernel 6.3 Release Includes Interesting Features
Although it's not a Long Term Release candidate, Linux 6.3 includes features that will benefit end users.