Skip to content

Instantly share code, notes, and snippets.

@acdesouza
Last active December 6, 2022 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdesouza/43c0edfcd5a6e027001617790d6b943f to your computer and use it in GitHub Desktop.
Save acdesouza/43c0edfcd5a6e027001617790d6b943f to your computer and use it in GitHub Desktop.
Bag of useful bash tools created combining multiple commands
# ----------------------------------------------------------------------------------------------------
# Renames all files in a directory removing the inital 3 digits from its filename
# "001 Potato.sh" => "Potato.sh"
# "002-Carrot.sh" => "Carrot.sh"
# "003.Letuce.sh" => "Letuce.sh"
# "1003. Tomato.sh" => "Tomato.sh"
#
# Source: https://stackoverflow.com/a/28305274
# MV is a DESTRUCTIVE command. Check if the mv command is correct.
for x in *; do echo mv \"$x\" \"`echo $x | sed -E 's/^[[:digit:]]{3,4}([[:space:]]|-|\\.){1,2}//'`\"; done
# Execute it
for x in *; do mv "$x" "`echo $x | sed -E 's/^[[:digit:]]{3,4}([[:space:]]|-|\\.){1,2}//'`"; done
# ----------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------
# Finds duplicated files in the same directory
# 1. List all files an its calculated MD5
# 2. Modify the output, so I can sort by MD5
# From..: MD5 (./ExistingFileName.extension) = AC4069a44658fcf2cc381736403e74ef
# To....: AC4069a44658fcf2cc381736403e74ef MD5 (./ExistingFileName.extension)
# 3. Sort by MD5, so equal numbers will be sequential
# 4. Use uniq to show only the duplications
# Must use GNU uniq. MacOS uniq doesn't have -w param
find . -type f -exec md5 {} \; | awk -F '=' '{print $2 "\t" $1}' | sort | uniq -w 33 -D
# Using sha sums simplifies the line. Because the output already shows the hash before the filename
find . -type f -exec sha1sum {} \; | sort | guniq -D -w 40
# ----------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment