Convenient functions and aliases to add to your ~/.bash_rc or ~/.bash_profile files to make your life in the terminal better.
alias gitlog='git log --all --graph --oneline --pretty --decorate'
If you're on a mac and want md5sum command to behave similarly to linux:
alias md5sum='md5 -r'
Are you tired of dealing with filenaming? Want to simply have the most meaningful possible filename? Tired of finding that your file has a dumb name and a meaningless timestamp (since you restored it from a backup or transferred it between filesystems)? Want to be able to identify if your file has been corrupted or have a naming scheme that easily identifies file duplicates? This does that.
It applies a consistent timestamp (based on modified time) to files, adds an optional descriptive slug that you provide and a 6 character md5 hash. This way you can md5 the file in the future to see if it's md5 has has changed (thus it's corrupted or altered). Or if you use this naming strategy on all of your files, you can search your system for the md5 to see all the duplicates of that file. Likewise it's great if you're uploading to cloud drives or keeping text indexes of your data. As time goes on, you can rename your files all you want, but the md5 should stay the same (until you alter the file).
Use this when downloading files (eg. from your camera, downloads, anything) for archival.
Usage examples:
porename
or porename --help
- displays helpporename .
- dry-run of recursively renaming all files under current directoryporename --apply .
- when you know you want to apply the renames, add --apply
as the first argument and it will actually move the files.porename --apply . | tee rename_log.csv
- you can pipe output to a .csv to keep the original and new filenames in a spreadsheetporename --apply .
- recursively renames all files under current directoryporename --apply DescriptiveSlug
- recursively renames all files under current directory with DescriptiveSlugporename --apply /path/to/folder/or/file
- recursively renames specific file or all files under specified directoryporename --apply /path/to/folder/or/file DescriptiveSlug
- recursively renames specific file or all files under specified directory with DescriptiveSlugResult: Will rename a file or all files within a directory with their timestamp, 6 characters of the file's md5 hash, and DescriptiveSlug
Note: --apply
must always be the first argument, if you do not include --apply it will run a dry-run simulation so you can see what it would do.
eg. real-world usage:
porename ~/NewDroneFootage/ NM-SantaFe
Results in a simulation (dry-run) showing how the files would be renamed.
porename --apply ~/NewDroneFootage/ NM-SantaFe
Results all files in that directory to be named such as: 20180902-095648_NM-SantaFe.76a0b4.mp4
Alternatively, if you cd ~/NewDroneFootage/
so that you're in the directory, porename --apply NM-Santafe
would have the same result.
Alternatively, if the DescriptiveSlug "NM-SantaFe is ommited, this would result in: 20180902-095648.76a0b4.mp4
Alternatively, instead of a directory you can specify the path to a single file.
NOTE: This assumes the file modified times are correct and authoritive, this is generally a best guess, however sometimes your filenames may contain a timestamp which you'd rather use... Always watch the output of running this command as it shows the previous name and new name for each file. Then rerun with --apply
as the first argument when you're ready to apply the renames. If you wish to log the command, simply append to the end of the command with >> log.csv
or pipe it into tee | tee -a log.csv
you can open the log as a spreadsheet.
function porename {
if [ -z "$1" ] || [ "$1" == "--help" ]; then
echo "Po's rename script - make names good. po@poism.com"
echo "Note will only apply changes if first arg given is --apply"
echo "USAGE EXAMPLES:"
echo " porename . # Dry-run, recursively, from current directory: modtimestamp.md5.ext"
echo " porename /path/to/folder/or/file # Dry-run, file or recursive from given directory: modtimestamp.md5.ext"
echo " porename --apply DescriptiveSlug # Apply recursively from current directory: modtimestamp_DescriptiveSlug.md5.ext"
echo " porename /path/to/folder/or/file DescriptiveSlug # Dry-run, file or recursive from given directory: modtimestamp_DescriptiveSlug.md5.ext"
return
elif [ "$1" == "--apply" ]; then
apply="--apply"
f=$( [ "$2" == "" ] && pwd || echo "$2" )
fslug=$( [ "$3" == "" ] && echo "" || echo "$3" )
else
apply=""
f=$( [ "$1" == "" ] && pwd || echo "$1" )
fslug=$( [ "$2" == "" ] && echo "" || echo "$2" )
fi
if [ ! -f "$f" ] && [ ! -d "$f" ]; then
fslug="$f"
f="`pwd`"
fi
if [ -d "$f" ]; then
find "$f" -type f -print0 | while IFS= read -r -d '' file; do
porename $apply "$file" "$fslug"
done
elif [ -f "$f" ]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
fhash=$(md5 -r "$f" | cut -c1-6 )
else
fhash=$(md5sum "$f" | cut -c1-6 )
fi
fext=$(echo "${f##*.}" | awk '{print tolower($0)}')
d=$(dirname "$f")
slug=$( [ "$fslug" == "" ] && echo "" || echo "_$fslug" )
newname="$d/$(date -r "$f" +%Y%m%d-%H%M%S)$slug.$fhash.$fext"
if [ "$apply" == "--apply" ]; then
printf '"RENAMED:", "%s", "%s"\n' "$f" "$newname"
mv "$f" "$newname"
else
printf '"RENAME (dry-run):", "%s", "%s"\n' "$f" "$newname"
fi
fi
}