Platform-independent toolkit – Swiss File Knife
Individual
In the previous examples, sfk list
took into account all the files from the Music
directory; however, you can restrict the output to certain files. For example, the command
sfk list Music .wav .mp3
only lists music files that have the .wav
or .mp3
extension. A colon lets you exclude files. For example,
sfk list Music :.wav
lists all files without a .wav
extension.
You need to replace the shell wildcards *
and ?
in SFK with \*
and \?
– otherwise you run the risk that the shell will interpret these characters rather than sfk
. Instead of \*
you can also type %
. Thus, the command
sfk list Music .mp3%
displays all the files that end with .mp3
(Figure 3).
If necessary, you can combine several selection criteria. For example, the following command will list all .mp3
files whose file names do not contain the string Bubble
:
$ sfk list Music %.mp3 :%Bubble%
The selection criteria always occur at the end of the entire command and refer to the specified directory (e.g., Music
in this example). However, SFK can also show files from multiple directories with the use of the -dir
parameter:
$sfk list -dir Music Downloads
Now the list includes all the files from the Music
and Downloads
subdirectories. To limit the output to certain files, enter the selection criteria after the -file
parameter.
The following example thus retrieves all MP3 files from Music
and Downloads
that do not contain the Bubble
string in the file name:
$ sfk list -dir Music Downloads -file %.mp3 :%Bubble%
Such commands are not useful just to trim music collections: Programmers can use them to collect Makefiles
from all subdirectories.
Double or Nothing
SFK's true strength is revealed in more complex operations. For example, the sfk deblank Music/
command removes all spaces from file names in the Music
directory. When you call it, however, nothing initially happens: As shown in Figure 4, SFK only outputs a preview of the files that you are trying to rename. To make the actual changes, you add the -yes
parameter:
$ sfk deblank -yes Music/
By default, the tool removes the spaces in both file and directory names. To limit the changes to files, you need to use the select
command to select all files and run the deblank
function against the selection:
$ sfk select Music/ +deblank -yes
The plus sign to the left of deblank
tells SFK to concatenate the select
and deblank
commands. The file list prepared by select
in this case receives the command specified after the plus sign – deblank
in this case.
In addition to space characters, SFK can also detect and eliminate duplicates. It does not rely purely on the file name, but always compares the contents of the files. The following command returns the names of all the files in both the Music
and the Download
directories (see Figure 5):
$ sfk dupfind -dir Music Downloads
To delete the duplicate files, append the +delete
function (or for short, +del
). SFK first only shows you what files it would remove. To confirm the deletion, append the -yes
parameter, as for deblank
:
$ sfk dupfind -dir Music Downloads +del -yes
Unlike list
, the order of the directories plays an important role in dupfind
: SFK assumes that the original files reside in Music
and that the files in the directories listed later are duplicates.
Text Mangle
Text files are another focus of SFK. If you often exchange text documents between Windows and Linux, you will appreciate, for example, the lf-to-crlf
and crlf-to-lf
commands, which convert between the different line breaks used in files on the two systems.
Programmers can use detab
to convert the tabs in a text file into space characters; entab
works the other way around. SFK also offers a powerful search-and-replace function. The following command returns all rows from the server.log
file that start with Error
:
$ sfk filter server.log "-ls+Error"
-ls+Error
is not a regular expression, but a SFK-specific pattern. Its structure is explained by the fairly extensive online help that you can retrieve by typing sfk filter
. In the example above, ls
stands for "line start." To display all lines in which Error
occurs, use two plus signs:
$ sfk filter server.log "++Error"
As the name of the command suggests, filter
can be used to manipulate text files. For example, the following command replaces each instance of great
with fantastic
in the great.txt
file:
$ sfk filter great.txt -rep "_great_fantastic_"
Again, the command initially only shows the changes on standard output (Figure 6). To write them to the text file, you need to add the -yes
parameter; alternatively, you could create a new text file (Listing 1).
Listing 1
SFK filter Command
$ sfk filter -yes great.txt -write -to fantastic.txt -rep "_great_fantastic_"
« Previous 1 2 3 Next »
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
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4