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.
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.
News
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.