Search code examples
shelldatetimeterminalls

shell script connect three commands


I have shell script, and need to forward number of files and folders in some directory to .txt file, I did it with ls | wc -

but in that file need to be date and time when script was ran

ls | wc -l | date > prep.txt

in file prep i have only date not this first count of files and folders


Solution

  • date does not read its standard input; it makes no sense to pipe into it.

    date >prep.txt
    ls | wc -l >>prep.txt
    

    If you insist on a single redirection,

    { date; ls | wc -l; } >prep.txt
    

    You could also get really complicated;

    date +"%c $(ls | wc -l)" >prep.txt
    

    but there is really no reason to go there.