Paul Sutton

FileCreation

Bash scripting 2 – files

In between getting my head around rust, I am doing more with bash.

The following script is based on the iso download script, but creates 3 empty text files

#!/bin/bash
echo Create files listed in an array with the touch command
files=(
  # list of files to create
  "file1.txt"
  "file2.txt"
  "file3.txt"
)

# Loop to create each file
for filelist in "${files[@]}"; do
  touch "$filelist"
done

#list files
ls -l

Which is good for creating lots of files of the same type. Now letgs say we would like to add some text to each of the files

we can amend the script as follows

# Loop to append each file
for filelist in "${files[@]}"; do
  touch "$filelist" # create the files
  lorem > "$filelist" # add lorem text to each file
  #lorem >> "$filelist" # append lorem text to each file 
done

#list files
ls -l

You will need access to the lorem command, which can be installed on a Debian based system using

sudo apt install libtext-lorem-perl

Please see the stackoverflow post

Also see the man page for lorem

man lorem

You will, of course need to set the execute permissions

chmod +x script.sh

Tags

#Bash,#Bashscripting,#FileCreation


MastodonPeertubeJoin Mastodon

AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay