edit bash script to truncate leading numbers on files when creating folders












-1














for file in *; do
if [[ -f "$file" ]]; then
mkdir "${file%.*}"
mv "$file" "${file%.*}"
fi
done


So I will have tons of files that will look like "012345_randomnameoffile.pdf" and I want to edit this script to create a folder name of just the numbers and move the file into the folder after creation but if that folder already exists just move the file into the folder. Any help would be greatly appreciated.










share|improve this question
























  • The answer to part of your question can be found in the mkdir man page.   I don't understand the rest of the question.
    – Scott
    Dec 17 '18 at 20:36










  • so as it stands this script will do a one to one creation of a folder for each file with the same name and put that file in the folder. I want this script to only create folders with the numbers as the folder name and put any file that starts with those numbers into the same folder.
    – davidjryan
    Dec 17 '18 at 20:38










  • OK, does every file have a name that begins with one or more digits (number characters)  immediately followed by one (or more) space(s)?
    – Scott
    Dec 17 '18 at 20:41
















-1














for file in *; do
if [[ -f "$file" ]]; then
mkdir "${file%.*}"
mv "$file" "${file%.*}"
fi
done


So I will have tons of files that will look like "012345_randomnameoffile.pdf" and I want to edit this script to create a folder name of just the numbers and move the file into the folder after creation but if that folder already exists just move the file into the folder. Any help would be greatly appreciated.










share|improve this question
























  • The answer to part of your question can be found in the mkdir man page.   I don't understand the rest of the question.
    – Scott
    Dec 17 '18 at 20:36










  • so as it stands this script will do a one to one creation of a folder for each file with the same name and put that file in the folder. I want this script to only create folders with the numbers as the folder name and put any file that starts with those numbers into the same folder.
    – davidjryan
    Dec 17 '18 at 20:38










  • OK, does every file have a name that begins with one or more digits (number characters)  immediately followed by one (or more) space(s)?
    – Scott
    Dec 17 '18 at 20:41














-1












-1








-1







for file in *; do
if [[ -f "$file" ]]; then
mkdir "${file%.*}"
mv "$file" "${file%.*}"
fi
done


So I will have tons of files that will look like "012345_randomnameoffile.pdf" and I want to edit this script to create a folder name of just the numbers and move the file into the folder after creation but if that folder already exists just move the file into the folder. Any help would be greatly appreciated.










share|improve this question















for file in *; do
if [[ -f "$file" ]]; then
mkdir "${file%.*}"
mv "$file" "${file%.*}"
fi
done


So I will have tons of files that will look like "012345_randomnameoffile.pdf" and I want to edit this script to create a folder name of just the numbers and move the file into the folder after creation but if that folder already exists just move the file into the folder. Any help would be greatly appreciated.







bash-scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 17 '18 at 22:23







davidjryan

















asked Dec 17 '18 at 20:28









davidjryandavidjryan

92




92












  • The answer to part of your question can be found in the mkdir man page.   I don't understand the rest of the question.
    – Scott
    Dec 17 '18 at 20:36










  • so as it stands this script will do a one to one creation of a folder for each file with the same name and put that file in the folder. I want this script to only create folders with the numbers as the folder name and put any file that starts with those numbers into the same folder.
    – davidjryan
    Dec 17 '18 at 20:38










  • OK, does every file have a name that begins with one or more digits (number characters)  immediately followed by one (or more) space(s)?
    – Scott
    Dec 17 '18 at 20:41


















  • The answer to part of your question can be found in the mkdir man page.   I don't understand the rest of the question.
    – Scott
    Dec 17 '18 at 20:36










  • so as it stands this script will do a one to one creation of a folder for each file with the same name and put that file in the folder. I want this script to only create folders with the numbers as the folder name and put any file that starts with those numbers into the same folder.
    – davidjryan
    Dec 17 '18 at 20:38










  • OK, does every file have a name that begins with one or more digits (number characters)  immediately followed by one (or more) space(s)?
    – Scott
    Dec 17 '18 at 20:41
















The answer to part of your question can be found in the mkdir man page.   I don't understand the rest of the question.
– Scott
Dec 17 '18 at 20:36




The answer to part of your question can be found in the mkdir man page.   I don't understand the rest of the question.
– Scott
Dec 17 '18 at 20:36












so as it stands this script will do a one to one creation of a folder for each file with the same name and put that file in the folder. I want this script to only create folders with the numbers as the folder name and put any file that starts with those numbers into the same folder.
– davidjryan
Dec 17 '18 at 20:38




so as it stands this script will do a one to one creation of a folder for each file with the same name and put that file in the folder. I want this script to only create folders with the numbers as the folder name and put any file that starts with those numbers into the same folder.
– davidjryan
Dec 17 '18 at 20:38












OK, does every file have a name that begins with one or more digits (number characters)  immediately followed by one (or more) space(s)?
– Scott
Dec 17 '18 at 20:41




OK, does every file have a name that begins with one or more digits (number characters)  immediately followed by one (or more) space(s)?
– Scott
Dec 17 '18 at 20:41










1 Answer
1






active

oldest

votes


















1














Found it



for file in *; do mkdir -p -- "${file%%_*}" && 
mv -- "${file}" "${file%%_*}/${file#*_}"; done


This is basically what I wanted with the added bonus of renaming the file and removing the case number prefix.






share|improve this answer























  • How would I represent a space in this script instead of an _ ? I edited everything to try to make it all more consistent.
    – davidjryan
    Dec 17 '18 at 22:24












  • Pretty straightforward: "${file%% *}". There's a bug in this script: ${file##*_} should be ${file#*_} in case there is more than one _ in the filename. It may not matter in your specific case but in general one may lose fragments of names because of the bug.
    – Kamil Maciorowski
    Dec 17 '18 at 22:45










  • I appreciate it, you might have just saved me a considerable amount of headache
    – davidjryan
    Dec 17 '18 at 22:57











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1385348%2fedit-bash-script-to-truncate-leading-numbers-on-files-when-creating-folders%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Found it



for file in *; do mkdir -p -- "${file%%_*}" && 
mv -- "${file}" "${file%%_*}/${file#*_}"; done


This is basically what I wanted with the added bonus of renaming the file and removing the case number prefix.






share|improve this answer























  • How would I represent a space in this script instead of an _ ? I edited everything to try to make it all more consistent.
    – davidjryan
    Dec 17 '18 at 22:24












  • Pretty straightforward: "${file%% *}". There's a bug in this script: ${file##*_} should be ${file#*_} in case there is more than one _ in the filename. It may not matter in your specific case but in general one may lose fragments of names because of the bug.
    – Kamil Maciorowski
    Dec 17 '18 at 22:45










  • I appreciate it, you might have just saved me a considerable amount of headache
    – davidjryan
    Dec 17 '18 at 22:57
















1














Found it



for file in *; do mkdir -p -- "${file%%_*}" && 
mv -- "${file}" "${file%%_*}/${file#*_}"; done


This is basically what I wanted with the added bonus of renaming the file and removing the case number prefix.






share|improve this answer























  • How would I represent a space in this script instead of an _ ? I edited everything to try to make it all more consistent.
    – davidjryan
    Dec 17 '18 at 22:24












  • Pretty straightforward: "${file%% *}". There's a bug in this script: ${file##*_} should be ${file#*_} in case there is more than one _ in the filename. It may not matter in your specific case but in general one may lose fragments of names because of the bug.
    – Kamil Maciorowski
    Dec 17 '18 at 22:45










  • I appreciate it, you might have just saved me a considerable amount of headache
    – davidjryan
    Dec 17 '18 at 22:57














1












1








1






Found it



for file in *; do mkdir -p -- "${file%%_*}" && 
mv -- "${file}" "${file%%_*}/${file#*_}"; done


This is basically what I wanted with the added bonus of renaming the file and removing the case number prefix.






share|improve this answer














Found it



for file in *; do mkdir -p -- "${file%%_*}" && 
mv -- "${file}" "${file%%_*}/${file#*_}"; done


This is basically what I wanted with the added bonus of renaming the file and removing the case number prefix.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 18 '18 at 5:08









Kamil Maciorowski

24.8k155277




24.8k155277










answered Dec 17 '18 at 21:43









davidjryandavidjryan

92




92












  • How would I represent a space in this script instead of an _ ? I edited everything to try to make it all more consistent.
    – davidjryan
    Dec 17 '18 at 22:24












  • Pretty straightforward: "${file%% *}". There's a bug in this script: ${file##*_} should be ${file#*_} in case there is more than one _ in the filename. It may not matter in your specific case but in general one may lose fragments of names because of the bug.
    – Kamil Maciorowski
    Dec 17 '18 at 22:45










  • I appreciate it, you might have just saved me a considerable amount of headache
    – davidjryan
    Dec 17 '18 at 22:57


















  • How would I represent a space in this script instead of an _ ? I edited everything to try to make it all more consistent.
    – davidjryan
    Dec 17 '18 at 22:24












  • Pretty straightforward: "${file%% *}". There's a bug in this script: ${file##*_} should be ${file#*_} in case there is more than one _ in the filename. It may not matter in your specific case but in general one may lose fragments of names because of the bug.
    – Kamil Maciorowski
    Dec 17 '18 at 22:45










  • I appreciate it, you might have just saved me a considerable amount of headache
    – davidjryan
    Dec 17 '18 at 22:57
















How would I represent a space in this script instead of an _ ? I edited everything to try to make it all more consistent.
– davidjryan
Dec 17 '18 at 22:24






How would I represent a space in this script instead of an _ ? I edited everything to try to make it all more consistent.
– davidjryan
Dec 17 '18 at 22:24














Pretty straightforward: "${file%% *}". There's a bug in this script: ${file##*_} should be ${file#*_} in case there is more than one _ in the filename. It may not matter in your specific case but in general one may lose fragments of names because of the bug.
– Kamil Maciorowski
Dec 17 '18 at 22:45




Pretty straightforward: "${file%% *}". There's a bug in this script: ${file##*_} should be ${file#*_} in case there is more than one _ in the filename. It may not matter in your specific case but in general one may lose fragments of names because of the bug.
– Kamil Maciorowski
Dec 17 '18 at 22:45












I appreciate it, you might have just saved me a considerable amount of headache
– davidjryan
Dec 17 '18 at 22:57




I appreciate it, you might have just saved me a considerable amount of headache
– davidjryan
Dec 17 '18 at 22:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1385348%2fedit-bash-script-to-truncate-leading-numbers-on-files-when-creating-folders%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]