how to change directory for folders seriallyGet the source directory of a Bash script from within the script itselfHow do I parse command line arguments in Bash?How to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?How to count all the lines of code in a directory recursively?How to concatenate string variables in BashHow to change the output color of echo in LinuxTar a directory, but don't store full absolute paths in the archive
English word for "product of tinkering"
Character descriptions
How can I tell the difference between unmarked sugar and stevia?
Project Euler #7 10001st prime in C++
Passing multiple files through stdin (over ssh)
Is open-sourcing the code of a webapp not recommended?
1980s live-action movie where individually-coloured nations on clouds fight
Difference between > and >> when used with a named pipe
Does an ice chest packed full of frozen food need ice?
Soft question: Examples where lack of mathematical rigour cause security breaches?
C++ Arduino IDE receiving garbled `char` from function
What do abbreviations in movie scripts stand for?
How come the nude protesters were not arrested?
What makes Ada the language of choice for the ISS's safety-critical systems?
Overlapping String-Blocks
How do governments keep track of their issued currency?
SOQL Not Recognizing Field?
SQL counting distinct over partition
Share calendar details request from manager's manager
What to do when surprise and a high initiative roll conflict with the narrative?
Using "subway" as name for London Underground?
Are there downsides to using std::string as a buffer?
Why didn't Voldemort recognize that Dumbledore was affected by his curse?
Were Alexander the Great and Hephaestion lovers?
how to change directory for folders serially
Get the source directory of a Bash script from within the script itselfHow do I parse command line arguments in Bash?How to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?How to count all the lines of code in a directory recursively?How to concatenate string variables in BashHow to change the output color of echo in LinuxTar a directory, but don't store full absolute paths in the archive
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have several sub folders in a directory (dir.1 dir.2 dir.3 etc) containing files (p.dat and c.dat) from which I want to extract part of the content to a.dat. How can I adjust my directory path to read p.dat and c.dat in all the sub directories, serially?
I set dir=dir.* (where * indicate the series)
#!/bin/sh
for dir in `ls | grep dir`
do
cd $dir
sed -n '1p' p.dat | awk 'printf("%15.3f",(($2-$1)/2+$1)*10-200)' >> ../all.dat
sed -n '2p' p.dat | awk 'printf("%15.3f",(($2-$1)/2+$1)*10-200)' >> ../all.dat
outkey c.dat >> ../a.dat
cd ..
done
sort all.dat -o temp.dat
mv temp.dat a.dat
#more a.dat
bash
|
show 7 more comments
I have several sub folders in a directory (dir.1 dir.2 dir.3 etc) containing files (p.dat and c.dat) from which I want to extract part of the content to a.dat. How can I adjust my directory path to read p.dat and c.dat in all the sub directories, serially?
I set dir=dir.* (where * indicate the series)
#!/bin/sh
for dir in `ls | grep dir`
do
cd $dir
sed -n '1p' p.dat | awk 'printf("%15.3f",(($2-$1)/2+$1)*10-200)' >> ../all.dat
sed -n '2p' p.dat | awk 'printf("%15.3f",(($2-$1)/2+$1)*10-200)' >> ../all.dat
outkey c.dat >> ../a.dat
cd ..
done
sort all.dat -o temp.dat
mv temp.dat a.dat
#more a.dat
bash
1
for dir in dir*
– Cyrus
Mar 24 at 17:20
2
...and all the bugs that shellcheck.net finds, which explicitly includes both quoting your expansion --cd "$dir"
is necessary to correctly handle directory names with characters in IFS or which could be parsed as globs -- and checking the exit status ofcd
(cd "$dir" || continue
). Much more efficient, btw, to put>../all.dat
on the same line immediately after thedone
; then everything in the loop appends to that file, opening it just once rather than re-opening it over and over.
– Charles Duffy
Mar 24 at 17:32
@CharlesDuffy, thanks for your comment. Do you suggest cd "$dir" || continue to replace cd $dir; and >../all.dat immediately after done (same line)? when I tried this, this result shows 'cd: $dir: no such file or directory'.
– geomarine
Mar 24 at 18:12
1
That error message makes it look like you used the wrong kind of quotes.cd "$dir" || continue
(the correct form) is notcd '$dir' || continue
.
– Charles Duffy
Mar 24 at 18:14
1
It's not clear from your last comment how you applied Cyrus's advice. It should just befor dir in dir*; do
, with nols
orgrep
anywhere. See Why you shouldn't parse the output ofls
.
– Charles Duffy
Mar 24 at 18:15
|
show 7 more comments
I have several sub folders in a directory (dir.1 dir.2 dir.3 etc) containing files (p.dat and c.dat) from which I want to extract part of the content to a.dat. How can I adjust my directory path to read p.dat and c.dat in all the sub directories, serially?
I set dir=dir.* (where * indicate the series)
#!/bin/sh
for dir in `ls | grep dir`
do
cd $dir
sed -n '1p' p.dat | awk 'printf("%15.3f",(($2-$1)/2+$1)*10-200)' >> ../all.dat
sed -n '2p' p.dat | awk 'printf("%15.3f",(($2-$1)/2+$1)*10-200)' >> ../all.dat
outkey c.dat >> ../a.dat
cd ..
done
sort all.dat -o temp.dat
mv temp.dat a.dat
#more a.dat
bash
I have several sub folders in a directory (dir.1 dir.2 dir.3 etc) containing files (p.dat and c.dat) from which I want to extract part of the content to a.dat. How can I adjust my directory path to read p.dat and c.dat in all the sub directories, serially?
I set dir=dir.* (where * indicate the series)
#!/bin/sh
for dir in `ls | grep dir`
do
cd $dir
sed -n '1p' p.dat | awk 'printf("%15.3f",(($2-$1)/2+$1)*10-200)' >> ../all.dat
sed -n '2p' p.dat | awk 'printf("%15.3f",(($2-$1)/2+$1)*10-200)' >> ../all.dat
outkey c.dat >> ../a.dat
cd ..
done
sort all.dat -o temp.dat
mv temp.dat a.dat
#more a.dat
bash
bash
edited Mar 25 at 9:20
Dominique
2,81552147
2,81552147
asked Mar 24 at 17:15
geomarinegeomarine
63
63
1
for dir in dir*
– Cyrus
Mar 24 at 17:20
2
...and all the bugs that shellcheck.net finds, which explicitly includes both quoting your expansion --cd "$dir"
is necessary to correctly handle directory names with characters in IFS or which could be parsed as globs -- and checking the exit status ofcd
(cd "$dir" || continue
). Much more efficient, btw, to put>../all.dat
on the same line immediately after thedone
; then everything in the loop appends to that file, opening it just once rather than re-opening it over and over.
– Charles Duffy
Mar 24 at 17:32
@CharlesDuffy, thanks for your comment. Do you suggest cd "$dir" || continue to replace cd $dir; and >../all.dat immediately after done (same line)? when I tried this, this result shows 'cd: $dir: no such file or directory'.
– geomarine
Mar 24 at 18:12
1
That error message makes it look like you used the wrong kind of quotes.cd "$dir" || continue
(the correct form) is notcd '$dir' || continue
.
– Charles Duffy
Mar 24 at 18:14
1
It's not clear from your last comment how you applied Cyrus's advice. It should just befor dir in dir*; do
, with nols
orgrep
anywhere. See Why you shouldn't parse the output ofls
.
– Charles Duffy
Mar 24 at 18:15
|
show 7 more comments
1
for dir in dir*
– Cyrus
Mar 24 at 17:20
2
...and all the bugs that shellcheck.net finds, which explicitly includes both quoting your expansion --cd "$dir"
is necessary to correctly handle directory names with characters in IFS or which could be parsed as globs -- and checking the exit status ofcd
(cd "$dir" || continue
). Much more efficient, btw, to put>../all.dat
on the same line immediately after thedone
; then everything in the loop appends to that file, opening it just once rather than re-opening it over and over.
– Charles Duffy
Mar 24 at 17:32
@CharlesDuffy, thanks for your comment. Do you suggest cd "$dir" || continue to replace cd $dir; and >../all.dat immediately after done (same line)? when I tried this, this result shows 'cd: $dir: no such file or directory'.
– geomarine
Mar 24 at 18:12
1
That error message makes it look like you used the wrong kind of quotes.cd "$dir" || continue
(the correct form) is notcd '$dir' || continue
.
– Charles Duffy
Mar 24 at 18:14
1
It's not clear from your last comment how you applied Cyrus's advice. It should just befor dir in dir*; do
, with nols
orgrep
anywhere. See Why you shouldn't parse the output ofls
.
– Charles Duffy
Mar 24 at 18:15
1
1
for dir in dir*
– Cyrus
Mar 24 at 17:20
for dir in dir*
– Cyrus
Mar 24 at 17:20
2
2
...and all the bugs that shellcheck.net finds, which explicitly includes both quoting your expansion --
cd "$dir"
is necessary to correctly handle directory names with characters in IFS or which could be parsed as globs -- and checking the exit status of cd
(cd "$dir" || continue
). Much more efficient, btw, to put >../all.dat
on the same line immediately after the done
; then everything in the loop appends to that file, opening it just once rather than re-opening it over and over.– Charles Duffy
Mar 24 at 17:32
...and all the bugs that shellcheck.net finds, which explicitly includes both quoting your expansion --
cd "$dir"
is necessary to correctly handle directory names with characters in IFS or which could be parsed as globs -- and checking the exit status of cd
(cd "$dir" || continue
). Much more efficient, btw, to put >../all.dat
on the same line immediately after the done
; then everything in the loop appends to that file, opening it just once rather than re-opening it over and over.– Charles Duffy
Mar 24 at 17:32
@CharlesDuffy, thanks for your comment. Do you suggest cd "$dir" || continue to replace cd $dir; and >../all.dat immediately after done (same line)? when I tried this, this result shows 'cd: $dir: no such file or directory'.
– geomarine
Mar 24 at 18:12
@CharlesDuffy, thanks for your comment. Do you suggest cd "$dir" || continue to replace cd $dir; and >../all.dat immediately after done (same line)? when I tried this, this result shows 'cd: $dir: no such file or directory'.
– geomarine
Mar 24 at 18:12
1
1
That error message makes it look like you used the wrong kind of quotes.
cd "$dir" || continue
(the correct form) is not cd '$dir' || continue
.– Charles Duffy
Mar 24 at 18:14
That error message makes it look like you used the wrong kind of quotes.
cd "$dir" || continue
(the correct form) is not cd '$dir' || continue
.– Charles Duffy
Mar 24 at 18:14
1
1
It's not clear from your last comment how you applied Cyrus's advice. It should just be
for dir in dir*; do
, with no ls
or grep
anywhere. See Why you shouldn't parse the output of ls
.– Charles Duffy
Mar 24 at 18:15
It's not clear from your last comment how you applied Cyrus's advice. It should just be
for dir in dir*; do
, with no ls
or grep
anywhere. See Why you shouldn't parse the output of ls
.– Charles Duffy
Mar 24 at 18:15
|
show 7 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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
);
);
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%2fstackoverflow.com%2fquestions%2f55326392%2fhow-to-change-directory-for-folders-serially%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55326392%2fhow-to-change-directory-for-folders-serially%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
1
for dir in dir*
– Cyrus
Mar 24 at 17:20
2
...and all the bugs that shellcheck.net finds, which explicitly includes both quoting your expansion --
cd "$dir"
is necessary to correctly handle directory names with characters in IFS or which could be parsed as globs -- and checking the exit status ofcd
(cd "$dir" || continue
). Much more efficient, btw, to put>../all.dat
on the same line immediately after thedone
; then everything in the loop appends to that file, opening it just once rather than re-opening it over and over.– Charles Duffy
Mar 24 at 17:32
@CharlesDuffy, thanks for your comment. Do you suggest cd "$dir" || continue to replace cd $dir; and >../all.dat immediately after done (same line)? when I tried this, this result shows 'cd: $dir: no such file or directory'.
– geomarine
Mar 24 at 18:12
1
That error message makes it look like you used the wrong kind of quotes.
cd "$dir" || continue
(the correct form) is notcd '$dir' || continue
.– Charles Duffy
Mar 24 at 18:14
1
It's not clear from your last comment how you applied Cyrus's advice. It should just be
for dir in dir*; do
, with nols
orgrep
anywhere. See Why you shouldn't parse the output ofls
.– Charles Duffy
Mar 24 at 18:15