Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create several files from a list in a text file?

Tags:

bash

Say I have a list in a file which contains filenames (without spaces):

filename
othername
somethingelse
...
  • Each line followed by a CR, One "filename" per line

Is there some kind of touch script I can use to create hundreds of files with titles set by a list?

It would also be incredibly helpful if I could fill each file with content upon creation.

Any bashers have any tips: I'm using Ubuntu 10.04

Thanks

like image 947
TryTryAgain Avatar asked Dec 22 '11 02:12

TryTryAgain


People also ask

How do I get a list of files in a folder from text?

Right-click that folder and select Show more options. Click Copy File List to Clipboard on the classic menu. You'll still need to paste the copied list into a text file. Launch Run, type Notepad in the Open box, and click OK.

How do I create multiple blank text files in a folder?

Windows 10 Microsoft provides a way of creating a new, blank text file using the right-click menu in File Explorer. Open File Explorer and navigate to the folder where you want to create the text file. Right-click in the folder and go to New > Text Document. The text file is given a default name, New Text Document.

How do I select multiple files in a folder from a list of file names?

Click the first file or folder, and then press and hold the Ctrl key. While holding Ctrl , click each of the other files or folders you want to select.


1 Answers

To simply create the files, assuming that file_full_of_files_names is a text file with whitespace-delimited filenames:

cat file_full_of_files_names | xargs touch

To actually fill them with content first, from a file called initial_content:

cat file_full_of_files_names | tr ' \t' '\n\n' | while read filename; do
    if test -f "$filename"; then
       echo "Skipping \"$filename\", it already exists"
    else
       cp -i initial_content "$filename"
    fi
done
like image 119
Aaron McDaid Avatar answered Nov 15 '22 03:11

Aaron McDaid