Bash script for writing ls to a file in another directory
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm trying to write a script that will go to specific directory, ls
files in this directory and then write it to a file in another directory. This is what I came up with so far:
#!/bin/bash
getDisk1()
cd cwiczenia
getDisk2() ls >> cd /home/kai/listaplikow.txt
return
getDisk1
getDisk2
I'm running the script from the folder /home/kai/
, but getDisk2
isnt writing any results to its destination. Can anyone please help me with this?
bash shell-script
add a comment |Â
up vote
0
down vote
favorite
I'm trying to write a script that will go to specific directory, ls
files in this directory and then write it to a file in another directory. This is what I came up with so far:
#!/bin/bash
getDisk1()
cd cwiczenia
getDisk2() ls >> cd /home/kai/listaplikow.txt
return
getDisk1
getDisk2
I'm running the script from the folder /home/kai/
, but getDisk2
isnt writing any results to its destination. Can anyone please help me with this?
bash shell-script
Change thiscd cwiczenia | ls >> cd /home/kai/listaplikow.txt
tocd cwiczenia && ls >> /home/kai/listaplikow.txt
â George Udosen
Feb 26 at 17:18
1
@Jesse_b can you explain the syntaxcd cwiczenia | ls >> cd /home/kai/listaplikow.txt
?
â George Udosen
Feb 26 at 17:21
@GeorgeUdosen Duh. I wasn't even paying attention to the pipe. I just was trying to point out he was writing to a file named cd.
â Jesse_b
Feb 26 at 17:24
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to write a script that will go to specific directory, ls
files in this directory and then write it to a file in another directory. This is what I came up with so far:
#!/bin/bash
getDisk1()
cd cwiczenia
getDisk2() ls >> cd /home/kai/listaplikow.txt
return
getDisk1
getDisk2
I'm running the script from the folder /home/kai/
, but getDisk2
isnt writing any results to its destination. Can anyone please help me with this?
bash shell-script
I'm trying to write a script that will go to specific directory, ls
files in this directory and then write it to a file in another directory. This is what I came up with so far:
#!/bin/bash
getDisk1()
cd cwiczenia
getDisk2() ls >> cd /home/kai/listaplikow.txt
return
getDisk1
getDisk2
I'm running the script from the folder /home/kai/
, but getDisk2
isnt writing any results to its destination. Can anyone please help me with this?
bash shell-script
edited Feb 26 at 18:24
Maplicant
183
183
asked Feb 26 at 17:16
K.Mazur
11
11
Change thiscd cwiczenia | ls >> cd /home/kai/listaplikow.txt
tocd cwiczenia && ls >> /home/kai/listaplikow.txt
â George Udosen
Feb 26 at 17:18
1
@Jesse_b can you explain the syntaxcd cwiczenia | ls >> cd /home/kai/listaplikow.txt
?
â George Udosen
Feb 26 at 17:21
@GeorgeUdosen Duh. I wasn't even paying attention to the pipe. I just was trying to point out he was writing to a file named cd.
â Jesse_b
Feb 26 at 17:24
add a comment |Â
Change thiscd cwiczenia | ls >> cd /home/kai/listaplikow.txt
tocd cwiczenia && ls >> /home/kai/listaplikow.txt
â George Udosen
Feb 26 at 17:18
1
@Jesse_b can you explain the syntaxcd cwiczenia | ls >> cd /home/kai/listaplikow.txt
?
â George Udosen
Feb 26 at 17:21
@GeorgeUdosen Duh. I wasn't even paying attention to the pipe. I just was trying to point out he was writing to a file named cd.
â Jesse_b
Feb 26 at 17:24
Change this
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
to cd cwiczenia && ls >> /home/kai/listaplikow.txt
â George Udosen
Feb 26 at 17:18
Change this
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
to cd cwiczenia && ls >> /home/kai/listaplikow.txt
â George Udosen
Feb 26 at 17:18
1
1
@Jesse_b can you explain the syntax
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
?â George Udosen
Feb 26 at 17:21
@Jesse_b can you explain the syntax
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
?â George Udosen
Feb 26 at 17:21
@GeorgeUdosen Duh. I wasn't even paying attention to the pipe. I just was trying to point out he was writing to a file named cd.
â Jesse_b
Feb 26 at 17:24
@GeorgeUdosen Duh. I wasn't even paying attention to the pipe. I just was trying to point out he was writing to a file named cd.
â Jesse_b
Feb 26 at 17:24
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
Your commands are nonsense:
cd Pictures | ls >> cd /home/kai/listaplikow.txt
"Go into the Pictures
directory. Pipe the (null) output of the cd
command into ls
, which does not take standard input. Send ls
' output to a file called cd
."
What you probably want is something like:
( cd Pictures; ls >> /home/kai/listaplikow.txt )
The reason this is in parens is do that it executes in a subshell, and does not (try to) change the working directory of the underlying script.
Either way, it's a bad idea to parse the output of ls
in general. It might be better do do something like:
find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt
If you don't like seeing the path in which the files lie, it's still better to use find
to actually get the list of files:
find Pictures/ -maxdepth 1 -print0 | xargs -0 -L1 basename >> /home/kai/listaplikow.txt
1
It was that, orif cd Pictures; then ls; fi
(:
â DopeGhoti
Feb 26 at 17:26
I cannot give any points cause im new :( @DopeGhoti what does this mean find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt? Should i make it in functions?
â K.Mazur
Feb 26 at 17:27
also this one cd cwiczenia | ls >> cd /home/kai/listaplikow.txt works the way it is. Im new so dont judge :)
â K.Mazur
Feb 26 at 17:30
Note thatls >> cd /home/kai/listaplikow.txt
is directly equivalent to the more usualls /home/kai/listaplikow.txt >> cd
â roaima
Feb 26 at 17:31
the one with -maxdepth isnt the best solution cause it gives me : cwiczenia/something cwiczenia/somethingelse ... and i would just need something somethingelse is there better solution?
â K.Mazur
Feb 26 at 17:53
 |Â
show 1 more comment
up vote
0
down vote
Change you syntax:
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
to
cd cwiczenia && ls >> /home/kai/listaplikow.txt
Explanation:
&&
: ensure that the previous commandcd
is successful before thels
command is run.
Thank U @GeorgeUdosen i cannot +1 to anyone cause im new. Should i make in functions?
â K.Mazur
Feb 26 at 17:29
I think you can as the question belongs to you!
â George Udosen
Feb 26 at 17:33
@GeorgeUdosen voting up comes at 15 rep
â Jeff Schaller
Feb 26 at 18:23
its true @JeffSchaller
â K.Mazur
Feb 26 at 18:23
when i make it with function like in my example it says that cd: Pictures no such file or directory. both folders cwiczenia and Pictures are in home/kai/, so when im making first function and im going into cwiczenia folder and then make second function that goes to Pictures folder i should first make cd .. and then go into Pictures? cause i thought that my script is always in the folder that im executing it
â K.Mazur
Feb 26 at 18:36
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Your commands are nonsense:
cd Pictures | ls >> cd /home/kai/listaplikow.txt
"Go into the Pictures
directory. Pipe the (null) output of the cd
command into ls
, which does not take standard input. Send ls
' output to a file called cd
."
What you probably want is something like:
( cd Pictures; ls >> /home/kai/listaplikow.txt )
The reason this is in parens is do that it executes in a subshell, and does not (try to) change the working directory of the underlying script.
Either way, it's a bad idea to parse the output of ls
in general. It might be better do do something like:
find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt
If you don't like seeing the path in which the files lie, it's still better to use find
to actually get the list of files:
find Pictures/ -maxdepth 1 -print0 | xargs -0 -L1 basename >> /home/kai/listaplikow.txt
1
It was that, orif cd Pictures; then ls; fi
(:
â DopeGhoti
Feb 26 at 17:26
I cannot give any points cause im new :( @DopeGhoti what does this mean find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt? Should i make it in functions?
â K.Mazur
Feb 26 at 17:27
also this one cd cwiczenia | ls >> cd /home/kai/listaplikow.txt works the way it is. Im new so dont judge :)
â K.Mazur
Feb 26 at 17:30
Note thatls >> cd /home/kai/listaplikow.txt
is directly equivalent to the more usualls /home/kai/listaplikow.txt >> cd
â roaima
Feb 26 at 17:31
the one with -maxdepth isnt the best solution cause it gives me : cwiczenia/something cwiczenia/somethingelse ... and i would just need something somethingelse is there better solution?
â K.Mazur
Feb 26 at 17:53
 |Â
show 1 more comment
up vote
3
down vote
Your commands are nonsense:
cd Pictures | ls >> cd /home/kai/listaplikow.txt
"Go into the Pictures
directory. Pipe the (null) output of the cd
command into ls
, which does not take standard input. Send ls
' output to a file called cd
."
What you probably want is something like:
( cd Pictures; ls >> /home/kai/listaplikow.txt )
The reason this is in parens is do that it executes in a subshell, and does not (try to) change the working directory of the underlying script.
Either way, it's a bad idea to parse the output of ls
in general. It might be better do do something like:
find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt
If you don't like seeing the path in which the files lie, it's still better to use find
to actually get the list of files:
find Pictures/ -maxdepth 1 -print0 | xargs -0 -L1 basename >> /home/kai/listaplikow.txt
1
It was that, orif cd Pictures; then ls; fi
(:
â DopeGhoti
Feb 26 at 17:26
I cannot give any points cause im new :( @DopeGhoti what does this mean find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt? Should i make it in functions?
â K.Mazur
Feb 26 at 17:27
also this one cd cwiczenia | ls >> cd /home/kai/listaplikow.txt works the way it is. Im new so dont judge :)
â K.Mazur
Feb 26 at 17:30
Note thatls >> cd /home/kai/listaplikow.txt
is directly equivalent to the more usualls /home/kai/listaplikow.txt >> cd
â roaima
Feb 26 at 17:31
the one with -maxdepth isnt the best solution cause it gives me : cwiczenia/something cwiczenia/somethingelse ... and i would just need something somethingelse is there better solution?
â K.Mazur
Feb 26 at 17:53
 |Â
show 1 more comment
up vote
3
down vote
up vote
3
down vote
Your commands are nonsense:
cd Pictures | ls >> cd /home/kai/listaplikow.txt
"Go into the Pictures
directory. Pipe the (null) output of the cd
command into ls
, which does not take standard input. Send ls
' output to a file called cd
."
What you probably want is something like:
( cd Pictures; ls >> /home/kai/listaplikow.txt )
The reason this is in parens is do that it executes in a subshell, and does not (try to) change the working directory of the underlying script.
Either way, it's a bad idea to parse the output of ls
in general. It might be better do do something like:
find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt
If you don't like seeing the path in which the files lie, it's still better to use find
to actually get the list of files:
find Pictures/ -maxdepth 1 -print0 | xargs -0 -L1 basename >> /home/kai/listaplikow.txt
Your commands are nonsense:
cd Pictures | ls >> cd /home/kai/listaplikow.txt
"Go into the Pictures
directory. Pipe the (null) output of the cd
command into ls
, which does not take standard input. Send ls
' output to a file called cd
."
What you probably want is something like:
( cd Pictures; ls >> /home/kai/listaplikow.txt )
The reason this is in parens is do that it executes in a subshell, and does not (try to) change the working directory of the underlying script.
Either way, it's a bad idea to parse the output of ls
in general. It might be better do do something like:
find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt
If you don't like seeing the path in which the files lie, it's still better to use find
to actually get the list of files:
find Pictures/ -maxdepth 1 -print0 | xargs -0 -L1 basename >> /home/kai/listaplikow.txt
edited Feb 26 at 19:14
answered Feb 26 at 17:22
DopeGhoti
40.2k54779
40.2k54779
1
It was that, orif cd Pictures; then ls; fi
(:
â DopeGhoti
Feb 26 at 17:26
I cannot give any points cause im new :( @DopeGhoti what does this mean find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt? Should i make it in functions?
â K.Mazur
Feb 26 at 17:27
also this one cd cwiczenia | ls >> cd /home/kai/listaplikow.txt works the way it is. Im new so dont judge :)
â K.Mazur
Feb 26 at 17:30
Note thatls >> cd /home/kai/listaplikow.txt
is directly equivalent to the more usualls /home/kai/listaplikow.txt >> cd
â roaima
Feb 26 at 17:31
the one with -maxdepth isnt the best solution cause it gives me : cwiczenia/something cwiczenia/somethingelse ... and i would just need something somethingelse is there better solution?
â K.Mazur
Feb 26 at 17:53
 |Â
show 1 more comment
1
It was that, orif cd Pictures; then ls; fi
(:
â DopeGhoti
Feb 26 at 17:26
I cannot give any points cause im new :( @DopeGhoti what does this mean find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt? Should i make it in functions?
â K.Mazur
Feb 26 at 17:27
also this one cd cwiczenia | ls >> cd /home/kai/listaplikow.txt works the way it is. Im new so dont judge :)
â K.Mazur
Feb 26 at 17:30
Note thatls >> cd /home/kai/listaplikow.txt
is directly equivalent to the more usualls /home/kai/listaplikow.txt >> cd
â roaima
Feb 26 at 17:31
the one with -maxdepth isnt the best solution cause it gives me : cwiczenia/something cwiczenia/somethingelse ... and i would just need something somethingelse is there better solution?
â K.Mazur
Feb 26 at 17:53
1
1
It was that, or
if cd Pictures; then ls; fi
(:â DopeGhoti
Feb 26 at 17:26
It was that, or
if cd Pictures; then ls; fi
(:â DopeGhoti
Feb 26 at 17:26
I cannot give any points cause im new :( @DopeGhoti what does this mean find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt? Should i make it in functions?
â K.Mazur
Feb 26 at 17:27
I cannot give any points cause im new :( @DopeGhoti what does this mean find Pictures/ -maxdepth 1 >> /home/kai/listaplikow.txt? Should i make it in functions?
â K.Mazur
Feb 26 at 17:27
also this one cd cwiczenia | ls >> cd /home/kai/listaplikow.txt works the way it is. Im new so dont judge :)
â K.Mazur
Feb 26 at 17:30
also this one cd cwiczenia | ls >> cd /home/kai/listaplikow.txt works the way it is. Im new so dont judge :)
â K.Mazur
Feb 26 at 17:30
Note that
ls >> cd /home/kai/listaplikow.txt
is directly equivalent to the more usual ls /home/kai/listaplikow.txt >> cd
â roaima
Feb 26 at 17:31
Note that
ls >> cd /home/kai/listaplikow.txt
is directly equivalent to the more usual ls /home/kai/listaplikow.txt >> cd
â roaima
Feb 26 at 17:31
the one with -maxdepth isnt the best solution cause it gives me : cwiczenia/something cwiczenia/somethingelse ... and i would just need something somethingelse is there better solution?
â K.Mazur
Feb 26 at 17:53
the one with -maxdepth isnt the best solution cause it gives me : cwiczenia/something cwiczenia/somethingelse ... and i would just need something somethingelse is there better solution?
â K.Mazur
Feb 26 at 17:53
 |Â
show 1 more comment
up vote
0
down vote
Change you syntax:
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
to
cd cwiczenia && ls >> /home/kai/listaplikow.txt
Explanation:
&&
: ensure that the previous commandcd
is successful before thels
command is run.
Thank U @GeorgeUdosen i cannot +1 to anyone cause im new. Should i make in functions?
â K.Mazur
Feb 26 at 17:29
I think you can as the question belongs to you!
â George Udosen
Feb 26 at 17:33
@GeorgeUdosen voting up comes at 15 rep
â Jeff Schaller
Feb 26 at 18:23
its true @JeffSchaller
â K.Mazur
Feb 26 at 18:23
when i make it with function like in my example it says that cd: Pictures no such file or directory. both folders cwiczenia and Pictures are in home/kai/, so when im making first function and im going into cwiczenia folder and then make second function that goes to Pictures folder i should first make cd .. and then go into Pictures? cause i thought that my script is always in the folder that im executing it
â K.Mazur
Feb 26 at 18:36
add a comment |Â
up vote
0
down vote
Change you syntax:
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
to
cd cwiczenia && ls >> /home/kai/listaplikow.txt
Explanation:
&&
: ensure that the previous commandcd
is successful before thels
command is run.
Thank U @GeorgeUdosen i cannot +1 to anyone cause im new. Should i make in functions?
â K.Mazur
Feb 26 at 17:29
I think you can as the question belongs to you!
â George Udosen
Feb 26 at 17:33
@GeorgeUdosen voting up comes at 15 rep
â Jeff Schaller
Feb 26 at 18:23
its true @JeffSchaller
â K.Mazur
Feb 26 at 18:23
when i make it with function like in my example it says that cd: Pictures no such file or directory. both folders cwiczenia and Pictures are in home/kai/, so when im making first function and im going into cwiczenia folder and then make second function that goes to Pictures folder i should first make cd .. and then go into Pictures? cause i thought that my script is always in the folder that im executing it
â K.Mazur
Feb 26 at 18:36
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Change you syntax:
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
to
cd cwiczenia && ls >> /home/kai/listaplikow.txt
Explanation:
&&
: ensure that the previous commandcd
is successful before thels
command is run.
Change you syntax:
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
to
cd cwiczenia && ls >> /home/kai/listaplikow.txt
Explanation:
&&
: ensure that the previous commandcd
is successful before thels
command is run.
edited Feb 26 at 17:33
answered Feb 26 at 17:22
George Udosen
1,112318
1,112318
Thank U @GeorgeUdosen i cannot +1 to anyone cause im new. Should i make in functions?
â K.Mazur
Feb 26 at 17:29
I think you can as the question belongs to you!
â George Udosen
Feb 26 at 17:33
@GeorgeUdosen voting up comes at 15 rep
â Jeff Schaller
Feb 26 at 18:23
its true @JeffSchaller
â K.Mazur
Feb 26 at 18:23
when i make it with function like in my example it says that cd: Pictures no such file or directory. both folders cwiczenia and Pictures are in home/kai/, so when im making first function and im going into cwiczenia folder and then make second function that goes to Pictures folder i should first make cd .. and then go into Pictures? cause i thought that my script is always in the folder that im executing it
â K.Mazur
Feb 26 at 18:36
add a comment |Â
Thank U @GeorgeUdosen i cannot +1 to anyone cause im new. Should i make in functions?
â K.Mazur
Feb 26 at 17:29
I think you can as the question belongs to you!
â George Udosen
Feb 26 at 17:33
@GeorgeUdosen voting up comes at 15 rep
â Jeff Schaller
Feb 26 at 18:23
its true @JeffSchaller
â K.Mazur
Feb 26 at 18:23
when i make it with function like in my example it says that cd: Pictures no such file or directory. both folders cwiczenia and Pictures are in home/kai/, so when im making first function and im going into cwiczenia folder and then make second function that goes to Pictures folder i should first make cd .. and then go into Pictures? cause i thought that my script is always in the folder that im executing it
â K.Mazur
Feb 26 at 18:36
Thank U @GeorgeUdosen i cannot +1 to anyone cause im new. Should i make in functions?
â K.Mazur
Feb 26 at 17:29
Thank U @GeorgeUdosen i cannot +1 to anyone cause im new. Should i make in functions?
â K.Mazur
Feb 26 at 17:29
I think you can as the question belongs to you!
â George Udosen
Feb 26 at 17:33
I think you can as the question belongs to you!
â George Udosen
Feb 26 at 17:33
@GeorgeUdosen voting up comes at 15 rep
â Jeff Schaller
Feb 26 at 18:23
@GeorgeUdosen voting up comes at 15 rep
â Jeff Schaller
Feb 26 at 18:23
its true @JeffSchaller
â K.Mazur
Feb 26 at 18:23
its true @JeffSchaller
â K.Mazur
Feb 26 at 18:23
when i make it with function like in my example it says that cd: Pictures no such file or directory. both folders cwiczenia and Pictures are in home/kai/, so when im making first function and im going into cwiczenia folder and then make second function that goes to Pictures folder i should first make cd .. and then go into Pictures? cause i thought that my script is always in the folder that im executing it
â K.Mazur
Feb 26 at 18:36
when i make it with function like in my example it says that cd: Pictures no such file or directory. both folders cwiczenia and Pictures are in home/kai/, so when im making first function and im going into cwiczenia folder and then make second function that goes to Pictures folder i should first make cd .. and then go into Pictures? cause i thought that my script is always in the folder that im executing it
â K.Mazur
Feb 26 at 18:36
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426743%2fbash-script-for-writing-ls-to-a-file-in-another-directory%23new-answer', 'question_page');
);
Post as a guest
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
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
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
Change this
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
tocd cwiczenia && ls >> /home/kai/listaplikow.txt
â George Udosen
Feb 26 at 17:18
1
@Jesse_b can you explain the syntax
cd cwiczenia | ls >> cd /home/kai/listaplikow.txt
?â George Udosen
Feb 26 at 17:21
@GeorgeUdosen Duh. I wasn't even paying attention to the pipe. I just was trying to point out he was writing to a file named cd.
â Jesse_b
Feb 26 at 17:24