Create Multiple Files and Name Them from Command Line

I need to create 300 blank files and put them in a folder on my server then name them from a list in a txt file, I have SSH access, so is it possible to do this using command line?

3 Answers

You can use xargs for this:

xargs -a file_list.txt touch

This supplies each line of the text file as the parameter to touch. There's no point in creating the files first, might as well just create the outright with the correct name from your text file.

If you wish to output to an arbitrary directory, eg PATH then try:

cat file_list.txt | xargs -I % touch PATH/%

0

For simple way to do it try.

touch $(cat filename.txt)
#!/bin/bash

while read filename; do
  touch $filename
done < filename.txt

This will iterate over each line of filename.txt and create empty file with name in the current line.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest