Distributing files with Fsniper
Move It!
![© Tetastock, Fotolia.com © Tetastock, Fotolia.com](/var/linux_magazin/storage/images/linux-magazine.com/issues/2009/102/move-it/fo_9393330_speed-bully_tetastock.png/427022-1-eng-US/fo_9393330_speed-bully_Tetastock.png_medium.png)
© 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
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](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.