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)