120

I need to create a text file (unless it already exists) and write a new line to the file all using bash.

I'm sure it's simple, but could anyone explain this to me?

Machavity
  • 30,841
  • 27
  • 92
  • 100
switz
  • 24,384
  • 25
  • 76
  • 101

9 Answers9

150

Creating a text file in unix can be done through a text editor (vim, emacs, gedit, etc). But what you want might be something like this

echo "insert text here" > myfile.txt

That will put the text 'insert text here' into a file myfile.txt. To verify that this worked use the command 'cat'.

cat myfile.txt

If you want to append to a file use this

echo "append this text" >> myfile.txt
ostler.c
  • 3,282
  • 3
  • 21
  • 21
49

If you're wanting this as a script, the following Bash script should do what you want (plus tell you when the file already exists):

#!/bin/bash
if [ -e $1 ]; then
  echo "File $1 already exists!"
else
  echo >> $1
fi

If you don't want the "already exists" message, you can use:

#!/bin/bash
if [ ! -e $1 ]; then
  echo >> $1
fi

Edit about using:

Save whichever version with a name you like, let's say "create_file" (quotes mine, you don't want them in the file name). Then, to make the file executatble, at a command prompt do:

chmod u+x create_file

Put the file in a directory in your path, then use it with:

create_file NAME_OF_NEW_FILE

The $1 is a special shell variable which takes the first argument on the command line after the program name; i.e. $1 will pick up NAME_OF_NEW_FILE in the above usage example.

GreenMatt
  • 18,244
  • 7
  • 53
  • 79
22

Assuming you mean UNIX shell commands, just run

echo >> file.txt

echo prints a newline, and the >> tells the shell to append that newline to the file, creating if it doesn't already exist.

In order to properly answer the question, though, I'd need to know what you would want to happen if the file already does exist. If you wanted to replace its current contents with the newline, for example, you would use

echo > file.txt

EDIT: and in response to Justin's comment, if you want to add the newline only if the file didn't already exist, you can do

test -e file.txt || echo > file.txt

At least that works in Bash, I'm not sure if it also does in other shells.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • +1. I think there's 1 other possible interpretation: create and write a newline if the file doesn't yet exist, else don't modify the file at all. – Justin Ardini Jan 11 '11 at 21:46
  • @Justin: good point, I guess I can add that one too and we'll have all the options covered. – David Z Jan 11 '11 at 22:12
19
#!/bin/bash
file_location=/home/test/$1.json
if [ -e $policy ]; then
  echo "File $1.json already exists!"
else
  cat > $file_location <<EOF
{
      "contact": {
          "name": "xyz",
          "phonenumber":   "xxx-xxx-xxxx"
      }
    }
EOF
fi

This code checks if the given JSON file of the user is present in test home directory or not. If it's not present it will create it with the content. You can modify the file location and content according to your needs.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Moh
  • 231
  • 2
  • 2
3

Your question is a a bit vague. This is a shell command that does what I think you want to do:

echo >> name_of_file
zwol
  • 135,547
  • 38
  • 252
  • 361
3
echo -e "Hello there, new line!" >> new_file.txt

or

cat <<EOF | sudo tee /etc/test.txt
Hello World
EOF
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
Md Mehedi Hasan
  • 1,733
  • 1
  • 21
  • 34
  • 1
    But you show two different command with two differents *effects*: The first will *append* while second will *replace content*! Consider `-a` switch of `tee` and/or having only one `>` redirector instead of two!! – F. Hauri - Give Up GitHub Jan 29 '23 at 13:25
1
cat my_file.txt | cat > new_file.txt

Create a ".sh" file and drop this command there, then run))

This will put the output of my_file to new_file and by the way will create it.

Instead of cat my_file.txt you can put any text, it automatically will be send to new_file.

elcortegano
  • 2,444
  • 11
  • 40
  • 58
Veaceslav
  • 41
  • 5
0

"Touch" command also could be used for creating a file.
Example:

touch new_file.txt

This command will create new file new_file.txt in current folder.

0

If you want to use one-liners

To execute some command if logical comparison is true:

[ -e myfile.txt ] && echo "myfile.txt already exists!"

Logical operator "AND" (&&) always executes the first command and only if exit status of that command is Success (0) then the next command is executed.

Or reversely execute command of logical comparison is false

[ -e myfile.txt ] || echo "insert text here" > myfile.txt

Logical operator "OR" (||) always executes the first command and only if exit status command is NOT Success (0) then the next command is executed.

user3852017
  • 190
  • 1
  • 9