2

I have many text files enumerated. I want to add missing text files and fill them with 0; e.g.,:

   -directory
     -1.txt
     -2.txt
     -6.txt

I want to add 3.txt, 4.txt, and 5.txt to the directory. Each text file contains one character, 0. Here's an example:

   -directory
     -1.txt
     -2.txt
     -3.txt
     -4.txt
     -5.txt
     -6.txt

Can this be done with a simple command?

Thank you.

6

Change 6 with whatever number you want.

touch {1..6}.txt

touchs the existing files and create an empty one for missing ones.

find . -name '*.txt' -size 0 -exec tee <<<"0" {} +

Adds number 0 to all empty files. remember that if your already existing files are empty it will add zero to all of them too.


A not efficient way:

for i in {1..100}.txt; do if [ ! -f "$i" ]; then echo 0 > "$i"; fi; done;

Looks for .txt files between 1 to 100 one by one and creates them if they're not there. Created files have one text character, "0"; existing files are not modified.

| improve this answer | |

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.