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).

Figure 3: The first command returns all MP3 files; the second then suppresses all MP3 files containing the word Bubble.

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:

Figure 4: Some commands – like deblank, here – initially only simulate the action. The tool thus shows you how it would rename the files. The new names are to the right of the arrows.
$ 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):

Figure 5: Here SFK has discovered a file that resides in both the Music and the Downloads folder.
$ 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_"
Figure 6: The filter command here replaces the great string with fantastic; it is not case sensitive and does not act just on single words.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Command Line: File Management

    Do some serious spring cleaning and reorganize your data. The right commands can help you to keep on top of your file and directory management.

  • Free Software Projects

    If you want to convert a bunch of audio files into another format and prefer to steer clear of the command line, the Gnac graphical tool might do the trick. Also, the Gourmet Recipe Manager helps out in the kitchen.

  • Swiss Army Knife

    Pandoc lets you convert files from one markup format to another at the command line.

  • Czkawka

    Czkawka helps find and remove duplicate and obsolete files to free up valuable disk space.

  • Fsniper

    Every day, computers are inundated with hundreds of files. Fsniper welcomes the new arrivals and processes them according to rules that you define.

comments powered by Disqus
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

News