I am looking for a way (preferably terminal) to organize over 1000 fonts by their first letter.
Basically create directories A-Z, #
then move the font files to those directories based on their filename first character. Fonts that begin with numbers [0-9] or other special characters to be moved to the #
directory.
A late python option:
How to use
move_files.py
Run it with the directory as argument:
The script will only create the (sub) directory(-ies) (uppercase) if it is actually needed
Explanation
The script:
lists the files, gets the first character (defines the sourcepath):
checks if the item is a file:
defines the targeted folder for either if the first char is alpha or not:
checks if the folder already exists or not, creates it if not:
moves the item into its corresponding folder:
Code-golfed yet readable with just two commands and two regular expressions:
If you have a huge amount of files to move, too many to fit into the process argument list (yes, there's a limit and it may be just a few kilobytes), you can generate the file list with a different command and pipe that to
prename
, e. g.:This has the added benefit of not trying to move the literal file name
[[:alnum:]]?*
if no files match the glob pattern.find
also allows many more match criteria than shell globbing. An alternative is to set thenullglob
shell option and close the standard input stream ofprename
.1In both cases remove the
-n
switch to actually move the files and not just show how they would be moved.Addendum: You can remove the empty directories again with:
1
shopt -s nullglob; prename ... <&-
I didn't work out a nice way to make the directory names uppercase (or move the files with uppercase letters), although you could do it afterwards with
rename
...or more readably:
Remove
echo
after testing to actually move the filesAnd then
remove
-n
if it looks good after testing and run again.If you don't mind zsh, a function and a couple of
zmv
commands:The
mmv
function makes the directory and moves the file.zmv
then provides pattern-matching and substitution. First, moving filenames starting with an alphabet, then everything else:Run again without the
echo
inmmv
's definition to actually perform the move.The following commands within the directory containing the fonts should work, if you want to use from outside the font storage directory, change
for f in ./*
tofor f in /directory/containing/fonts/*
. This is a very shell based method, so quite slow, and is also non-recursive. This will only create directories, if there are files which start with the matching character.As a one liner, again from within the font storage directory:
A method using find, with similar string manipulation, using bash parameter expansion, which will be recursive, and should be somewhat quicker than the pure shell version:
Or more readably:
Map each filename to a directory name using
tr
, thenmkdir
andmv
: