No Linux No Life – Powerful File Generator

Live smart.

One of the standout aspects of Linux that sets it apart from other major operating systems is its robust automation capabilities. This remarkable feature is made possible by an intriguing fusion of commands, allowing you to live smarter and streamline your tech-driven life.

This command iterates over each line of a file that contains a list of file names and creates actual files using the corresponding names.

while IFS= read -r filename; do touch "$filename"; done < sample.txt

Let’s break down the command:

  • while starts a loop that reads each line from the sample.txt file.
  • IFS= ensures that leading and trailing whitespace in each line is preserved during the read operation.
  • read -r filename reads a line from the file and assigns it to the filename variable.
  • ; separates the individual commands within the loop.
  • touch "$filename" creates a file with the name stored in the filename variable.
  • done marks the end of the loop.
  • < sample.txt redirects the contents of sample.txt as input for the loop.

By running this command, the script will read each line from sample.txt and create a file with the corresponding name. For example, if sample.txt contains the following lines:

AAA
BBB
CCC

The command will create three files named AAA, BBB, and CCC in the current directory.

Please note that if any file with the same name already exists in the directory, the touch command will update the access and modification times of that file instead of creating a new file.

Leave a Reply