Rename multiple files of different name and extensions with parent directory name
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 with 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
New contributor
add a comment |
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 with 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
New contributor
2
What is the "respective subdir"? Just the extension, liketxt
,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
yesterday
add a comment |
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 with 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
New contributor
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 with 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
rename mv
New contributor
New contributor
edited yesterday
Sourav Ghosh
39729
39729
New contributor
asked Dec 28 '18 at 19:28
Rob Pomarico
1
1
New contributor
New contributor
2
What is the "respective subdir"? Just the extension, liketxt
,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
yesterday
add a comment |
2
What is the "respective subdir"? Just the extension, liketxt
,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
yesterday
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
yesterday
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
yesterday
add a comment |
1 Answer
1
active
oldest
votes
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" ] && 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
add a comment |
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
});
}
});
Rob Pomarico is a new contributor. Be nice, and check out our Code of Conduct.
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%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
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" ] && 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
add a comment |
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" ] && 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
add a comment |
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" ] && 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
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" ] && 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
edited Dec 28 '18 at 21:03
answered Dec 28 '18 at 20:26
George Udosen
19.8k94267
19.8k94267
add a comment |
add a comment |
Rob Pomarico is a new contributor. Be nice, and check out our Code of Conduct.
Rob Pomarico is a new contributor. Be nice, and check out our Code of Conduct.
Rob Pomarico is a new contributor. Be nice, and check out our Code of Conduct.
Rob Pomarico is a new contributor. Be nice, and check out our Code of Conduct.
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%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
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
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
yesterday