This is a mirror of the GitLab repo! https://gitlab.com/Lynnesbian/fif
Find a file
2023-03-15 09:59:12 +10:00
.idea cargo update, minor changes 2021-12-17 10:05:04 +10:00
.reuse updated copyright years, fixed reuse compliance 2022-01-23 03:10:17 +10:00
.vscode visual studio code 2022-09-19 01:59:48 +10:00
doc update dependencies 2023-01-02 07:36:11 +10:00
LICENSES cargo update, add GPL license text 2022-02-03 15:51:20 +10:00
src clippy, format 2023-03-15 06:51:29 +10:00
.drone.yml updated copyright years, fixed reuse compliance 2022-01-23 03:10:17 +10:00
.gitattributes updated copyright years, fixed reuse compliance 2022-01-23 03:10:17 +10:00
.gitignore update and fix dependencies 2022-09-04 12:47:17 +10:00
.gitlab-ci.yml REALLY update gitlab CI 2023-03-15 09:59:12 +10:00
.kateproject add .kateproject file for project indexing 2021-07-21 21:16:32 +10:00
.mailmap consider lynne/lynnesbian to be the same author 2021-04-21 16:35:30 +10:00
build.rs clippy cleanup 2022-09-04 13:48:54 +10:00
Cargo.lock release v0.7.0 🥳 2023-03-15 06:53:05 +10:00
Cargo.toml release v0.7.0 🥳 2023-03-15 06:53:05 +10:00
CHANGELOG.md release v0.7.0 🥳 2023-03-15 06:53:05 +10:00
clippy.sh updated copyright years, fixed reuse compliance 2022-01-23 03:10:17 +10:00
clippy.toml fix value parsing, bump MSRV 2023-03-15 06:42:44 +10:00
Cross.toml updated copyright years, fixed reuse compliance 2022-01-23 03:10:17 +10:00
LICENSE better error handling and pattern matching, added license 2021-02-05 15:57:21 +10:00
README.md update all dependencies, remove deprecated code 2022-09-04 13:41:41 +10:00
rustfmt.toml updated copyright years, fixed reuse compliance 2022-01-23 03:10:17 +10:00
test.py python formatting 2022-01-24 00:16:06 +10:00

fif

A screenshot demonstrating fif's ability to detect the correct file extensions for a few files.

Version Minimum Supported Rust Version License Build status Unsafe forbidden Dependency versions REUSE status

A command-line tool for detecting and optionally correcting files with incorrect extensions.

fif recursively scans the given directory and outputs a shell script to fix the name of any files with incorrect extensions. By default, fif will scan all non-hidden files in the given directory, and will ignore symlinks.

As fif prints a shell script to stdout rather than acting on the files directly, you may wish to redirect its output to a file, e.g. fif ~/Documents > output.sh. You can also pipe the output directly into your shell, e.g. fif ~/Documents | bash, although this is not recommended - you should look over fif's output and verify for yourself that it won't do anything that will give you a headache before running it.

Features

  • Cross platform
  • Multi-threaded
  • Configurable

Building

fif can be built, installed, and tested with Cargo, as with most rust programs:

git clone https://gitlab.com/Lynnesbian/fif/
cd fif
# run tests (optional)
cargo test --locked
# build fif with its default feature set
cargo build --locked --release

The --locked flag ensures that Cargo uses the dependency versions specified in the lock file, and the --release flag builds fif with release optimisations enabled -- this takes longer, but produces a much faster binary.

Installing

# install the fif crate (to ~/.cargo/bin or %USERPROFILE%\.cargo\bin by default)
cargo install --locked fif

To update, simply re-run the install command, or use a tool like cargo-update , which can update crates installed via cargo install.

On macOS, fif can be installed through MacPorts:

sudo port selfupdate
sudo port install fif

Cargo Features

fif supports using infer or xdg-mime as its backend for looking up file types. By default, xdg-mime will be used on *nix systems (Linux, macOS, *BSD, etc.), and infer on all other systems.

xdg-mime should work on any *nix system with the Shared MIME Info library installed (consult your package manager), although I've only tested it on Linux and FreeBSD. infer should work on any system, but it supports far fewer file types than xdg-mime does, thanks to the excellent Shared MIME Info library.

You can override the default backend for your system at compile time like so:

# xdg-mime
cargo install fif --locked --features=xdg-mime-backend
# infer
cargo install fif --locked --features=infer-backend

It is also possible to get a more minimal build by installing without default features:

cargo install fif --locked --no-default-features

This will disable some non-essential but nice to have features, like multi-threading support.

For more info on fif's compile-time features, see the wiki.

Usage

See fif --help for more.

The basics

The simplest way to use fif looks like this:

fif ~/Downloads

This command will scan all non-hidden files in your ~/Downloads directory.

The -e and -E flags can be used to specify individual extensions and sets of extensions to scan, respectively:

# only scan files with the extensions .jpeg, .jpg, .zip, and .docx
fif -e jpeg,jpg,zip,docx ~/Documents
# only scan files with "image extensions" - .jpg, .png, .gif, .webp...
fif -E images ~/Pictures
# scan .zip files, videos, and audio
fif -e zip -E videos,audio ~/Downloads

Both -e and -E have equivalent -x and -X flags that exclude the given extensions rather than including them:

# scan everything except filenames ending in .zip
fif -x zip ~/Downloads
# scan all files with image extensions, but not .jpg and .jpeg files
fif -x jpg,jpeg -E images ~/Pictures
# scan everything except text and system files
fif -X text,system ~/.local/share

Output

By default, fif will output a bash script (or PowerShell script on Windows) that can be used to fix all the files it found with incorrect file extensions.

You might find it useful to output this script to a file (rather than to stdout):

fif ~/Documents > output.sh

You can also manually specify an output format to use:

fif -O powershell ~/Documents > output.ps1

The --fix flag

If, however, you'd rather have fif fix the misnamed files itself, rather than relying on a shell script, you can use the --fix flag:

# rename any misnamed .jpg files in the user's pictures directory
fif ~/Pictures --fix -e jpg
# fix any misnamed files found in the user's downloads
fif ~/Downloads --fix

By default, fif will avoid taking any potentially destructive actions (renaming a file to a name that already exists). This behaviour can be disabled with the --overwrite flag, which will instead prompt you to overwrite the existing file. fif will also prompt you to retry on errors.

For more information on how the --fix flag works, and how it behaves in conjunction with the --overwrite flag and --prompt parameter, see the corresponding wiki page.

Logging

By default, fif will log any info, warnings, and errors encountered during execution. This can be changed with the -v flag:

# also log debug info
fif -v ~/Downloads
# ...and trace info
fif -vv ~/Downloads

You can also reduce the level of logging with the -q flag:

# don't show info
fif -q ~/Downloads
# ...or warnings
fif -qq ~/Downloads
# ...or even errors!
fif -qqq ~/Downloads

The verbosity of the logging can also be modified by setting the environment variable FIF_LOG or RUST_LOG to off, trace, debug, info, warn, or error. Values set by FIF_LOG override RUST_LOG, and both override the -v and -q flags.

For example:

# show all levels except trace
FIF_LOG=debug fif ~/Downloads
# only show errors
FIF_LOG=error fif ~/Downloads

The five logging levels are used as follows:

Level Description Example
error Errors that cause fif to stop running fif was unable to open the provided directory
warn Warnings that don't cause fif to stop running fif was unable to determine the MIME type of a given file
info Information pertaining to fif's status The provided directory was scanned without issue, and no files are in need of renaming
debug Debug information - usually not important to end users The list of extensions fif will consider
trace Trace info - usually not important to end users "Found 15 items to check", "Scan successful", etc.

For a more comprehensive explanation of all of fif's parameters and how to use them, run fif --help (or fif -h for a more concise overview).

Version policy

fif adheres to the semantic versioning principles. While fif remains at version 0.x, the version number will be updated as follows:

  • The MAJOR version will be bumped to 1 when I believe fif to be "feature complete".
  • The MINOR version will be bumped whenever I add a fairly important feature to fif (in the past, this has been bumped when adding the ability to exclude extensions, and when fif gained the ability to output a bash script rather than a list of invalid filenames). The MINOR version will also be bumped when increasing the MSRV.
  • The PATCH version will be bumped in all other cases, including minor feature additions (in the past, this has occurred when adding features such as more output formats and the "ignore unknown extensions" flag).

If/when fif hits version 1.0, these rules will likely remain the same as they are now.

Code style

fif is formatted with rustfmt using a nightly toolchain, specifically with the command cargo +nightly fmt. Tabs are used for indentation, and are assumed to be two spaces wide. Lines are 120 characters wide. Braces are placed on the same line ("OTBS" format), except in the case of where clauses in generic parameters.

For more detailed information on the formatting rules used by this project, see the configured options in rustfmt.toml.

Additional credits

Some of the code for correctly handling files with multiple valid extensions (particularly in the case of the Portable Executable format) comes from Czkawka

License

Copyright (C) 2021 Lynnesbian

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.