0

I've written a shell script to create a number of files. The user specifies the number of files and their file extension from the command line. Here is my code

#!/bin/bash

NUM_FILES=0
FILE_TYPES=""
LOCATION=`pwd`

if [ $# -eq 0 ]; then
    echo "**** ERROR: You must specify the number of files you wish to create and (optional) the file types.\n\n"
    exit 1
fi

if [ ! -z $1 ]; then
    NUM_FILES=$1
fi

if [ ! -z $2 ]; then
    FILE_TYPES=$2
else
    FILE_TYPES=".tmp"
fi

n=$NUM_FILES

for (( i=1; i <= n; i++ )) 
do
    touch TEMP_FILE_$i.$FILE_TYPES    #Files created here - this is where my script needs work
    echo "Created file " TEMP_FILE_$i.$FILE_TYPES
done

My question relates to creating files of a specified type. The second argument passed to the command line ($FILE_TYPES) is used to specify the extension that you want to use (txt, java, py, etc), but when I run the script Linux interprets all the files as type text and just adds the extension to the end of the file name.

eg, running bash createFiles.sh 4 java will result in four text files being created named TEMP_FILE_1.java but viewing the directory in Linux GUI shows the files of being type text.

How can I modify the shell script so that it will create files of the type specified?

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Space Cadet
  • 385
  • 6
  • 23
  • I have just tested your script and it does exactly what you want. Can you try "ls -lh" in your console and show the output? – Matias Barrios Sep 28 '17 at 15:09
  • `if [ ! -z $1 ]; ...` is really asking for trouble. Just write `NUM_FILES=${1:-0}; FILE_TYPES=${2:-.tmp}` – William Pursell Sep 28 '17 at 15:57
  • Hi William - I'm relatively new to shell scripting so can I ask why that statement is asking for trouble? It's intended to check to ensure that the first argument passed to the script is not null – Space Cadet Sep 29 '17 at 13:29
  • 1
    Please read [How does accepting an answer work?](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work). Don't write the word "solved" in your question title. – Quentin Oct 02 '17 at 09:20

2 Answers2

1

There is no "type" of regular file in Linux/Unix. You could register any of your own file extension to open files with specified extension with application you will chose.

Please look at answers for question: Register file extensions / mime types in Linux

Also to understand other association mechanism on Linux named "Shebangs (#!)", you could read this: File extensions and association with programs in linux

MrCryo
  • 641
  • 7
  • 16
1

Many tools look at actual file contents to determine file type (a'la file utility). When you create empty files, theres no content that could be inspected to infer file type. So if you want to create files that would be recognized as particular type, probably you have to provide templates for every file type you support, and copy template contents to created files. For example, write some minimal Java program when creating .java files, write 1x1 pixel images when creating .png files etc.