Rename multiple files of different name and extensions with parent directory name












0















I'm trying to figure out how to rename multiple files (100s) that have different extensions to their respective sub directory name. Here is the structure:



Parentdir
|--subdir1
|---name1.txt
|---name2.jpeg
|--name3.ovc
|--subdir2
|--name4.txt
|--name5.tiff
|-name6.mpeg


what I would like to do is batch process a rename of all files within their respective subdir name while maintaining their extension.



From the parent directory I was using this command but I'm having issues with the file extensions:



for subdir in *; do mv $subdir/*  $subdir.mpeg; done;









share|improve this question




















  • 2





    What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?

    – PerlDuck
    Dec 28 '18 at 19:35








  • 1





    Possible duplicate of Search folder, find and copy files to new folder corresponding file ending

    – PerlDuck
    Dec 28 '18 at 19:42











  • Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )

    – Sergiy Kolodyazhnyy
    Dec 31 '18 at 9:54
















0















I'm trying to figure out how to rename multiple files (100s) that have different extensions to their respective sub directory name. Here is the structure:



Parentdir
|--subdir1
|---name1.txt
|---name2.jpeg
|--name3.ovc
|--subdir2
|--name4.txt
|--name5.tiff
|-name6.mpeg


what I would like to do is batch process a rename of all files within their respective subdir name while maintaining their extension.



From the parent directory I was using this command but I'm having issues with the file extensions:



for subdir in *; do mv $subdir/*  $subdir.mpeg; done;









share|improve this question




















  • 2





    What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?

    – PerlDuck
    Dec 28 '18 at 19:35








  • 1





    Possible duplicate of Search folder, find and copy files to new folder corresponding file ending

    – PerlDuck
    Dec 28 '18 at 19:42











  • Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )

    – Sergiy Kolodyazhnyy
    Dec 31 '18 at 9:54














0












0








0








I'm trying to figure out how to rename multiple files (100s) that have different extensions to their respective sub directory name. Here is the structure:



Parentdir
|--subdir1
|---name1.txt
|---name2.jpeg
|--name3.ovc
|--subdir2
|--name4.txt
|--name5.tiff
|-name6.mpeg


what I would like to do is batch process a rename of all files within their respective subdir name while maintaining their extension.



From the parent directory I was using this command but I'm having issues with the file extensions:



for subdir in *; do mv $subdir/*  $subdir.mpeg; done;









share|improve this question
















I'm trying to figure out how to rename multiple files (100s) that have different extensions to their respective sub directory name. Here is the structure:



Parentdir
|--subdir1
|---name1.txt
|---name2.jpeg
|--name3.ovc
|--subdir2
|--name4.txt
|--name5.tiff
|-name6.mpeg


what I would like to do is batch process a rename of all files within their respective subdir name while maintaining their extension.



From the parent directory I was using this command but I'm having issues with the file extensions:



for subdir in *; do mv $subdir/*  $subdir.mpeg; done;






rename mv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 0:54







Rob Pomarico

















asked Dec 28 '18 at 19:28









Rob PomaricoRob Pomarico

32




32








  • 2





    What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?

    – PerlDuck
    Dec 28 '18 at 19:35








  • 1





    Possible duplicate of Search folder, find and copy files to new folder corresponding file ending

    – PerlDuck
    Dec 28 '18 at 19:42











  • Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )

    – Sergiy Kolodyazhnyy
    Dec 31 '18 at 9:54














  • 2





    What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?

    – PerlDuck
    Dec 28 '18 at 19:35








  • 1





    Possible duplicate of Search folder, find and copy files to new folder corresponding file ending

    – PerlDuck
    Dec 28 '18 at 19:42











  • Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )

    – Sergiy Kolodyazhnyy
    Dec 31 '18 at 9:54








2




2





What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?

– PerlDuck
Dec 28 '18 at 19:35







What is the "respective subdir"? Just the extension, like txt, jpeg, ovc, etc? In other words: what is the expected outcome?

– PerlDuck
Dec 28 '18 at 19:35






1




1





Possible duplicate of Search folder, find and copy files to new folder corresponding file ending

– PerlDuck
Dec 28 '18 at 19:42





Possible duplicate of Search folder, find and copy files to new folder corresponding file ending

– PerlDuck
Dec 28 '18 at 19:42













Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )

– Sergiy Kolodyazhnyy
Dec 31 '18 at 9:54





Your question is unclear. It would be helpful if you could add an example of old + new filename, or show example of new structure ( i.e. , edited example of what you have as structure in the question )

– Sergiy Kolodyazhnyy
Dec 31 '18 at 9:54










1 Answer
1






active

oldest

votes


















0














I conjured this script that can help to achieve what you want:



#!/usr/bin/env bash
set -e

for i in "$1"/*/*
do
old="${i##*/}"
ext="${i##*.}"
name=$(basename "${i%/*}")
[ ! -d "$i" ] && [ ! -f "$1/$name/$name.$ext" ] && mv "$1/$name/$old" "$1/$name/$name.$ext"

done


Test:



parentdir
├── subdir1
│   ├── name1.ovc
│   └── name1.txt
└── subdir2
├── name2.ovc
└── name2.txt


Results:



parentdir
├── subdir1
│   ├── subdir1.ovc
│   └── subdir1.txt
└── subdir2
├── subdir2.ovc
└── subdir2.txt


Usage: ./script.sh Parentdir



Information:





  • old="${i##*/}": returns filename to change (name1.ovc)


  • ext="${i##*.}": returns file extension (mpeg, txt)


  • name=$(basename "${i%/*}"): returns immediate parent directory of target
    file (subdir1)


  • [ ! -d "$i" ]: check if not a directory


  • [ ! -f "$1/$name/$name.$ext" ]: Check if the file already exists and has been renamed already






share|improve this answer


























  • This did the trick, thanks George

    – Rob Pomarico
    Jan 4 at 0:53











  • Hi George, thanks again and I have one more question. I ran the script and sometimes the file already has the same name as the directory it is in. I get a 'same name' message and the script stops. How can I modify to skip the file and move on to the next one?

    – Rob Pomarico
    Jan 4 at 5:44











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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%2faskubuntu.com%2fquestions%2f1105196%2frename-multiple-files-of-different-name-and-extensions-with-parent-directory-nam%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









0














I conjured this script that can help to achieve what you want:



#!/usr/bin/env bash
set -e

for i in "$1"/*/*
do
old="${i##*/}"
ext="${i##*.}"
name=$(basename "${i%/*}")
[ ! -d "$i" ] && [ ! -f "$1/$name/$name.$ext" ] && mv "$1/$name/$old" "$1/$name/$name.$ext"

done


Test:



parentdir
├── subdir1
│   ├── name1.ovc
│   └── name1.txt
└── subdir2
├── name2.ovc
└── name2.txt


Results:



parentdir
├── subdir1
│   ├── subdir1.ovc
│   └── subdir1.txt
└── subdir2
├── subdir2.ovc
└── subdir2.txt


Usage: ./script.sh Parentdir



Information:





  • old="${i##*/}": returns filename to change (name1.ovc)


  • ext="${i##*.}": returns file extension (mpeg, txt)


  • name=$(basename "${i%/*}"): returns immediate parent directory of target
    file (subdir1)


  • [ ! -d "$i" ]: check if not a directory


  • [ ! -f "$1/$name/$name.$ext" ]: Check if the file already exists and has been renamed already






share|improve this answer


























  • This did the trick, thanks George

    – Rob Pomarico
    Jan 4 at 0:53











  • Hi George, thanks again and I have one more question. I ran the script and sometimes the file already has the same name as the directory it is in. I get a 'same name' message and the script stops. How can I modify to skip the file and move on to the next one?

    – Rob Pomarico
    Jan 4 at 5:44
















0














I conjured this script that can help to achieve what you want:



#!/usr/bin/env bash
set -e

for i in "$1"/*/*
do
old="${i##*/}"
ext="${i##*.}"
name=$(basename "${i%/*}")
[ ! -d "$i" ] && [ ! -f "$1/$name/$name.$ext" ] && mv "$1/$name/$old" "$1/$name/$name.$ext"

done


Test:



parentdir
├── subdir1
│   ├── name1.ovc
│   └── name1.txt
└── subdir2
├── name2.ovc
└── name2.txt


Results:



parentdir
├── subdir1
│   ├── subdir1.ovc
│   └── subdir1.txt
└── subdir2
├── subdir2.ovc
└── subdir2.txt


Usage: ./script.sh Parentdir



Information:





  • old="${i##*/}": returns filename to change (name1.ovc)


  • ext="${i##*.}": returns file extension (mpeg, txt)


  • name=$(basename "${i%/*}"): returns immediate parent directory of target
    file (subdir1)


  • [ ! -d "$i" ]: check if not a directory


  • [ ! -f "$1/$name/$name.$ext" ]: Check if the file already exists and has been renamed already






share|improve this answer


























  • This did the trick, thanks George

    – Rob Pomarico
    Jan 4 at 0:53











  • Hi George, thanks again and I have one more question. I ran the script and sometimes the file already has the same name as the directory it is in. I get a 'same name' message and the script stops. How can I modify to skip the file and move on to the next one?

    – Rob Pomarico
    Jan 4 at 5:44














0












0








0







I conjured this script that can help to achieve what you want:



#!/usr/bin/env bash
set -e

for i in "$1"/*/*
do
old="${i##*/}"
ext="${i##*.}"
name=$(basename "${i%/*}")
[ ! -d "$i" ] && [ ! -f "$1/$name/$name.$ext" ] && mv "$1/$name/$old" "$1/$name/$name.$ext"

done


Test:



parentdir
├── subdir1
│   ├── name1.ovc
│   └── name1.txt
└── subdir2
├── name2.ovc
└── name2.txt


Results:



parentdir
├── subdir1
│   ├── subdir1.ovc
│   └── subdir1.txt
└── subdir2
├── subdir2.ovc
└── subdir2.txt


Usage: ./script.sh Parentdir



Information:





  • old="${i##*/}": returns filename to change (name1.ovc)


  • ext="${i##*.}": returns file extension (mpeg, txt)


  • name=$(basename "${i%/*}"): returns immediate parent directory of target
    file (subdir1)


  • [ ! -d "$i" ]: check if not a directory


  • [ ! -f "$1/$name/$name.$ext" ]: Check if the file already exists and has been renamed already






share|improve this answer















I conjured this script that can help to achieve what you want:



#!/usr/bin/env bash
set -e

for i in "$1"/*/*
do
old="${i##*/}"
ext="${i##*.}"
name=$(basename "${i%/*}")
[ ! -d "$i" ] && [ ! -f "$1/$name/$name.$ext" ] && mv "$1/$name/$old" "$1/$name/$name.$ext"

done


Test:



parentdir
├── subdir1
│   ├── name1.ovc
│   └── name1.txt
└── subdir2
├── name2.ovc
└── name2.txt


Results:



parentdir
├── subdir1
│   ├── subdir1.ovc
│   └── subdir1.txt
└── subdir2
├── subdir2.ovc
└── subdir2.txt


Usage: ./script.sh Parentdir



Information:





  • old="${i##*/}": returns filename to change (name1.ovc)


  • ext="${i##*.}": returns file extension (mpeg, txt)


  • name=$(basename "${i%/*}"): returns immediate parent directory of target
    file (subdir1)


  • [ ! -d "$i" ]: check if not a directory


  • [ ! -f "$1/$name/$name.$ext" ]: Check if the file already exists and has been renamed already







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 10 at 19:23

























answered Dec 28 '18 at 20:26









George UdosenGeorge Udosen

20.5k94467




20.5k94467













  • This did the trick, thanks George

    – Rob Pomarico
    Jan 4 at 0:53











  • Hi George, thanks again and I have one more question. I ran the script and sometimes the file already has the same name as the directory it is in. I get a 'same name' message and the script stops. How can I modify to skip the file and move on to the next one?

    – Rob Pomarico
    Jan 4 at 5:44



















  • This did the trick, thanks George

    – Rob Pomarico
    Jan 4 at 0:53











  • Hi George, thanks again and I have one more question. I ran the script and sometimes the file already has the same name as the directory it is in. I get a 'same name' message and the script stops. How can I modify to skip the file and move on to the next one?

    – Rob Pomarico
    Jan 4 at 5:44

















This did the trick, thanks George

– Rob Pomarico
Jan 4 at 0:53





This did the trick, thanks George

– Rob Pomarico
Jan 4 at 0:53













Hi George, thanks again and I have one more question. I ran the script and sometimes the file already has the same name as the directory it is in. I get a 'same name' message and the script stops. How can I modify to skip the file and move on to the next one?

– Rob Pomarico
Jan 4 at 5:44





Hi George, thanks again and I have one more question. I ran the script and sometimes the file already has the same name as the directory it is in. I get a 'same name' message and the script stops. How can I modify to skip the file and move on to the next one?

– Rob Pomarico
Jan 4 at 5:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • 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%2faskubuntu.com%2fquestions%2f1105196%2frename-multiple-files-of-different-name-and-extensions-with-parent-directory-nam%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

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei