How do I loop through a file to create a series of new .docx files?

Viewed 53

Background

I have a file of file names like so:

something.txt
another.cpp
whoa.cxx
...

I want to create a corresponding text file for each one as such in the current directory:

./something.txt.docx
./another.cpp.docx
./whoa.cxx.docx

This should be a very simple operation...but the series of commands I am thinking of trying don't seem to make sense logically:

Attempted solutions

  1. Pipe a cat into touch : cat file | touch <not sure what to put here>.docx.

    a. As you can see, I am at a loss of how to append a .docx extension to each file name that I encounter, and I do not know a way to neatly do a regex append in this manner. The touch man page does not seem to be helpful on this front either.

  2. Pipe cat into xargs touch: cat file | xargs -I{} touch {}.docx

    a. My intention was to use the -I option to literally append .docx to get the result seen above. Below is the intended functionality of -I:

    -I replace-str

    Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1.

    This threw the error xargs: .docx: No such file or directory.

Question

How do loop through a file and create corresponding .docx's for each line in the file?

2 Answers

Assuming the file contents are reasonably well formatted and don't have strange corner cases, you can do this:

while IFS= read -r line; do
  touch "$line.docx"
done < input-file-name.txt

I just confirmed that it works on my system.

This can be achieved by below command

ls -1rt | awk '{ print "touch " $0 ".docx" }' | bash

Related