Linux create file with content

Creating files with some content with shell script

I need to configure a server with a few files and I want to do it programmatically. I need to create files say /home/a.config, /var/spool/b.config, /etc/c.config Files above have some contents (multi lines). I want to create ONE shell script which can create all three file with multiple lines (around 10). I would like to know the how can I use CAT command to do that. (inside shell script). I am looking something like this

echo " going to create /home/a.config" cat "HOW CAN I HAVE MULTIPLE LINES HERE?? " > /home/a.config 

5 Answers 5

cat filename first line second line third line EOF 

You can place several of these in the same script.

@Nagev: If you don’t want any parameter expansion, command substitution, and arithmetic expansion in the here document you can quote the beginning delimiter ‘EOF’ instead of having to escape each individual dollar sign.

@JúlioFalbo: If you don’t quote the here doc delimiter (e.g. EOF ) then parameters (variables) in the text will be expanded.

file="/tmp/test.txt" echo "Adding first line" > $file echo "Adding first line replaced" > $file echo "Appending second line " >> $file echo "Appending third line" >> $file cat $file 

> to add/replace the content ( here actual content got replaced by the 2nd line)
>> to append

Result
Adding first line replaced
Appending second line
Appending third line

#!/bin/bash var="your text" echo "simply put, just so: $var" > a.config 

For further info, see Input/Output part of abs.
Hope, this helps.

If you’ve got variables like $1 or $HOMEDIR in your text then these normally get evaluated and substituted with actual values. If you want to prevent these from getting substituted then you need to quote the opening limit string (EOF in example below) with single quote ‘EOF’ , double quote «EOF» or precede it with backslash \EOF

Closing limit string stays as is. EOF

This is especially useful if you are writing shell scripts to a file.

cat /etc/rc.d/init.d/startup case $1 in start) start ;; stop) stop ;; restart) stop start ;; status) pid=$(tomcat_pid) if [ -n "$pid" ] then echo "Tomcat is running with pid: $pid" else echo "Tomcat is not running" fi ;; esac EOF 

Refer Example 19.7 Parameter Substitution Turned off in Here Documents

Читайте также:  Linux sendmail send file

Источник

Create text file and fill it using bash

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?

9 Answers 9

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’.

If you want to append to a file use this

echo "append this text" >> myfile.txt 

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 

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:

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.

@Switz: See edit explaining $1. If you replace $1 in the script with «text.txt», it will always use «text.txt» as the filename.

Assuming you mean UNIX shell commands, just run

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

Читайте также:  Linux new line symbol

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.

Источник

Create file with content in one line of bash

I wish to create a single file with some contents known to me. How do I do this in couple lines of bash? this command will be used inside of a single script, so it should create file, add text, save, and quit automatically by itself without human intervention. I know that

cat >> some.text type some stuff ctrl + D 

I presume you are aware that cat >> some.txt will append to whatever is already in the file — try running it twice. If you want to overwrite, use a single > .

4 Answers 4

The delimiter (here END ) is an arbitrary word. Quoting this delimiter after the

You could also do the following: echo ‘some stuff’ > your/file.txt

For multiline, here’s another example: printf «some stuff\nmore stuff» >> your/file.txt

For making it multiline its also possilbe to echo in «execution mode»:

echo -e "line1\nline2" > /tmp/file 

so the \n will make a carriage return.

Great answer from @that-other-guy, also important to note that you can include the directory of the file in there and not to forget your bin/bash stuff at the start, and that it works for more than just text files. See below my example for yaml files. And remember to make your bash files executable after with: chmod u+x fileName.sh

#!/usr/bin/bash cat >> ~/dir/newDir/yamlFiles/testing.yaml  

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to create a file in Linux from terminal window? [closed]

  • touch /path/to/file for an empty file
  • somecommand > /path/to/file for a file containing the output of some command.
 eg: grep --help > randomtext.txt echo "This is some text" > randomtext.txt 

UNIX is not a command line environment, but a family of (very different) OSes. That said: Yes, this should work on most Unices

touch will work in UNIX, because it's a standard tool. The somecommand example will work because it uses standard syntax. The nano sample may not work because an editor named nano may not be installed (nano is not standardized). The standard editor is ed and could be used in place of nano , or you could use $EDITOR to use your user- or system-configured default text editor, if there is one.

Additionally, you could simply say >/path/to/file to create an empty file, even if you don't have touch .

Create the file using cat

Now, just type whatever you want in the file:

When I tried cat /etc/systemd/system/sample.service , it said "no such file or directory" rather than creating a new sample.service file.

@TylerH cat /etc/systemd/system/sample.service prints the file to the console cat > /etc/systemd/system/sample.service redirects standard input to the file (which is why you need to close standard input by pressing control-d.

There are several possible solutions:

Create an empty file

touch file >file echo -n > file printf '' > file 

The echo version will work only if your version of echo supports the -n switch to suppress newlines. This is a non-standard addition. The other examples will all work in a POSIX shell.

Create a file containing a newline and nothing else

echo '' > file printf '\n' > file 

This is a valid "text file" because it ends in a newline.

Write text into a file

"$EDITOR" file echo 'text' > file cat > file file 

These are equivalent. The $EDITOR command assumes that you have an interactive text editor defined in the EDITOR environment variable and that you interactively enter equivalent text. The cat version presumes a literal newline after the \ and after each other line. Other than that these will all work in a POSIX shell.

Of course there are many other methods of writing and creating files, too.

Источник

Оцените статью
Adblock
detector