Creating a .bash file to test for loop
I am trying to loop through all files located in a directory ending in .txt and rename them (replacing beginning with end). However I would like to take the output from this for loop and pipe it to a bash file so that I can check the file and make sure what I have written does what I want it to, then I can just execute the .bash file if what I see fits the task. I've tried
for file in *.txt ; do mv $file ${file//beginning/end} ; done | rename.bash
however this renamed the file and created an empty rename.bash
command-line bash
New contributor
Morgan Gladden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I am trying to loop through all files located in a directory ending in .txt and rename them (replacing beginning with end). However I would like to take the output from this for loop and pipe it to a bash file so that I can check the file and make sure what I have written does what I want it to, then I can just execute the .bash file if what I see fits the task. I've tried
for file in *.txt ; do mv $file ${file//beginning/end} ; done | rename.bash
however this renamed the file and created an empty rename.bash
command-line bash
New contributor
Morgan Gladden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I am trying to loop through all files located in a directory ending in .txt and rename them (replacing beginning with end). However I would like to take the output from this for loop and pipe it to a bash file so that I can check the file and make sure what I have written does what I want it to, then I can just execute the .bash file if what I see fits the task. I've tried
for file in *.txt ; do mv $file ${file//beginning/end} ; done | rename.bash
however this renamed the file and created an empty rename.bash
command-line bash
New contributor
Morgan Gladden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am trying to loop through all files located in a directory ending in .txt and rename them (replacing beginning with end). However I would like to take the output from this for loop and pipe it to a bash file so that I can check the file and make sure what I have written does what I want it to, then I can just execute the .bash file if what I see fits the task. I've tried
for file in *.txt ; do mv $file ${file//beginning/end} ; done | rename.bash
however this renamed the file and created an empty rename.bash
command-line bash
command-line bash
New contributor
Morgan Gladden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Morgan Gladden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Morgan Gladden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Jan 3 at 19:38
Morgan GladdenMorgan Gladden
32
32
New contributor
Morgan Gladden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Morgan Gladden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Morgan Gladden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Put echo before mv. This will print the command instead of executing it.
As well:
Quote variables to avoid word splitting on spaces in the filenames. In this case you'll need to put quotes in quotes since you're outputting to a script.
I've never seen a
.bashextension in use, so I'd prefer.shinstead for the output script.You can't pipe directly to a file. Use output redirection
>instead. If you need to use a pipe, usetee.
In sum:
for file in *.txt; do
echo "mv '$file' '${file//beginning/end}'"
done > rename.sh
1
Why not.bash? I name all my bash scripts this way, I think it’s a good practice to have the interpreter as the script file extension, this way you see what’s ashscript and what’s abashone without opening the file.
– dessert
Jan 3 at 19:56
@dessert Because.shscripts are typical for Bourne-shell scripts in general and extensions are generally a preference of the user/author :) There exit several/bin/shor/bin/bashscripts on Ubuntu which don't use any extension at all either. A good example of that ison_ac_power
– Sergiy Kolodyazhnyy
Jan 3 at 20:06
Also generally a user shouldn't need to know if it's a bash script or not. As author you already know, so there's no point.
– Sergiy Kolodyazhnyy
Jan 3 at 20:12
1
@dessert Good point! A quick Google found points for .bash and for .sh (by convention). I guess ultimately it's personal preference. Personally I don't use it cause Gedit doesn't automatically syntax-highlight a .bash file unless it has a shebang. I might have also been thinking of the Python extension debate, where.py3files won't even be recognized as modules.
– wjandrea
Jan 3 at 20:34
add a comment |
Because mv doesn't output anything, and you can't pipe into a file.
What you can do is create a bash script using redirector:
for file in *.txt
do
echo "mv $file $(some command to manipulate the text)" >> script.bash
done
This will append the command to the file.
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
});
}
});
Morgan Gladden 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%2f1106697%2fcreating-a-bash-file-to-test-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Put echo before mv. This will print the command instead of executing it.
As well:
Quote variables to avoid word splitting on spaces in the filenames. In this case you'll need to put quotes in quotes since you're outputting to a script.
I've never seen a
.bashextension in use, so I'd prefer.shinstead for the output script.You can't pipe directly to a file. Use output redirection
>instead. If you need to use a pipe, usetee.
In sum:
for file in *.txt; do
echo "mv '$file' '${file//beginning/end}'"
done > rename.sh
1
Why not.bash? I name all my bash scripts this way, I think it’s a good practice to have the interpreter as the script file extension, this way you see what’s ashscript and what’s abashone without opening the file.
– dessert
Jan 3 at 19:56
@dessert Because.shscripts are typical for Bourne-shell scripts in general and extensions are generally a preference of the user/author :) There exit several/bin/shor/bin/bashscripts on Ubuntu which don't use any extension at all either. A good example of that ison_ac_power
– Sergiy Kolodyazhnyy
Jan 3 at 20:06
Also generally a user shouldn't need to know if it's a bash script or not. As author you already know, so there's no point.
– Sergiy Kolodyazhnyy
Jan 3 at 20:12
1
@dessert Good point! A quick Google found points for .bash and for .sh (by convention). I guess ultimately it's personal preference. Personally I don't use it cause Gedit doesn't automatically syntax-highlight a .bash file unless it has a shebang. I might have also been thinking of the Python extension debate, where.py3files won't even be recognized as modules.
– wjandrea
Jan 3 at 20:34
add a comment |
Put echo before mv. This will print the command instead of executing it.
As well:
Quote variables to avoid word splitting on spaces in the filenames. In this case you'll need to put quotes in quotes since you're outputting to a script.
I've never seen a
.bashextension in use, so I'd prefer.shinstead for the output script.You can't pipe directly to a file. Use output redirection
>instead. If you need to use a pipe, usetee.
In sum:
for file in *.txt; do
echo "mv '$file' '${file//beginning/end}'"
done > rename.sh
1
Why not.bash? I name all my bash scripts this way, I think it’s a good practice to have the interpreter as the script file extension, this way you see what’s ashscript and what’s abashone without opening the file.
– dessert
Jan 3 at 19:56
@dessert Because.shscripts are typical for Bourne-shell scripts in general and extensions are generally a preference of the user/author :) There exit several/bin/shor/bin/bashscripts on Ubuntu which don't use any extension at all either. A good example of that ison_ac_power
– Sergiy Kolodyazhnyy
Jan 3 at 20:06
Also generally a user shouldn't need to know if it's a bash script or not. As author you already know, so there's no point.
– Sergiy Kolodyazhnyy
Jan 3 at 20:12
1
@dessert Good point! A quick Google found points for .bash and for .sh (by convention). I guess ultimately it's personal preference. Personally I don't use it cause Gedit doesn't automatically syntax-highlight a .bash file unless it has a shebang. I might have also been thinking of the Python extension debate, where.py3files won't even be recognized as modules.
– wjandrea
Jan 3 at 20:34
add a comment |
Put echo before mv. This will print the command instead of executing it.
As well:
Quote variables to avoid word splitting on spaces in the filenames. In this case you'll need to put quotes in quotes since you're outputting to a script.
I've never seen a
.bashextension in use, so I'd prefer.shinstead for the output script.You can't pipe directly to a file. Use output redirection
>instead. If you need to use a pipe, usetee.
In sum:
for file in *.txt; do
echo "mv '$file' '${file//beginning/end}'"
done > rename.sh
Put echo before mv. This will print the command instead of executing it.
As well:
Quote variables to avoid word splitting on spaces in the filenames. In this case you'll need to put quotes in quotes since you're outputting to a script.
I've never seen a
.bashextension in use, so I'd prefer.shinstead for the output script.You can't pipe directly to a file. Use output redirection
>instead. If you need to use a pipe, usetee.
In sum:
for file in *.txt; do
echo "mv '$file' '${file//beginning/end}'"
done > rename.sh
edited Jan 3 at 19:55
answered Jan 3 at 19:45
wjandreawjandrea
8,47842259
8,47842259
1
Why not.bash? I name all my bash scripts this way, I think it’s a good practice to have the interpreter as the script file extension, this way you see what’s ashscript and what’s abashone without opening the file.
– dessert
Jan 3 at 19:56
@dessert Because.shscripts are typical for Bourne-shell scripts in general and extensions are generally a preference of the user/author :) There exit several/bin/shor/bin/bashscripts on Ubuntu which don't use any extension at all either. A good example of that ison_ac_power
– Sergiy Kolodyazhnyy
Jan 3 at 20:06
Also generally a user shouldn't need to know if it's a bash script or not. As author you already know, so there's no point.
– Sergiy Kolodyazhnyy
Jan 3 at 20:12
1
@dessert Good point! A quick Google found points for .bash and for .sh (by convention). I guess ultimately it's personal preference. Personally I don't use it cause Gedit doesn't automatically syntax-highlight a .bash file unless it has a shebang. I might have also been thinking of the Python extension debate, where.py3files won't even be recognized as modules.
– wjandrea
Jan 3 at 20:34
add a comment |
1
Why not.bash? I name all my bash scripts this way, I think it’s a good practice to have the interpreter as the script file extension, this way you see what’s ashscript and what’s abashone without opening the file.
– dessert
Jan 3 at 19:56
@dessert Because.shscripts are typical for Bourne-shell scripts in general and extensions are generally a preference of the user/author :) There exit several/bin/shor/bin/bashscripts on Ubuntu which don't use any extension at all either. A good example of that ison_ac_power
– Sergiy Kolodyazhnyy
Jan 3 at 20:06
Also generally a user shouldn't need to know if it's a bash script or not. As author you already know, so there's no point.
– Sergiy Kolodyazhnyy
Jan 3 at 20:12
1
@dessert Good point! A quick Google found points for .bash and for .sh (by convention). I guess ultimately it's personal preference. Personally I don't use it cause Gedit doesn't automatically syntax-highlight a .bash file unless it has a shebang. I might have also been thinking of the Python extension debate, where.py3files won't even be recognized as modules.
– wjandrea
Jan 3 at 20:34
1
1
Why not
.bash? I name all my bash scripts this way, I think it’s a good practice to have the interpreter as the script file extension, this way you see what’s a sh script and what’s a bash one without opening the file.– dessert
Jan 3 at 19:56
Why not
.bash? I name all my bash scripts this way, I think it’s a good practice to have the interpreter as the script file extension, this way you see what’s a sh script and what’s a bash one without opening the file.– dessert
Jan 3 at 19:56
@dessert Because
.sh scripts are typical for Bourne-shell scripts in general and extensions are generally a preference of the user/author :) There exit several /bin/sh or /bin/bash scripts on Ubuntu which don't use any extension at all either. A good example of that is on_ac_power– Sergiy Kolodyazhnyy
Jan 3 at 20:06
@dessert Because
.sh scripts are typical for Bourne-shell scripts in general and extensions are generally a preference of the user/author :) There exit several /bin/sh or /bin/bash scripts on Ubuntu which don't use any extension at all either. A good example of that is on_ac_power– Sergiy Kolodyazhnyy
Jan 3 at 20:06
Also generally a user shouldn't need to know if it's a bash script or not. As author you already know, so there's no point.
– Sergiy Kolodyazhnyy
Jan 3 at 20:12
Also generally a user shouldn't need to know if it's a bash script or not. As author you already know, so there's no point.
– Sergiy Kolodyazhnyy
Jan 3 at 20:12
1
1
@dessert Good point! A quick Google found points for .bash and for .sh (by convention). I guess ultimately it's personal preference. Personally I don't use it cause Gedit doesn't automatically syntax-highlight a .bash file unless it has a shebang. I might have also been thinking of the Python extension debate, where
.py3 files won't even be recognized as modules.– wjandrea
Jan 3 at 20:34
@dessert Good point! A quick Google found points for .bash and for .sh (by convention). I guess ultimately it's personal preference. Personally I don't use it cause Gedit doesn't automatically syntax-highlight a .bash file unless it has a shebang. I might have also been thinking of the Python extension debate, where
.py3 files won't even be recognized as modules.– wjandrea
Jan 3 at 20:34
add a comment |
Because mv doesn't output anything, and you can't pipe into a file.
What you can do is create a bash script using redirector:
for file in *.txt
do
echo "mv $file $(some command to manipulate the text)" >> script.bash
done
This will append the command to the file.
add a comment |
Because mv doesn't output anything, and you can't pipe into a file.
What you can do is create a bash script using redirector:
for file in *.txt
do
echo "mv $file $(some command to manipulate the text)" >> script.bash
done
This will append the command to the file.
add a comment |
Because mv doesn't output anything, and you can't pipe into a file.
What you can do is create a bash script using redirector:
for file in *.txt
do
echo "mv $file $(some command to manipulate the text)" >> script.bash
done
This will append the command to the file.
Because mv doesn't output anything, and you can't pipe into a file.
What you can do is create a bash script using redirector:
for file in *.txt
do
echo "mv $file $(some command to manipulate the text)" >> script.bash
done
This will append the command to the file.
answered Jan 3 at 19:45
vidarlovidarlo
9,38942445
9,38942445
add a comment |
add a comment |
Morgan Gladden is a new contributor. Be nice, and check out our Code of Conduct.
Morgan Gladden is a new contributor. Be nice, and check out our Code of Conduct.
Morgan Gladden is a new contributor. Be nice, and check out our Code of Conduct.
Morgan Gladden 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%2f1106697%2fcreating-a-bash-file-to-test-for-loop%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