Rename files based on checksum
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have a md5sum list and a lot of file which I wanted to checksum and then rename them according to the md5sum list.
Example of the list:
d4cd401ade018617629b39efed7b7be4 foo.bar
8fdb07ca55c164e0d5a69eff49fe800e bar.foo
8b167d01009f066aaf2d6c1ba336d842 foobar
Now I wanted to checksum every files in current directory, if the checksum are matched with the list above then rename it as the right colum.
How I can do that?
files rename hashsum checksum
add a comment |
I have a md5sum list and a lot of file which I wanted to checksum and then rename them according to the md5sum list.
Example of the list:
d4cd401ade018617629b39efed7b7be4 foo.bar
8fdb07ca55c164e0d5a69eff49fe800e bar.foo
8b167d01009f066aaf2d6c1ba336d842 foobar
Now I wanted to checksum every files in current directory, if the checksum are matched with the list above then rename it as the right colum.
How I can do that?
files rename hashsum checksum
add a comment |
I have a md5sum list and a lot of file which I wanted to checksum and then rename them according to the md5sum list.
Example of the list:
d4cd401ade018617629b39efed7b7be4 foo.bar
8fdb07ca55c164e0d5a69eff49fe800e bar.foo
8b167d01009f066aaf2d6c1ba336d842 foobar
Now I wanted to checksum every files in current directory, if the checksum are matched with the list above then rename it as the right colum.
How I can do that?
files rename hashsum checksum
I have a md5sum list and a lot of file which I wanted to checksum and then rename them according to the md5sum list.
Example of the list:
d4cd401ade018617629b39efed7b7be4 foo.bar
8fdb07ca55c164e0d5a69eff49fe800e bar.foo
8b167d01009f066aaf2d6c1ba336d842 foobar
Now I wanted to checksum every files in current directory, if the checksum are matched with the list above then rename it as the right colum.
How I can do that?
files rename hashsum checksum
files rename hashsum checksum
asked Dec 6 '16 at 12:56
SandPoxSandPox
377
377
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
I haven't fully tested, it's just theoretically working. Substitute where needed:
#! /bin/bash
for II in *
do
if [ -f "$II" ]; then
TMPV=$(md5sum "$II")
MD="${TMPV% *}"
TMPV=$(grep "$MD" hashes.txt)
if [ ! -z "$TMPV" ]; then
FN="${TMPV#* }"
echo "Found: $II"
echo "MD5 is: $MD"
echo "Which matches $FN in hashes database"
echo "Will Rename $II TO $FN"
echo ""
# CAREFUL, RENAME CMD: mv "$II" "$FN"
fi;
fi;
done;
As I say, haven't tested it, but it seemed to work on my box.
add a comment |
First of all, I'm not going to claim that this is the most profound solution but, here is one way to do it.
Let's say you have the file with the checksum and filenames called filelist.txt
then you could use something like:
while read -r checksum fname; do for f in file*; do if [[ $checksum == $(md5sum "$f" | cut -d' ' -f1) ]]; then mv "$f" "$fname"; fi ; done ; done < filelist.txt
I got error "md5sum: file*: No such file or directory"
– SandPox
Dec 7 '16 at 10:47
1
You got the error becausefile*
does not exist. Changefile*
to*
and it should work. The latter will loop through all files within the directory.
– Valentin Bajrami
Dec 7 '16 at 11:11
add a comment |
My idea:
- At first you need to sort your known checksums:
sort checksums.txt > sorted_checksums.txt
- Generate file for all existing files and also sort them:
md5sum * | sort > real_checksums.txt
- Join this two files and exclude records with same new and old names:
join -o "2.2 1.2" sorted_checksums.txt real_checksums.txt | awk '$1 != $2' > rename_pairs.txt
- Rename all files:
cat rename_pairs.txt | xargs -L 1 echo mv
(Removeecho
fromxargs
to actually rename files)
WARNING: this will work only if there is no spaces in filenames. You could use awk 'NF != 2' sorted_checksums.txt real_checksums.txt
to check that and if there will be any line printed, then you need to use something else (may be simple perl
or python
program) for steps 3 and 4.
add a comment |
Read the checksums into an associative array, then walk through the files and rename them as needed. Put the renamed files in a separate directory tree, in case there is overlap between the new names and the old names.
#!/bin/bash
mkdir renamed
typeset -A names
while read -r sum name; do
names[$sum]=$name
done <list.md5sum
for file in *; do
if [[ -f $file ]]; then
sum=$(md5sum <"$file"); sum=${sum%% *}
if [[ -n ${names[$sum]} ]]; then
mv -- "$file" "renamed/${names[$sum]}"
fi
fi
done
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%2f328421%2frename-files-based-on-checksum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
I haven't fully tested, it's just theoretically working. Substitute where needed:
#! /bin/bash
for II in *
do
if [ -f "$II" ]; then
TMPV=$(md5sum "$II")
MD="${TMPV% *}"
TMPV=$(grep "$MD" hashes.txt)
if [ ! -z "$TMPV" ]; then
FN="${TMPV#* }"
echo "Found: $II"
echo "MD5 is: $MD"
echo "Which matches $FN in hashes database"
echo "Will Rename $II TO $FN"
echo ""
# CAREFUL, RENAME CMD: mv "$II" "$FN"
fi;
fi;
done;
As I say, haven't tested it, but it seemed to work on my box.
add a comment |
I haven't fully tested, it's just theoretically working. Substitute where needed:
#! /bin/bash
for II in *
do
if [ -f "$II" ]; then
TMPV=$(md5sum "$II")
MD="${TMPV% *}"
TMPV=$(grep "$MD" hashes.txt)
if [ ! -z "$TMPV" ]; then
FN="${TMPV#* }"
echo "Found: $II"
echo "MD5 is: $MD"
echo "Which matches $FN in hashes database"
echo "Will Rename $II TO $FN"
echo ""
# CAREFUL, RENAME CMD: mv "$II" "$FN"
fi;
fi;
done;
As I say, haven't tested it, but it seemed to work on my box.
add a comment |
I haven't fully tested, it's just theoretically working. Substitute where needed:
#! /bin/bash
for II in *
do
if [ -f "$II" ]; then
TMPV=$(md5sum "$II")
MD="${TMPV% *}"
TMPV=$(grep "$MD" hashes.txt)
if [ ! -z "$TMPV" ]; then
FN="${TMPV#* }"
echo "Found: $II"
echo "MD5 is: $MD"
echo "Which matches $FN in hashes database"
echo "Will Rename $II TO $FN"
echo ""
# CAREFUL, RENAME CMD: mv "$II" "$FN"
fi;
fi;
done;
As I say, haven't tested it, but it seemed to work on my box.
I haven't fully tested, it's just theoretically working. Substitute where needed:
#! /bin/bash
for II in *
do
if [ -f "$II" ]; then
TMPV=$(md5sum "$II")
MD="${TMPV% *}"
TMPV=$(grep "$MD" hashes.txt)
if [ ! -z "$TMPV" ]; then
FN="${TMPV#* }"
echo "Found: $II"
echo "MD5 is: $MD"
echo "Which matches $FN in hashes database"
echo "Will Rename $II TO $FN"
echo ""
# CAREFUL, RENAME CMD: mv "$II" "$FN"
fi;
fi;
done;
As I say, haven't tested it, but it seemed to work on my box.
edited 4 hours ago
Rui F Ribeiro
41.9k1483142
41.9k1483142
answered Dec 6 '16 at 13:46
nonzyrononzyro
808
808
add a comment |
add a comment |
First of all, I'm not going to claim that this is the most profound solution but, here is one way to do it.
Let's say you have the file with the checksum and filenames called filelist.txt
then you could use something like:
while read -r checksum fname; do for f in file*; do if [[ $checksum == $(md5sum "$f" | cut -d' ' -f1) ]]; then mv "$f" "$fname"; fi ; done ; done < filelist.txt
I got error "md5sum: file*: No such file or directory"
– SandPox
Dec 7 '16 at 10:47
1
You got the error becausefile*
does not exist. Changefile*
to*
and it should work. The latter will loop through all files within the directory.
– Valentin Bajrami
Dec 7 '16 at 11:11
add a comment |
First of all, I'm not going to claim that this is the most profound solution but, here is one way to do it.
Let's say you have the file with the checksum and filenames called filelist.txt
then you could use something like:
while read -r checksum fname; do for f in file*; do if [[ $checksum == $(md5sum "$f" | cut -d' ' -f1) ]]; then mv "$f" "$fname"; fi ; done ; done < filelist.txt
I got error "md5sum: file*: No such file or directory"
– SandPox
Dec 7 '16 at 10:47
1
You got the error becausefile*
does not exist. Changefile*
to*
and it should work. The latter will loop through all files within the directory.
– Valentin Bajrami
Dec 7 '16 at 11:11
add a comment |
First of all, I'm not going to claim that this is the most profound solution but, here is one way to do it.
Let's say you have the file with the checksum and filenames called filelist.txt
then you could use something like:
while read -r checksum fname; do for f in file*; do if [[ $checksum == $(md5sum "$f" | cut -d' ' -f1) ]]; then mv "$f" "$fname"; fi ; done ; done < filelist.txt
First of all, I'm not going to claim that this is the most profound solution but, here is one way to do it.
Let's say you have the file with the checksum and filenames called filelist.txt
then you could use something like:
while read -r checksum fname; do for f in file*; do if [[ $checksum == $(md5sum "$f" | cut -d' ' -f1) ]]; then mv "$f" "$fname"; fi ; done ; done < filelist.txt
answered Dec 6 '16 at 13:45
Valentin BajramiValentin Bajrami
6,17611729
6,17611729
I got error "md5sum: file*: No such file or directory"
– SandPox
Dec 7 '16 at 10:47
1
You got the error becausefile*
does not exist. Changefile*
to*
and it should work. The latter will loop through all files within the directory.
– Valentin Bajrami
Dec 7 '16 at 11:11
add a comment |
I got error "md5sum: file*: No such file or directory"
– SandPox
Dec 7 '16 at 10:47
1
You got the error becausefile*
does not exist. Changefile*
to*
and it should work. The latter will loop through all files within the directory.
– Valentin Bajrami
Dec 7 '16 at 11:11
I got error "md5sum: file*: No such file or directory"
– SandPox
Dec 7 '16 at 10:47
I got error "md5sum: file*: No such file or directory"
– SandPox
Dec 7 '16 at 10:47
1
1
You got the error because
file*
does not exist. Change file*
to *
and it should work. The latter will loop through all files within the directory.– Valentin Bajrami
Dec 7 '16 at 11:11
You got the error because
file*
does not exist. Change file*
to *
and it should work. The latter will loop through all files within the directory.– Valentin Bajrami
Dec 7 '16 at 11:11
add a comment |
My idea:
- At first you need to sort your known checksums:
sort checksums.txt > sorted_checksums.txt
- Generate file for all existing files and also sort them:
md5sum * | sort > real_checksums.txt
- Join this two files and exclude records with same new and old names:
join -o "2.2 1.2" sorted_checksums.txt real_checksums.txt | awk '$1 != $2' > rename_pairs.txt
- Rename all files:
cat rename_pairs.txt | xargs -L 1 echo mv
(Removeecho
fromxargs
to actually rename files)
WARNING: this will work only if there is no spaces in filenames. You could use awk 'NF != 2' sorted_checksums.txt real_checksums.txt
to check that and if there will be any line printed, then you need to use something else (may be simple perl
or python
program) for steps 3 and 4.
add a comment |
My idea:
- At first you need to sort your known checksums:
sort checksums.txt > sorted_checksums.txt
- Generate file for all existing files and also sort them:
md5sum * | sort > real_checksums.txt
- Join this two files and exclude records with same new and old names:
join -o "2.2 1.2" sorted_checksums.txt real_checksums.txt | awk '$1 != $2' > rename_pairs.txt
- Rename all files:
cat rename_pairs.txt | xargs -L 1 echo mv
(Removeecho
fromxargs
to actually rename files)
WARNING: this will work only if there is no spaces in filenames. You could use awk 'NF != 2' sorted_checksums.txt real_checksums.txt
to check that and if there will be any line printed, then you need to use something else (may be simple perl
or python
program) for steps 3 and 4.
add a comment |
My idea:
- At first you need to sort your known checksums:
sort checksums.txt > sorted_checksums.txt
- Generate file for all existing files and also sort them:
md5sum * | sort > real_checksums.txt
- Join this two files and exclude records with same new and old names:
join -o "2.2 1.2" sorted_checksums.txt real_checksums.txt | awk '$1 != $2' > rename_pairs.txt
- Rename all files:
cat rename_pairs.txt | xargs -L 1 echo mv
(Removeecho
fromxargs
to actually rename files)
WARNING: this will work only if there is no spaces in filenames. You could use awk 'NF != 2' sorted_checksums.txt real_checksums.txt
to check that and if there will be any line printed, then you need to use something else (may be simple perl
or python
program) for steps 3 and 4.
My idea:
- At first you need to sort your known checksums:
sort checksums.txt > sorted_checksums.txt
- Generate file for all existing files and also sort them:
md5sum * | sort > real_checksums.txt
- Join this two files and exclude records with same new and old names:
join -o "2.2 1.2" sorted_checksums.txt real_checksums.txt | awk '$1 != $2' > rename_pairs.txt
- Rename all files:
cat rename_pairs.txt | xargs -L 1 echo mv
(Removeecho
fromxargs
to actually rename files)
WARNING: this will work only if there is no spaces in filenames. You could use awk 'NF != 2' sorted_checksums.txt real_checksums.txt
to check that and if there will be any line printed, then you need to use something else (may be simple perl
or python
program) for steps 3 and 4.
answered Dec 6 '16 at 16:36
Fedor DikarevFedor Dikarev
1,103310
1,103310
add a comment |
add a comment |
Read the checksums into an associative array, then walk through the files and rename them as needed. Put the renamed files in a separate directory tree, in case there is overlap between the new names and the old names.
#!/bin/bash
mkdir renamed
typeset -A names
while read -r sum name; do
names[$sum]=$name
done <list.md5sum
for file in *; do
if [[ -f $file ]]; then
sum=$(md5sum <"$file"); sum=${sum%% *}
if [[ -n ${names[$sum]} ]]; then
mv -- "$file" "renamed/${names[$sum]}"
fi
fi
done
add a comment |
Read the checksums into an associative array, then walk through the files and rename them as needed. Put the renamed files in a separate directory tree, in case there is overlap between the new names and the old names.
#!/bin/bash
mkdir renamed
typeset -A names
while read -r sum name; do
names[$sum]=$name
done <list.md5sum
for file in *; do
if [[ -f $file ]]; then
sum=$(md5sum <"$file"); sum=${sum%% *}
if [[ -n ${names[$sum]} ]]; then
mv -- "$file" "renamed/${names[$sum]}"
fi
fi
done
add a comment |
Read the checksums into an associative array, then walk through the files and rename them as needed. Put the renamed files in a separate directory tree, in case there is overlap between the new names and the old names.
#!/bin/bash
mkdir renamed
typeset -A names
while read -r sum name; do
names[$sum]=$name
done <list.md5sum
for file in *; do
if [[ -f $file ]]; then
sum=$(md5sum <"$file"); sum=${sum%% *}
if [[ -n ${names[$sum]} ]]; then
mv -- "$file" "renamed/${names[$sum]}"
fi
fi
done
Read the checksums into an associative array, then walk through the files and rename them as needed. Put the renamed files in a separate directory tree, in case there is overlap between the new names and the old names.
#!/bin/bash
mkdir renamed
typeset -A names
while read -r sum name; do
names[$sum]=$name
done <list.md5sum
for file in *; do
if [[ -f $file ]]; then
sum=$(md5sum <"$file"); sum=${sum%% *}
if [[ -n ${names[$sum]} ]]; then
mv -- "$file" "renamed/${names[$sum]}"
fi
fi
done
answered Dec 7 '16 at 1:31
GillesGilles
546k12911101624
546k12911101624
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%2f328421%2frename-files-based-on-checksum%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