Tuesday, June 2, 2015

How to create files named after lines in a text file under Linux




I have a text file which contains a list of items (one for each line) and I need to create, in a given directory, a file for every entry in the said list; each one of these files should be named with a string in the said list. Also, if possible, after the string contained in the text file, a custom text should be appended to the filenames.



Which commands should I use to do that from the command line? I suppose that cat, grep and touch (and maybe xargs?) should be involved in the process, but after a few trials I still can't find the correct syntax.



Can someone enlighten me about this?


Answer



Say you have list.txt like this:



file 1
file 2

file 3


You can achieve what you want with:



while read FILE; do touch "${FILE} some string"; done < list.txt

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...