Single command to rename a file to the first N characters of its hash

Clash Royale CLAN TAG#URR8PPP
I'm on macOS Mojave.
I hate having to name images when adding them to my website (Jekyll, for anyone who cares). Instead, I think a reasonable strategy is to generate a hash based on the contents of the file and use the first N characters of the hash as the file name. This strategy also helps with versioning my assets when serving the website out of Amazon S3 and CloudFront.
Example
Given a file somescreenshot.png, I would run this TBD command and the result is the renamed file 8dbm2hz1.png.
Progress
I've made progress on this front but currently it's a multi-step approach.
- Generate the hash, take the first eight characters and send to the clipboard.
shasum -a 256 -b inputfile.png | head -c8 | pbcopy - Rename the file.
mv inputfile.png [paste from #1]
I need to make this command an efficient piped, single step process.
shell-script
add a comment |
I'm on macOS Mojave.
I hate having to name images when adding them to my website (Jekyll, for anyone who cares). Instead, I think a reasonable strategy is to generate a hash based on the contents of the file and use the first N characters of the hash as the file name. This strategy also helps with versioning my assets when serving the website out of Amazon S3 and CloudFront.
Example
Given a file somescreenshot.png, I would run this TBD command and the result is the renamed file 8dbm2hz1.png.
Progress
I've made progress on this front but currently it's a multi-step approach.
- Generate the hash, take the first eight characters and send to the clipboard.
shasum -a 256 -b inputfile.png | head -c8 | pbcopy - Rename the file.
mv inputfile.png [paste from #1]
I need to make this command an efficient piped, single step process.
shell-script
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
– Kusalananda
Feb 18 at 20:30
add a comment |
I'm on macOS Mojave.
I hate having to name images when adding them to my website (Jekyll, for anyone who cares). Instead, I think a reasonable strategy is to generate a hash based on the contents of the file and use the first N characters of the hash as the file name. This strategy also helps with versioning my assets when serving the website out of Amazon S3 and CloudFront.
Example
Given a file somescreenshot.png, I would run this TBD command and the result is the renamed file 8dbm2hz1.png.
Progress
I've made progress on this front but currently it's a multi-step approach.
- Generate the hash, take the first eight characters and send to the clipboard.
shasum -a 256 -b inputfile.png | head -c8 | pbcopy - Rename the file.
mv inputfile.png [paste from #1]
I need to make this command an efficient piped, single step process.
shell-script
I'm on macOS Mojave.
I hate having to name images when adding them to my website (Jekyll, for anyone who cares). Instead, I think a reasonable strategy is to generate a hash based on the contents of the file and use the first N characters of the hash as the file name. This strategy also helps with versioning my assets when serving the website out of Amazon S3 and CloudFront.
Example
Given a file somescreenshot.png, I would run this TBD command and the result is the renamed file 8dbm2hz1.png.
Progress
I've made progress on this front but currently it's a multi-step approach.
- Generate the hash, take the first eight characters and send to the clipboard.
shasum -a 256 -b inputfile.png | head -c8 | pbcopy - Rename the file.
mv inputfile.png [paste from #1]
I need to make this command an efficient piped, single step process.
shell-script
shell-script
edited Feb 18 at 20:39
Rui F Ribeiro
41.5k1481140
41.5k1481140
asked Feb 17 at 16:12
Ryan RodemoyerRyan Rodemoyer
1032
1032
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
– Kusalananda
Feb 18 at 20:30
add a comment |
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
– Kusalananda
Feb 18 at 20:30
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
– Kusalananda
Feb 18 at 20:30
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
– Kusalananda
Feb 18 at 20:30
add a comment |
3 Answers
3
active
oldest
votes
Why not use:
mv -- inputfile.png "$(shasum -a 256 -b inputfile.png | head -c8)"
It will move your file to the output of $(shasum -a 256 -b inputfile.png | head -c8) i.e. first 8 character of the hash.
add a comment |
Use command substitution to save the hash in a shell variable:
file=inputfile.png
h=$(shasum -a 256 -b "$file" | head -c8)
Then you can use it for renaming, printing to the terminal, sending to pbcopy, or whatever.
mv -- "$file" "$h"
echo "$h" | pbcopy
echo "'$file' renamed to '$h'"
If you want to keep the file extension, use something like this:
newname=$h.$file##*.
mv -- "$file" "$newname"
($var##*. removes everything up to the last dot.)
add a comment |
If you want to rename all files, then use xargs. This is an incantation of GNU tools, check your man pages to see if the corresponding options exist.
printf "%s" * |
xargs -0 -I sh -c 'echo mv "$1" "$(shasum -a 256 -b "$1"| head -c8)"' sh
This uses the null byte to separate filenames, which makes working with filenames that contain whitespace a safe operation.
The echo command is to be removed to actually move the files.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501191%2fsingle-command-to-rename-a-file-to-the-first-n-characters-of-its-hash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why not use:
mv -- inputfile.png "$(shasum -a 256 -b inputfile.png | head -c8)"
It will move your file to the output of $(shasum -a 256 -b inputfile.png | head -c8) i.e. first 8 character of the hash.
add a comment |
Why not use:
mv -- inputfile.png "$(shasum -a 256 -b inputfile.png | head -c8)"
It will move your file to the output of $(shasum -a 256 -b inputfile.png | head -c8) i.e. first 8 character of the hash.
add a comment |
Why not use:
mv -- inputfile.png "$(shasum -a 256 -b inputfile.png | head -c8)"
It will move your file to the output of $(shasum -a 256 -b inputfile.png | head -c8) i.e. first 8 character of the hash.
Why not use:
mv -- inputfile.png "$(shasum -a 256 -b inputfile.png | head -c8)"
It will move your file to the output of $(shasum -a 256 -b inputfile.png | head -c8) i.e. first 8 character of the hash.
edited Feb 17 at 16:26
answered Feb 17 at 16:17
Prvt_YadvPrvt_Yadv
2,66231027
2,66231027
add a comment |
add a comment |
Use command substitution to save the hash in a shell variable:
file=inputfile.png
h=$(shasum -a 256 -b "$file" | head -c8)
Then you can use it for renaming, printing to the terminal, sending to pbcopy, or whatever.
mv -- "$file" "$h"
echo "$h" | pbcopy
echo "'$file' renamed to '$h'"
If you want to keep the file extension, use something like this:
newname=$h.$file##*.
mv -- "$file" "$newname"
($var##*. removes everything up to the last dot.)
add a comment |
Use command substitution to save the hash in a shell variable:
file=inputfile.png
h=$(shasum -a 256 -b "$file" | head -c8)
Then you can use it for renaming, printing to the terminal, sending to pbcopy, or whatever.
mv -- "$file" "$h"
echo "$h" | pbcopy
echo "'$file' renamed to '$h'"
If you want to keep the file extension, use something like this:
newname=$h.$file##*.
mv -- "$file" "$newname"
($var##*. removes everything up to the last dot.)
add a comment |
Use command substitution to save the hash in a shell variable:
file=inputfile.png
h=$(shasum -a 256 -b "$file" | head -c8)
Then you can use it for renaming, printing to the terminal, sending to pbcopy, or whatever.
mv -- "$file" "$h"
echo "$h" | pbcopy
echo "'$file' renamed to '$h'"
If you want to keep the file extension, use something like this:
newname=$h.$file##*.
mv -- "$file" "$newname"
($var##*. removes everything up to the last dot.)
Use command substitution to save the hash in a shell variable:
file=inputfile.png
h=$(shasum -a 256 -b "$file" | head -c8)
Then you can use it for renaming, printing to the terminal, sending to pbcopy, or whatever.
mv -- "$file" "$h"
echo "$h" | pbcopy
echo "'$file' renamed to '$h'"
If you want to keep the file extension, use something like this:
newname=$h.$file##*.
mv -- "$file" "$newname"
($var##*. removes everything up to the last dot.)
answered Feb 17 at 16:25
ilkkachuilkkachu
61.1k1099175
61.1k1099175
add a comment |
add a comment |
If you want to rename all files, then use xargs. This is an incantation of GNU tools, check your man pages to see if the corresponding options exist.
printf "%s" * |
xargs -0 -I sh -c 'echo mv "$1" "$(shasum -a 256 -b "$1"| head -c8)"' sh
This uses the null byte to separate filenames, which makes working with filenames that contain whitespace a safe operation.
The echo command is to be removed to actually move the files.
add a comment |
If you want to rename all files, then use xargs. This is an incantation of GNU tools, check your man pages to see if the corresponding options exist.
printf "%s" * |
xargs -0 -I sh -c 'echo mv "$1" "$(shasum -a 256 -b "$1"| head -c8)"' sh
This uses the null byte to separate filenames, which makes working with filenames that contain whitespace a safe operation.
The echo command is to be removed to actually move the files.
add a comment |
If you want to rename all files, then use xargs. This is an incantation of GNU tools, check your man pages to see if the corresponding options exist.
printf "%s" * |
xargs -0 -I sh -c 'echo mv "$1" "$(shasum -a 256 -b "$1"| head -c8)"' sh
This uses the null byte to separate filenames, which makes working with filenames that contain whitespace a safe operation.
The echo command is to be removed to actually move the files.
If you want to rename all files, then use xargs. This is an incantation of GNU tools, check your man pages to see if the corresponding options exist.
printf "%s" * |
xargs -0 -I sh -c 'echo mv "$1" "$(shasum -a 256 -b "$1"| head -c8)"' sh
This uses the null byte to separate filenames, which makes working with filenames that contain whitespace a safe operation.
The echo command is to be removed to actually move the files.
edited Feb 17 at 21:27
community wiki
2 revs, 2 users 86%
glenn jackman
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501191%2fsingle-command-to-rename-a-file-to-the-first-n-characters-of-its-hash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.
– Kusalananda
Feb 18 at 20:30