Bash script for writing ls to a file in another directory

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question






















  • 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




    @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














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?







share|improve this question






















  • 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




    @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












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?







share|improve this question














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?









share|improve this question













share|improve this question




share|improve this question








edited Feb 26 at 18:24









Maplicant

183




183










asked Feb 26 at 17:16









K.Mazur

11




11











  • 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




    @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
















  • 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




    @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















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










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





share|improve this answer


















  • 1




    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











  • 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











  • 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


















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 command cd is successful before the ls command is run.





share|improve this answer






















  • 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










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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





share|improve this answer


















  • 1




    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











  • 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











  • 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















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





share|improve this answer


















  • 1




    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











  • 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











  • 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













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 26 at 19:14

























answered Feb 26 at 17:22









DopeGhoti

40.2k54779




40.2k54779







  • 1




    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











  • 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











  • 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




    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











  • 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











  • 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













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 command cd is successful before the ls command is run.





share|improve this answer






















  • 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














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 command cd is successful before the ls command is run.





share|improve this answer






















  • 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












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 command cd is successful before the ls command is run.





share|improve this answer














Change you syntax:



cd cwiczenia | ls >> cd /home/kai/listaplikow.txt


to



cd cwiczenia && ls >> /home/kai/listaplikow.txt


Explanation:




  • &&: ensure that the previous command cd is successful before the ls command is run.






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay