Last active
November 13, 2015 21:09
-
-
Save lxr/0e56da5ffe9bc23a79a8 to your computer and use it in GitHub Desktop.
rename files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
mvopts=-i | |
debug=false | |
unset command | |
while getopts fne: opt; do | |
case $opt in | |
f) mvopts=-f;; | |
n) debug=true;; | |
e) command="$command;$OPTARG";; | |
\?) exit 1;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ -z ${command+set} ]; then | |
command=${1?usage: rename [-fn] -e command | command [file ...]} | |
shift | |
fi | |
if [ $# -gt 0 ]; then | |
tmp=$(mktemp) | |
trap 'rm -f "$tmp"' EXIT | |
printf '%s\n' "$@" >"$tmp" | |
exec <"$tmp" | |
fi | |
set +e | |
IFS=' | |
' | |
sed -re "p;$command" | while { read old; read new; }; do | |
# this should perhaps be factored out into a separate command | |
status=0 | |
if ! $debug && [ "$old" != "$new" ]; then | |
mkdir -p -- "$(dirname -- "$new")" | |
mv "$mvopts" -- "$old" "$new"; status=$? | |
rmdir -p -- "$(dirname -- "$old")" 2>/dev/null | |
fi | |
if [ $status -eq 0 ]; then | |
printf '%s\n' "$new" | |
fi | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NAME | |
rename - rename files | |
SYNOPSIS | |
rename [-fn] -e command | command [file ...] | |
DESCRIPTION | |
The rename utility renames each file using the given sed(1) | |
command (with extended regular expressions) and writes the new | |
names to standard output. rename operates on entire filepaths. | |
Unsuccessful renames are logged to standard error. If no files | |
are given, a list of files, one per line, is read from standard | |
input. | |
The options are as follows: | |
-e command | |
Append command to the list of commands with which to | |
rename the files. | |
-f | |
Force overwriting of existing files. By default, | |
rename asks you to confirm. | |
-n | |
Don't actually move any files, just print what their | |
new names would be to standard output. | |
SEE ALSO | |
rename_scripts(1), sed(1), re_format(7) | |
CAVEATS | |
Neither the old nor new filenames may contain newlines. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
exec rename -e "y/ /__/" "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
exec rename -e "s:/: - :g" "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
exec rename -e "y/_/ /" "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
exec rename -e "s: - :/:g" "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NAME | |
rename.escape, rename.unescape, rename.fold, | |
rename.unfold - common renaming operations | |
SYNOPSIS | |
rename.escape [file ...] | |
rename.unescape [file ...] | |
rename.fold [file ...] | |
rename.unfold [file ...] | |
DESCRIPTION | |
These scripts use rename(1) to implement common renaming | |
operations. They all accept the same options as rename. | |
rename.escape replaces all spaces and tabs in the filenames | |
with underscores. rename.unescape undoes this transformation, | |
replacing all underscores with spaces. | |
rename.fold flattens directory hierarchies by replacing all | |
slashes with the string " - ". rename.unfold undoes this | |
transformation. | |
SEE ALSO | |
rename(1) | |
CAVEATS | |
rename.fold will translate even the slashes in ./ and ../ | |
components, which is most likely not desired. | |
rename.unfold cannot perfectly reconstruct the original | |
directory hierarchy if its files contain the string " - ". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment