Change file name while copying based on source path
I have a large number of files in directories of the format */*/*/*/*.txt
and I would like to copy them into a different place while replacing the forward-slashes in the path with underscores. For example, if a file is located at A/B/C/D/E.txt
, I'd like to copy it to dest/
so that its path after copying is dest/A_B_C_D_E.txt
. Is this possible?
files rename
add a comment |
I have a large number of files in directories of the format */*/*/*/*.txt
and I would like to copy them into a different place while replacing the forward-slashes in the path with underscores. For example, if a file is located at A/B/C/D/E.txt
, I'd like to copy it to dest/
so that its path after copying is dest/A_B_C_D_E.txt
. Is this possible?
files rename
add a comment |
I have a large number of files in directories of the format */*/*/*/*.txt
and I would like to copy them into a different place while replacing the forward-slashes in the path with underscores. For example, if a file is located at A/B/C/D/E.txt
, I'd like to copy it to dest/
so that its path after copying is dest/A_B_C_D_E.txt
. Is this possible?
files rename
I have a large number of files in directories of the format */*/*/*/*.txt
and I would like to copy them into a different place while replacing the forward-slashes in the path with underscores. For example, if a file is located at A/B/C/D/E.txt
, I'd like to copy it to dest/
so that its path after copying is dest/A_B_C_D_E.txt
. Is this possible?
files rename
files rename
asked Jan 16 at 6:51
David ScottDavid Scott
132
132
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use script like this:
for i in `find . -type f -name "*.txt"`
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done
If the number of files if very big you can try with while
instead of for
while read i
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done < (find . -type f -name "*.txt")
P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers
3
This will break on a bunch of filenames.
– jordanm
Jan 16 at 6:58
@jordanm, can you please clarify?
– Romeo Ninov
Jan 16 at 6:59
2
@RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697
– Kusalananda
Jan 16 at 7:15
2
@RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.
– Kusalananda
Jan 16 at 7:24
1
I was able to fix the leading._
problem by adding to thenewfile
line:newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
– David Scott
Jan 17 at 2:20
|
show 4 more comments
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%2f494743%2fchange-file-name-while-copying-based-on-source-path%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
You can use script like this:
for i in `find . -type f -name "*.txt"`
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done
If the number of files if very big you can try with while
instead of for
while read i
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done < (find . -type f -name "*.txt")
P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers
3
This will break on a bunch of filenames.
– jordanm
Jan 16 at 6:58
@jordanm, can you please clarify?
– Romeo Ninov
Jan 16 at 6:59
2
@RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697
– Kusalananda
Jan 16 at 7:15
2
@RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.
– Kusalananda
Jan 16 at 7:24
1
I was able to fix the leading._
problem by adding to thenewfile
line:newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
– David Scott
Jan 17 at 2:20
|
show 4 more comments
You can use script like this:
for i in `find . -type f -name "*.txt"`
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done
If the number of files if very big you can try with while
instead of for
while read i
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done < (find . -type f -name "*.txt")
P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers
3
This will break on a bunch of filenames.
– jordanm
Jan 16 at 6:58
@jordanm, can you please clarify?
– Romeo Ninov
Jan 16 at 6:59
2
@RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697
– Kusalananda
Jan 16 at 7:15
2
@RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.
– Kusalananda
Jan 16 at 7:24
1
I was able to fix the leading._
problem by adding to thenewfile
line:newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
– David Scott
Jan 17 at 2:20
|
show 4 more comments
You can use script like this:
for i in `find . -type f -name "*.txt"`
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done
If the number of files if very big you can try with while
instead of for
while read i
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done < (find . -type f -name "*.txt")
P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers
You can use script like this:
for i in `find . -type f -name "*.txt"`
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done
If the number of files if very big you can try with while
instead of for
while read i
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done < (find . -type f -name "*.txt")
P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers
edited Jan 17 at 5:23
answered Jan 16 at 6:57
Romeo NinovRomeo Ninov
5,77331928
5,77331928
3
This will break on a bunch of filenames.
– jordanm
Jan 16 at 6:58
@jordanm, can you please clarify?
– Romeo Ninov
Jan 16 at 6:59
2
@RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697
– Kusalananda
Jan 16 at 7:15
2
@RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.
– Kusalananda
Jan 16 at 7:24
1
I was able to fix the leading._
problem by adding to thenewfile
line:newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
– David Scott
Jan 17 at 2:20
|
show 4 more comments
3
This will break on a bunch of filenames.
– jordanm
Jan 16 at 6:58
@jordanm, can you please clarify?
– Romeo Ninov
Jan 16 at 6:59
2
@RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697
– Kusalananda
Jan 16 at 7:15
2
@RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.
– Kusalananda
Jan 16 at 7:24
1
I was able to fix the leading._
problem by adding to thenewfile
line:newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
– David Scott
Jan 17 at 2:20
3
3
This will break on a bunch of filenames.
– jordanm
Jan 16 at 6:58
This will break on a bunch of filenames.
– jordanm
Jan 16 at 6:58
@jordanm, can you please clarify?
– Romeo Ninov
Jan 16 at 6:59
@jordanm, can you please clarify?
– Romeo Ninov
Jan 16 at 6:59
2
2
@RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697
– Kusalananda
Jan 16 at 7:15
@RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697
– Kusalananda
Jan 16 at 7:15
2
2
@RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.
– Kusalananda
Jan 16 at 7:24
@RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.
– Kusalananda
Jan 16 at 7:24
1
1
I was able to fix the leading
._
problem by adding to the newfile
line: newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
– David Scott
Jan 17 at 2:20
I was able to fix the leading
._
problem by adding to the newfile
line: newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
– David Scott
Jan 17 at 2:20
|
show 4 more comments
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%2f494743%2fchange-file-name-while-copying-based-on-source-path%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