0

I trying to get the details of files inside several folders and add it to a file called aa.txt, but the content is overwritten when go to different folder.

How can I add it to the file without overwriting the previous content?

for folder in R1 R2 R3 R4 P1 P2 P3 P4 M1 M2 M3 M4 S1 S2 S3 S4
do
    cd $folder
    # run folowing commands in the folder

    ls -ltr | cat > ../aa.txt   
    echo " $folder done" >> ../Completed.text
    cd ..
done

2 Answers 2

2

You should append to the output file, using >>

ls -ltr | cat >> ../aa.txt
Sign up to request clarification or add additional context in comments.

Comments

1

In addition to Oscars' answer, you can also omit the for loop.

find R1 R2 R3 R4 P1 P2 P3 P4 M1 M2 M3 M4 S1 S2 S3 S4 -maxdepth 1 -type d -exec ls -lrt {} \; > aa.txt

The difference between > and >> is that the former clears the file before writing content, the latter appends content to the file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.