This is a collection of bash shell scripts for automated and semi-automated organization and management of large ebook collections. It contains the following tools:
-
organize-ebooks.shis used to automatically organize folders with potentially huge amounts of unorganized ebooks. This is done by renaming the files with proper names and moving them to other folders:- By default it searches the supplied ebook files for ISBNs, downloads the book metadata (author, title, series, publication date, etc.) from online sources like Goodreads, Amazon and Google Books and renames the files according to a specified template.
- If no ISBN is found, the script can optionally search for the ebooks online by their title and author, which are extracted from the filename or file metadata.
- Optionally an additional file that contains all the gathered ebook metadata can be saved together with the renamed book so it can later be used for additional verification, indexing or processing.
- Most ebook types are supported:
.epub,.mobi,.azw,.pdf,.djvu,.chm,.cbr,.cbz,.txt,.lit,.rtf,.doc,.docx,.pdb,.html,.fb2,.lrf,.odt,.prcand potentially others. Even compressed ebooks in arbitrary archive files are supported. For example a.zip,.raror other archive file that contains the.pdfor.htmlchapters of an ebook can be organized without a problem. - Optical character recognition (OCR) can be automatically used for
.pdf,.djvuand image files when no ISBNs were found in them by the fast and straightforward conversion to.txt. This is very useful for scanned ebooks that only contain images or were badly OCR-ed in the first place. - Files are checked for corruption (zero-filled files, broken pdfs, corrupt archive, etc.) and corrupt files can optionally be moved to another folder.
- Non-ebook documents, pamphlets and pamphlet-like documents like saved webpages, short pdfs, etc. can also be detected and optionally moved to another folder.
-
interactive-organizer.shcan be used to interactively and manually organize ebook files quickly. A good use case is the organization of the files that could not be automatically organized by theorganize-ebooks.shscript. It can also be used to semi-automatically verify the organized files by the above script and potentially reorganize some of them:- If
organize-ebooks.shwas called with--keep-metadata, the interactive organizer compares the old filename with the new one and shows suspicious differences between the two. Wrongly renamed files can be interactively renamed with this script. - There is a quick mode that skips files with names that contain the all of the original filename's tokens. Differences due to diacritical marks and truncated words are handled intelligently. A list of allowed differences can be configured and interactively updated while organizing the books.
- The script can restore files back to their original location or move them to one of many different pre-configurable output folders.
- Ebooks can be converted to
.txtand shown withlessdirectly in the current terminal or they can be opened with an external viewer without exiting from the interactive organization. - Books can be semi-automatically renamed by looking up their metadata (by ISBN or title) online.
- If
-
find-isbns.shtries to find valid ISBNs inside a file or instdinif no file was specified. Searching for ISBNs in files uses progressively more resource-intensive methods until some ISBNs are found, see the documentation below for more details. -
convert-to-txt.shconverts the supplied file to a text file. It can optionally also use OCR for.pdf,.djvuand image files. -
rename-calibre-library.shtraverses a calibre library folder and renames all the book files in it by reading their metadata from calibre'smetadata.opffiles. -
split-into-folders.shsplits the supplied ebook files (and the accompanying metadata files if present) into folders with consecutive names that each contain the specified number of files.
All of the tools use a library file lib.sh that has useful functions for building other ebook management scripts. More details for the different script options and parameters can be found in the Usage, options and configuration section.
There are two ways you can install and use the tools in this repository - directly or via docker images.
Since all of the tools are shell scripts, you should be able to use them directly from source in most up-to-date GNU/Linux distributions, as long as you have the needed dependencies installed. They should also be usable on other *nix systems like OS X and *BSD if you have the GNU versions of the dependencies installed or in the Windows Subsystem for Linux.
However, since non-linux systems are officially unsupported and may have unexpected issues, Docker containers are the preferred way to use the scripts in those systems. The docker images may also be easier to use than the bare scripts on non-GNU linux distributions or on older linux distributions like some LTS releases.
To install and use the bare shell scripts, follow these steps:
- Install the dependencies below.
- Make sure that your system has a UTF-8 locale.
- Clone the repository or download a release archive and extract it.
- For convenience, you may want to add the scripts folder to the
PATHenvironment variable.
You need recent versions of:
file,less,bash4.3+ and GNUcoreutils,awk,sedandgrep.- calibre for fetching metadata from online sources, conversion to txt (for ISBN searching) and ebook metadata extraction. Versions 2.84 and above are preferred because of their ability to manually specify from which specific online source we want to fetch metadata. For earlier versions you have to set
ISBN_METADATA_FETCH_ORDERandORGANIZE_WITHOUT_ISBN_SOURCESto empty strings. - p7zip for ISBN searching in ebooks that are in archives.
- Tesseract for running OCR on books - version 4 gives better results even though it's still in alpha. OCR is disabled by default and another engine can be configured if preferred.
- Optionally poppler, catdoc and DjVuLibre can be installed for faster than calibre's conversion of
.pdf,.docand.djvufiles respectively to.txt. - Optionally the Goodreads and WorldCat xISBN calibre plugins can be installed for better metadata fetching.
The scripts are only tested on linux, though they should work on any *nix system that has the needed dependencies. You can install everything needed with this command in Arch Linux:
pacman -S file less bash coreutils gawk sed grep calibre p7zip tesseract tesseract-data-eng python2-lxml poppler catdoc djvulibreNote: you can probably get much better OCR results by using the unstable 4.0 version of Tesseract. It is present in the AUR or you can easily make a package like this yourself.
Here is how to install the packages on Debian (and Debian-based distributions like Ubuntu):
apt-get install file less bash coreutils gawk sed grep calibre p7zip-full tesseract-ocr tesseract-ocr-osd tesseract-ocr-eng python-lxml poppler-utils catdoc djvulibre-binKeep in mind that a lot of debian-based distributions do not have up-to-date packages and the scripts work best when calibre's version is at least 2.84. For earlier versions you have to set ISBN_METADATA_FETCH_ORDER and ORGANIZE_WITHOUT_ISBN_SOURCES to empty strings.
The docker image includes all of the needed dependencies, even the extra calibre plugins. There is an automatically built docker image in the Docker Hub. You can pull it locally with docker pull ebooktools/scripts. You can also easily build the docker image yourself: simply clone this repository (or download the latest release archive and extract it) and then run docker build -t ebooktools/scripts:latest . in the folder.
Here are some Docker-specific usage details:
- You can start a docker container with all the ebook tools by running
docker run -it -v /some/host/folder:/unorganized-books ebooktools/scripts:latest. This will run a bash prompt that has all of the dependencies installed and all of the scripts already in thePATHso all the usage instructions bellow should apply. The contents of the host folder/some/host/folder(the path to the folder on your machine that you want to organize) will be mounted as the/unorganized-booksfolder in the container. - You can use the
-voption ofdocker runmultiple times to mount several host folders in the container. - Consider using the
--rmoption ofdocker runto clean up your containers after you are done with them. - The default container user has an UID of 1000, but you can change it with the
--useroption ofdocker runor by editing theDockerfileand rebuilding it yourself. - You can run specific scripts directly instead of the bash terminal like this:
docker run -it [other-docker-run-options] ebooktools/scripts:latest organize-ebooks.sh [ebook-tools-script-options]
For more Docker details, read the docker documentation and docker run reference specifically.
Scripts that work with multiple files recursively scan the supplied folders and assume that one file is one ebook. Ebooks that consist of multiple files should be compressed in a single file archive. The archive type does not matter, it can be .zip, .rar, .tar, .7z and others - all supported archive types by 7zip are fine.
All of the options documented below can either be passed to the scripts via command-line parameters or via environment variables. Command-line parameters supersede environment variables. Most parameters are not required and if nothing is specified, the default value will be used.
All of these options are part of the common library and may affect some or all of the scripts.
-
-v,--verbose; env. variableVERBOSE; default valuefalseWhether debug messages will be displayed on
stderr. Passing the parameter or changingVERBOSEtotrueand piping thestderrto a file is useful for debugging or keeping a record of exactly what happens without cluttering and overwhelming the normal execution output. -
-d,--dry-run; env. variableDRY_RUN; default valuefalseIf this is enabled, no file rename/move/symlink/etc. operations will actually be executed.
-
-sl,--symlink-only; env. variableSYMLINK_ONLY; default valuefalseInstead of moving the ebook files, create symbolic links to them.
-
-km,--keep-metadata; env. variableKEEP_METADATA; default valuefalseDo not delete the gathered metadata for the organized ebooks, instead save it in an accompanying file together with each renamed book. It is very useful for semi-automatic verification of the organized files with
interactive-organizer.shor for additional verification, indexing or processing at a later date.
-
-i=<value>,--isbn-regex=<value>; env. variableISBN_REGEX; see default value inlib.shThis is the regular expression used to match ISBN-like numbers in the supplied books. It is matched with
grep -P, so look-ahead and look-behind can be used. Also it is purposefully a bit loose (i.e. it can match some non-ISBN numbers), since the found numbers will be checked for validity. Due to unicode handling, the default value is too long for the README, you can find it inlib.sh. -
--isbn-blacklist-regex=<value>; env. variableISBN_BLACKLIST_REGEX; default value^(0123456789|([0-9xX])\2{9})$Any ISBNs that were matched by the
ISBN_REGEXabove and pass the ISBN validation algorithm are normalized and passed through this regular expression. Any ISBNs that successfully match against it are discarded. The idea is to ignore technically valid but probably wrong numbers like0123456789,0000000000,1111111111, etc. -
--isbn-direct-grep-files=<value>; env. variableISBN_DIRECT_GREP_FILES; default value^text/(plain|xml|html)$This is a regular expression that is matched against the MIME type of the searched files. Matching files are searched directly for ISBNs, without converting or OCR-ing them to
.txtfirst. -
--isbn-ignored-files=<value>; env. variableISBN_IGNORED_FILES; see default value inlib.shThis is a regular expression that is matched against the MIME type of the searched files. Matching files are not searched for ISBNs beyond their filename. The default value is a bit long because it tries to make the scripts ignore
.gifand.svgimages, audio, video and executable files and fonts, you can find it inlib.sh. -
--reorder-files-for-grep=<value>; env. variablesISBN_GREP_REORDER_FILES,ISBN_GREP_RF_SCAN_FIRST,ISBN_GREP_RF_REVERSE_LAST; default valuestrue,400,50These options specify if and how we should reorder the ebook text before searching for ISBNs in it. By default, the first 400 lines of the text are searched as they are, then the last 50 are searched in reverse and finally the remainder in the middle. This reordering is done to improve the odds that the first found ISBNs in a book text actually belong to that book (ex. from the copyright section or the back cover), instead of being random ISBNs mentioned in the middle of the book. No part of the text is searched twice, even if these regions overlap. If you use the command-line option, the format for
<value>isfalseto disable the functionality orfirst_lines,last_linesto enable it with the specified values. -
-mfo=<value>,--metadata-fetch-order=<value>; env. variablesISBN_METADATA_FETCH_ORDER; default valueGoodreads,Amazon.com,Google,ISBNDB,WorldCat xISBN,OZON.ruThis option allows you to specify the online metadata sources and order in which the scripts will try searching in them for books by their ISBN. The actual search is done by calibre's
fetch-ebook-metadatacommand-line application, so any custom calibre metadata plugins can also be used. To see the currently available options, runfetch-ebook-metadata --helpand check the description for the--allowed-pluginoption.If you use Calibre versions that are older than 2.84, it's required to manually set this option to an empty string.
Options for OCR:
-
-ocr=<value>,--ocr-enabled=<value>; env. variableOCR_ENABLED; default valuefalseWhether to enable OCR for
.pdf,.djvuand image files. It is disabled by default and can be used differently in two scripts:organize-ebooks.shcan use OCR for finding ISBNs in scanned books. Setting the value totruewill cause it to use OCR for books that failed to be converted to.txtor were converted to empty files by the simple conversion tools (ebook-convert,pdftotext,djvutxt). Setting the value toalwayswill cause it to use OCR even when the simple tools produced a non-empty result, if there were no ISBNs in it.convert-to-txt.shcan use OCR for the conversion to.txt. Setting the value totruewill cause it to use OCR for books that failed to be converted to.txtor were converted to empty files by the simple conversion tools. Setting it toalwayswill cause it to first try OCR-ing the books before trying the simple conversion tools.
-
-ocrop=<value>,--ocr-only-first-last-pages=<value>; env. variableOCR_ONLY_FIRST_LAST_PAGES; default value7,3(except forconvert-to-txt.shwhere it'sfalse)Value
n,minstructs the scripts to convert only the firstnand lastmpages when OCR-ing ebooks. This is done because OCR is a slow resource-intensive process and ISBN numbers are usually at the beginning or at the end of books. Setting the value tofalsedisables this optimization and is the default forconvert-to-txt.sh, where we probably want the whole book to be converted. -
-ocrc=<value>,--ocr-command=<value>; env. variableOCR_COMMAND; default valuetesseract_wrapperThis allows us to define a hook for using custom OCR settings or software. The default value is just a wrapper that allows us to use both tesseract 3 and 4 with some predefined settings. You can use a custom bash function or shell script - the first argument is the input image (books are OCR-ed page by page) and the second argument is the file you have to write the output text to.
-
--token-min-length=<value>; env. variableTOKEN_MIN_LENGTH; default value3When files and file metadata are parsed, they are split into words (or more precisely, either alpha or numeric tokens) and ones shorter than this value are ignored. By default, single and two character number and words are ignored.
-
--tokens-to-ignore=<value>; env. variableTOKENS_TO_IGNORE; complex default valueA regular expression that is matched against the filename/author/title tokens and matching tokens are ignored. The default regular expression includes common words that probably hinder online metadata searching like
book,novel,series,volumeand others, as well as probable publication years like (so1999is ignored while2033is not). You can see it inlib.sh. -
-owis=<value>,--organize-without-isbn-sources=<value>; env. variableORGANIZE_WITHOUT_ISBN_SOURCES; default valueGoodreads,Amazon.com,GoogleThis option allows you to specify the online metadata sources in which the scripts will try searching for books by non-ISBN metadata (i.e. author and title). The actual search is done by calibre's
fetch-ebook-metadatacommand-line application, so any custom calibre metadata plugins can also be used. To see the currently available options, runfetch-ebook-metadata --helpand check the description for the--allowed-pluginoption. Because Calibre versions older than 2.84 don't support the--allowed-pluginoption, if you want to use such an old Calibre version you should manually setORGANIZE_WITHOUT_ISBN_SOURCESto an empty string.In contrast to searching by ISBNs, searching by author and title is done concurrently in all of the allowed online metadata sources. The number of sources is smaller because some metadata sources can be searched only by ISBN or return many false-positives when searching by title and author.
-
-oft=<value>,--output-filename-template=<value>; env. variableOUTPUT_FILENAME_TEMPLATE; default value:"${d[AUTHORS]// & /, } - ${d[SERIES]:+[${d[SERIES]}] - }${d[TITLE]/:/ -}${d[PUBLISHED]:+ (${d[PUBLISHED]%%-*})}${d[ISBN]:+ [${d[ISBN]}]}.${d[EXT]}"This specifies how the filenames of the organized files will look. It is a bash string that is evaluated so it can be very flexible (and also potentially unsafe). The book metadata is present in a hashmap with name
dand uppercase keys. When changing this parameter, keep in mind that you have to either escape the$symbols or wrap everything in single quotes like so:-oft='"${d[TITLE]} by ${d[AUTHORS]}.${d[EXT]}"'By default the organized files start with the comma-separated author name(s), followed by the book series name and number in square brackets (if present), followed by the book title, the year of publication (if present), the ISBN(s) (if present) and the original extension. Here are are how output filenames using the default template look:
Cory Doctorow - [Little Brother #1] - Little Brother (2008) [0765319853].pdf Cory Doctorow - [Little Brother #2] - Homeland (2013) [9780765333698].epub Eliezer Yudkowsky - Harry Potter and the Methods of Rationality (2015).epub Lawrence Lessig - Remix - Making Art and Commerce Thrive in the Hybrid Economy (2008) [9781594201721].djvu Rick Falkvinge - Swarmwise (2013) [1463533152].pdf -
-ome=<value>,--output-metadata-extension=<value>; env. variableOUTPUT_METADATA_EXTENSION; default valuemetaIf
KEEP_METADATAis enabled, this is the extension of the additional metadata file that is saved next to each newly renamed file.
-
-fsf=<value>,--file-sort-flags=<value>; env. variableFILE_SORT_FLAGS; default value()(an empty bash array)A list with the sort options that will be used every time multiple files are processed (i.e. in every script except
convert-to-txt.sh). -
--debug-prefix-length=<value>; env. variableDEBUG_PREFIX_LENGTH; default value40The length of the debug prefix used by the some scripts in the output when
VERBOSEmode is enabled. -
--lib-hook=<path-fo-bash-file>; not configurable with environment variables for security reasonsWarning: for advanced users with bash knowledge only! This option allows you to inject (i.e.
source) bash files into all of the scripts in this collection. You can use this as a way to customize a script's internals, add functionality, make "plugins", etc. This isn't configurable from an environment variable for obvious security reasons, it explicitly has to be set via the CLI flag.
This is probably the most versatile script in the repository. It can automatically organize folders with huge quantities of unorganized ebook files. This is done by extracting ISBNs and/or metadata from the ebook files, downloading their full and hopefully correct metadata from online sources and auto-renaming the unorganized files with full and correct names and moving them to specified folders. Is supports virtually all ebook types, including ebooks in arbitrary or even nested archives (like the other scripts, it assumes that one file is one ebook, even if it's a huge archive). OCR can be used for scanned ebooks and corrupt ebooks and non-ebook documents (pamphlets) can be separated in specified folders. Most of the general options and flags above affect how this script operates, but there are also some specific options for it.
-
-cco,--corruption-check-only; env. variableCORRUPTION_CHECK_ONLY; default valuefalseDo not organize or rename files, just check them for corruption (ex. zero-filled files, corrupt archives or broken
.pdffiles). Useful with theOUTPUT_FOLDER_CORRUPToption. -
--tested-archive-extensions=<value>; env. variableTESTED_ARCHIVE_EXTENSIONS; default value^(7z|bz2|chm|arj|cab|gz|tgz|gzip|zip|rar|xz|tar|epub|docx|odt|ods|cbr|cbz|maff|iso)$A regular expression that specifies which file extensions will be tested with
7z tfor corruption. -
-owi,--organize-without-isbn; env. variableORGANIZE_WITHOUT_ISBN; default valuefalseSpecify whether the script will try to organize ebooks if there were no ISBN found in the book or if no metadata was found online with the retrieved ISBNs. If enabled, the script will first try to use calibre's
ebook-metacommand-line tool to extract the author and title metadata from the ebook file. The script will try searching the online metadata sources (ORGANIZE_WITHOUT_ISBN_SOURCES) by the extracted author & title and just by title. If there is no useful metadata or nothing is found online, the script will try to use the filename for searching. -
-wii=<value>,--without-isbn-ignore=<value>; env. variableWITHOUT_ISBN_IGNORE; complex default valueThis is a regular expression that is matched against lowercase filenames. All files that do not contain ISBNs are matched against it and matching files are ignored by the script, even if
ORGANIZE_WITHOUT_ISBNistrue. The default value is calibrated to match most periodicals (magazines, newspapers, etc.) so the script can ignore them. -
--pamphlet-included-files=<value>; env. variablePAMPHLET_INCLUDED_FILES; default value\.(png|jpg|jpeg|gif|bmp|svg|csv|pptx?)$This is a regular expression that is matched against lowercase filenames. All files that do not contain ISBNs and do not match
WITHOUT_ISBN_IGNOREare matched against it and matching files are considered pamphlets by default. They are moved toOUTPUT_FOLDER_PAMPHLETSif set, otherwise they are ignored. -
--pamphlet-excluded-files=<value>; env. variablePAMPHLET_EXCLUDED_FILES; default value\.(chm|epub|cbr|cbz|mobi|lit|pdb)$This is a regular expression that is matched against lowercase filenames. If files do not contain ISBNs and match against it, they are NOT considered as pamphlets, even if they have a small size or number of pages.
-
--pamphlet-max-pdf-pages=<value>; env. variablePAMPHLET_MAX_PDF_PAGES; default value50.pdffiles that do not contain valid ISBNs and have a lower number pages than this are considered pamplets/non-ebook documents. -
--pamphlet-max-filesize-kb=<value>; env. variablePAMPHLET_MAX_FILESIZE_KB; default value250Other files that do not contain valid ISBNs and are below this size in KBs are considered pamplets/non-ebook documents.
-
-o=<value>,--output-folder=<value>; env. variableOUTPUT_FOLDER; default value is the current working directory (check withpwd)The folder where ebooks that were renamed based on the ISBN metadata will be moved to.
-
-ofu=<value>,--output-folder-uncertain=<value>; env. variableOUTPUT_FOLDER_UNCERTAIN; empty default valueIf
ORGANIZE_WITHOUT_ISBNis enabled, this is the folder to which all ebooks that were renamed based on non-ISBN metadata will be moved to. -
-ofc=<value>,--output-folder-corrupt=<value>; env. variableOUTPUT_FOLDER_CORRUPT; empty default valueIf specified, corrupt files will be moved to this folder.
-
-ofp=<value>,--output-folder-pamphlets=<value>; env. variableOUTPUT_FOLDER_PAMPHLETS; empty default valueIs specified, pamphlets will be moved to this folder.
This script can be used to manually organize ebook files quickly. It can also be used to semi-automatically verify the ebooks organized by organize-ebooks.sh if the KEEP_METADATA option was enabled so the new filenames can be compared with the old ones.
-
-o=<value>,--output-folder=<value>; env. variableOUTPUT_FOLDERS; default value()(an empty bash array)You can use this argument multiple times (or use the array environment variable) to specify different folders to which you can quickly move ebook files. The first specified folder is the default.
-
-qm,--quick-mode; env. variableQUICK_MODE; default valuefalseThis mode is useful when
organize-ebooks.shwas called with--keep-metadata. Ebooks that contain all of the tokens from the old file name in the new one are directly moved to the default output folder. -
-cmbd=<value>,--custom-move-base-dir=<value>; env. variableCUSTOM_MOVE_BASE_DIR; empty default valueThis option is used to specify a base directory in whose sub-folders files can more easily be moved during the interactive session because of tab autocompletion.
-
-robd=<value>,--restore-original-base-dir=<value>; env. variableRESTORE_ORIGINAL_BASE_DIR; empty default valueIf you want to enable the option of restoring files to their original folders (or at least with the same folder structure), set this as the base path.
-
-ddm=<value>,--diacritic-difference-masking=<value>; env. variableDIACRITIC_DIFFERENCE_MASKINGS; complex default valueWhich differences due to accents and other diacritical marks to be ignored when comparing tokens in
QUICK_MODEand the interactive interface. The default value handles some basic cases like allowing letters likeá,à,âand others instead ofaand the reverse when comparing the old and new files. -
-mpw,--match-partial-words; env. variableMATCH_PARTIAL_WORDS; default valuefalseWhether tokens from the old filenames that partially match in the new filename to be accepted by
QUICK_MODEand the interactive interface.
This script tries to find valid ISBNs inside a file or in stdin if no file is specified. Searching for ISBNs in files uses progressively more resource-intensive methods until some ISBNs are found, see the documentation below for more details.
Some global options affect this script (especially the ones related to extracting ISBNs from files), but the only script-specific option is:
-
-irs=<value>,--isbn-return-separator=<value>; env. variableISBN_RET_SEPARATOR; default value$'\n'(a new line)This specifies the separator that will be used when returning any found ISBNs.
This script converts the supplied file to a text file. It can optionally also use OCR for .pdf, .djvu and image files. There are no local options, but a some of the global options affect this script's behavior a lot, especially the OCR ones.
This script traverses a calibre library folder and renames all the book files in it by reading their metadata from calibre's metadata.opf files.
-
-o=<value>,--output-folder=<value>; env. variableOUTPUT_FOLDER; the default value is the current working directory (check withpwd)This is the output folder the renamed books will be moved to.
-
-sm=<value>,--save-metadata=<value>; env. variableSAVE_METADATA; default valuerecreateThis specifies whether metadata files will be saved together with the renamed ebooks. Value
opfcopyjust copies calibre'smetadata.opfnext to each renamed file with aOUTPUT_METADATA_EXTENSIONextension, whilerecreatesaves a metadata file that is similar to the oneorganize-ebooks.shcreates. Any other value disables this function.
This script recursively scans the supplied folders for files and splits the found files (and the accompanying metadata files if present) into folders with consecutive names that each contain the specified number of files.
-
-o=<value>,--output-folder=<value>; env. variableOUTPUT_FOLDER; the default value is the current working directory (check withpwd)The output folder in which all the new consecutively named folders will be created.
-
-sn=<value>,--start-number=<value>; env. variableSTART_NUMBER; default value0The number of the first folder.
-
-fp=<value>,--folder-pattern=<value>; env. variableFOLDER_PATTERN; default value%05d000The
printfformat string that specifies the pattern with which new folders will be created. By default it creates folders like00000000, 00001000, 00002000, .... -
-fpf=<value>,--files-per-folder=<value>; env. variableFILES_PER_FOLDER; default value1000How many files should be moved to each folder.
There are several different ways that a specific file can be searched for ISBN numbers. Each step requires progressively more "expensive" operations. If at some point ISBNs are found, they are returned or used without trying the remaining strategies. These are the actual steps in order:
- Check the supplied file name for ISBNs (the path is ignored).
- If the MIME type of the file matches
ISBN_DIRECT_GREP_FILES, search the file contents directly for ISBNs. If the MIME type matchesISBN_IGNORED_FILES, the search stops with no results. - Check the file metadata from calibre's
ebook-metatool for ISBNs. - Try to extract the file as an archive with
7z. If successful, recursively repeat all of these steps for all the extracted files. This is very useful for normal archives, but it also works for.epup,.chm, and other ebook filetypes that are actually archives of some type that7zsupports. This method of searching them for ISBNs is typically much faster than the next step. - If the file is not an archive, try to convert it to a
.txtfile. Use calibre'sebook-convertunless a faster alternative is present -pdftotextfrompopplerfor.pdffiles,catdocfor.docfiles ordjvutxtfor.djvufiles. Search the resulting.txtfile for ISBNs directly. - If OCR is enabled and the simple conversion to
.txtfails or if its result is empty, try OCR-ing the file. If the simple conversion result is non-empty but does not contain ISBNs andOCR_ENABLEDis set toalways, run OCR as well.
The regular expression used for matching ISBNs (ISBN_REGEX in lib.sh) is purposefully a bit broad, to catch as many potential ISBNs as possible. To reduce false positives, all matched numbers are verified for correct ISBN check digits. Additionally, by default all matched numbers that pass the checks are filtered with the ISBN_BLACKLIST_REGEX regular expression. That way technically valid but probably wrong ISBNs like 0123456789, 0000000000, 1111111111, etc. are discarded.
-
Automatic organization can be slow - all the scripts are synchronous and single-threaded and metadata lookup by ISBN is not done concurrently. This is intentional so that the execution can be easily traced and so that the online services are not hammered by requests. If you want to optimize the performance, run multiple copies of the script on different folders.
-
The default setting for
ISBN_METADATA_FETCH_ORDERincludes two non-standard metadata sources: Goodreads and WorldCat xISBN. For best results, install the plugins (1, 2) for them in calibre and fine-tune the settings for metadata sources in the calibre GUI.
- Add hooks for different actions, for example the ability to call an external script for organizing an ebook instead of directly renaming/symlinking it.
- Add options for modifying different metadata fields before renaming the files.
- Add an option to specify the input filename format for more precise non-ISBN metadata extraction.
- Expand tests - cover all functions and add some whole script tests.
- Add an Arch Linux AUR package.
- Improve the docker image so there are no permission problems on mounted folders and no user with hardcoded UID 1000 (maybe like this).
- Or just maybe rewrite everything in a more portable (or at least saner) language than bash...
Please keep in mind that this is beta-quality software. To avoid data loss, make sure that you have a backup of any files you want to organize. You may also want to run the scripts with the --dry-run or --symlink-only option the first time to make sure that they would do what you expect them to do.
Also keep in mind that these shell scripts parse and extract complex arbitrary media and archive files and pass them to other external programs written in memory-unsafe languages. This is not very safe and specially-crafted malicious ebook files can probably compromise your system when you use these scripts. If you are cautious and want to organize untrusted or unknown ebook files, use something like QubesOS or at least do it in a separate VM/jail/container/etc.
These scripts are licensed under the GNU General Public License v3.0. For more details see the LICENSE file in the repository.