Rename files with renaming rule string (search files)
renamer -i recursive -s all -o apply -c rule -r " ()||-|_|cAa|"Rename files with renaming rule string (from pipe)
find | renamer -i pipe -o apply -c rule -r " ()||-|_|cAa|"Edit files with your editor (for example vim or emacs)
renamer -i recursive -s all -o apply -c editorDifferent ways to use renamer:
- input:
- dir: list directory (like
ls) - recursive: search directory tree (like
find) - pipe: pipe in the input.
- dir: list directory (like
- renaming:
- rule: provide a rule string.
- editor: edit the input yourself.
- config+profile: provide a configuration file with renaming profiles.
- interactive: apply rules and profiles with a TUI application.
- output:
- apply: rename according to given pattern.
- print: print what would be done, if apply option was given.
- validate: print if file system conforms to given rules (i.e. no renaming needed.)
The profile and the rule format are equally powerful, i.e. can be converted into each other. For now see the rename package to see features of rules and profiles.
Example configuration file format:
# An Example Config
title = "Some Example"
[profiles]
[profiles.toast-txt]
name = "n0"
[profiles.toast-txt.profile_rule]
word_separators = " ()"
delete_chars = ""
small_gap_mark = "-"
big_gap_mark = "_"
conversions = "caA"
modes_string = ""
[profiles.prettify-txt]
name = "n1"
[profiles.prettify-txt.profile_rule]
word_separators = " ()"
delete_chars = ""
small_gap_mark = "-"
big_gap_mark = "_"
conversions = "cAa"
modes_string = ""Set an alias for your common renames in your ~/.bashrc.
alias rn_mp3='renamer -i recursive -s all -o apply -c rule -r " ()||-|_|cAa|"'
alias rn_txt='renamer -i recursive -s all -o apply -c rule -r " ()||-|_|caA|"'
alias rename='renamer -i dir -s all -o apply -c editor'This is an alternative to setting up a config file with profiles.
The CLI program is cmd/renamer.go and compiles to renamer.
The main package is rnmanage.
In the below tree, the pkg tree is loosely representing the packages' dependencies.
.
├── cmd
│ └── renamer.go // CLI program
└── pkg
└── rnmanage // orchestrate renaming (editing, automatic) and file system.
├── edit // calls editor.
├── fsmanage // deals with file system.
│ ├── dir // lists directories.
│ └── testutil // creates mock file system.
└── autorn // orchestrates renaming.
├── profiler // save renaming rules into profiles.
└── rename // renames strings.
- next.
- implement some pipe tests, to make sure renamer doesn't delete anything.
- meaning: in a directory with files a, b and c with
ls a b | renamer ...only a and b get renamed.
- meaning: in a directory with files a, b and c with
- implement some pipe tests, to make sure renamer doesn't delete anything.
- features.
- give the choice to exclude (or include) hidden files (prefixed with
.or_).
- give the choice to exclude (or include) hidden files (prefixed with
- renaming: main functionality.
- implement interactive renaming.
- for each file choose method: edit, profile or apply some scripts.
- implement interactive renaming.
- renaming: features.
- automatically prefix, infix, suffix before file ending:
- individual files.
- add the current date.
- yyyy-mm-dd.
- yyyy-mm-dd_HH-MM.
- precise nano-second-timestamp.
- add the file creation date.
- yyyy-mm-dd.
- yyyy-mm-dd_HH-MM.
- precise nano-second-timestamp.
- add a random id (decimal, hexadecimal, alphanumerical)
- add the current date.
- individual files.
- groups of files.
- add incrementing id to list of files, also with prefixed zeroes.
- rename scanned pages.
- rename in different order (
afbecdtoabcdef) for scanned pages (first front side, then backsides in reverse). - example sorting for 6 PNG files: ABCDEF
- split the files in first and second half: --> ABC and DEF
- reverse order of second half: --> ABC and FED
- alternatingly merge the two halfs back together: --> AFBECD.
- rename in different order (
- automatically prefix, infix, suffix before file ending:
- renaming: pitfalls.
- test various types circular renames: eg
(a->b,b->a)should not delete anything, but should execute. - test over-naming renames:
(a->c,c->c), should not delete anything, should not execute. - test colliding renames:
(a->c,b->c)should not delete anything, should not execute.
- test various types circular renames: eg
- misc.
- also pull out
testutilpackage.
- also pull out
Maybe:
- Allow separator for rule strings
renamer -rule " ();;-;_;caA;" -separator=";" ...for a different separator than a pipe.
Done:
- misc.
- use this to parse toml config: https://github.com/BurntSushi/toml/tree/master
- use this for filesystem mocks: https://github.com/spf13/afero
- create my own config manager at https://github.com/kraasch/goconf
- check if config file exists.
- if it doesn't create default config.
- if it does exist read it's content to text blob.
- input: main functionality.
- get file list from pipe, ie allow these inputs:
-
ls | grep -E 'mp3$' | renamer -edit -
find | grep -E '.ogg$' | renamer -profile music_ogg
-
- if nothing is piped in, have two search modes:
recusvieanddir-
renamer -i dir -s all ...list all in pwd (default). -
renamer -i dir -s files ...list all files in pwd. -
renamer -i dir -s dirs ...list all directories in pwd. -
renamer -i recursive -s all ...as above, but recursive (default) -
renamer -i recursive -s files ...as above, but recursive. -
renamer -i recursive -s dirs ...as above, but recursive. -
renamer -i recursive -s dirs ...as above, but recursive.
-
- allow a way to rename by providing a rule string on CLI.
- ie
renamer -rule " ()||-|_|caA| ..."
- ie
- get file list from pipe, ie allow these inputs:
- output: main functionality.
- allow a
-actionflag to specify how to apply a name change (profile/editor)- have a
-action=validateflag which in conjunction with-profilechecks if any file breaks the profile, but doesn't apply the profile. - default to
-action=applyflag which applies the renaming rules specified in profile.
- have a
- allow a
- renaming: main functionality.
- implement renaming profiles.
- run
renamer -profile mediato rename media files with specified conversion (namedmedia, for renaming music, video, etc). - default to
renamer -profile defaultif no profile is specified.
- run
- implement pure editor version (use native editor, eg vi, emacs)
- run
renamer -editto rename with editor.
- run
- implement renaming profiles.
Replace bash scripts like these below.
function myUmlautRemove() {
sed 's/ä/ae/g' |
sed 's/ö/oe/g' |
sed 's/ü/ue/g' |
sed 's/ß/ss/g' |
sed 's/Ä/Ae/g' |
sed 's/Ö/Oe/g' |
sed 's/Ü/Ue/g' |
sed 's/ß/SS/g'
}
function mytoxfreename() {
sed 's/[^\.[:alnum:]]/-/g' | # remove all non-alphanumeric characters (except dots) by replacing with hyphens .
sed 's/[(\[\]){}]/-/g' | # replace all kinds of brackets with spaces .
sed 's/--[-]*/_/g' | # remove all groups of hyphens by replacing with underscores .
sed 's/^[-]*//g' | # remove hyphens from the beginning of file names .
sed 's/[^[:alnum:]]\././g' | # remove non-alphanumeric character next to dots (if there is any, eg one hyphen left to one dot) .
myUmlautRemove | # remove Ä, ä, Ö, ö, Ü, ü and ß .
iconv -f utf8 -t ascii//TRANSLIT | # remove diacritica .
tr 'A-Z' 'a-z' # make lower case .
}
function myDateIt() {
echo "Dating file '"$(ls -lasdh $1)"'."
# read parameter from command line with '$1' .
newname=$(mydate)_$1
# mv option '-n' prevents overwriting existing files; '-v' returns string with non-zero length on success.
success=$(mv -nv $1 $newname)
# test option '-n' tests for strings to be of non-zero length .
if $(test -n "$success"); then
echo "$success"
echo "Now at '"$(ls -lasdh $newname)"'."
else
echo "Dating failed (check if target file already exists: '$newname')"
fi
}
function mytoxremove() {
for x in *; do
newName=$(echo $x | mytoxfreename)
mv -vib "$x" "$newName"
myDateIt "$newName"
done
}
function make_names_nice() {
for x in *-by-*; do
mv "$x" "$(echo $x |
sed 's/\+/-/g' |
sed 's/\([^_]*\)_\(.*\)-by-\([^\.]*\).\(.*\)$/\1+\3+\2.\4/' |
tr '_' '-' |
tr '+' '_' |
sed 's/-_/_/g' |
sed 's/_-/_/g')"
done
}
This is work in progress. This program is
- powerful: interprets little input to do big things,
- risky: might delete by renaming,
- not tested enough yet.
- interesting project, file system mock for
os/exec: https://github.com/schollii/go-test-mock-exec-command - alternative package to parse toml config: https://github.com/pelletier/go-toml
