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:

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

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


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?

  • Change this cd cwiczenia | ls >> cd /home/kai/listaplikow.txt to cd cwiczenia && ls >> /home/kai/listaplikow.txt – George Udosen Feb 26 '18 at 17:18
  • 1
    @Jesse_b can you explain the syntax cd cwiczenia | ls >> cd /home/kai/listaplikow.txt? – George Udosen Feb 26 '18 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 '18 at 17:24

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, or if cd Pictures; then ls; fi (: – DopeGhoti Feb 26 '18 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 '18 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 '18 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 '18 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 '18 at 17:53

Change you syntax:

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


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


  • &&: ensure that the previous command cd is successful before the ls command is run.
  • Thank U @GeorgeUdosen i cannot +1 to anyone cause im new. Should i make in functions? – K.Mazur Feb 26 '18 at 17:29
  • I think you can as the question belongs to you! – George Udosen Feb 26 '18 at 17:33
  • @GeorgeUdosen voting up comes at 15 rep – Jeff Schaller Feb 26 '18 at 18:23
  • its true @JeffSchaller – K.Mazur Feb 26 '18 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 '18 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.