Open txt file linux

Open and write data to text file using Bash?

How can I write data to a text file automatically by shell scripting in Linux? I was able to open the file. However, I don’t know how to write data to it.

12 Answers 12

echo "some data for the file" >> fileName 

However, echo doesn’t deal with end of line characters (EOFs) in an ideal way. So, if you’re going to append more than one line, do it with printf :

printf "some data for the file\nAnd a new line" >> fileName 

The >> and > operators are very useful for redirecting output of commands, they work with multiple other bash commands.

the operator > redirects output to a file overwriting the file if one exists. The >> will append to an existing file.

If you need to do this with root privileges, do it this way: sudo sh -c ‘echo «some data for the file» >> fileName’

#!/bin/sh FILE="/path/to/file" /bin/cat $FILE text1 text2 # This comment will be inside of the file. The keyword EOM can be any text, but it must start the line and be alone. EOM # This will be also inside of the file, see the space in front of EOM. EOM # No comments and spaces around here, or it will not work. text4 EOM 

Newbies would benefit from knowing exactly what to do with this file. How to save it, how to run it, etc.

Please explain rather than simply putting up a piece of code. It would make it so much more helpful to everyone — especially newbies.

You can redirect the output of a command to a file:

If you want to write directly the command is echo ‘text’

If you need root permission: sudo sh -c ‘cat file > /etc/init/gunicorn.conf’ (thanks to lukaserat’s comment above)

An explanation of your code would go a long way toward making it more usable to others. Even callouts of vocabulary like «here document» and «redirection» can point searchers to other resources.

Explanation is never necessary when you already know the answer. To me the above code is not helpful due to lack of details.

I know this is a damn old question, but as the OP is about scripting, and for the fact that google brought me here, opening file descriptors for reading and writing at the same time should also be mentioned.

#!/bin/bash # Open file descriptor (fd) 3 for read/write on a text file. exec 3<> poem.txt # Let's print some text to fd 3 echo "Roses are red" >&3 echo "Violets are blue" >&3 echo "Poems are cute" >&3 echo "And so are you" >&3 # Close fd 3 exec 3>&- 

Then cat the file on terminal

$ cat poem.txt Roses are red Violets are blue Poems are cute And so are you 

This example causes file poem.txt to be open for reading and writing on file descriptor 3. It also shows that *nix boxes know more fd’s then just stdin, stdout and stderr (fd 0,1,2). It actually holds a lot. Usually the max number of file descriptors the kernel can allocate can be found in /proc/sys/file-max or /proc/sys/fs/file-max but using any fd above 9 is dangerous as it could conflict with fd’s used by the shell internally. So don’t bother and only use fd’s 0-9. If you need more the 9 file descriptors in a bash script you should use a different language anyways 🙂

Читайте также:  Linux etc passwd permission denied

Anyhow, fd’s can be used in a lot of interesting ways.

Источник

How to open the file in bash

The file is used to store the data permanently and use the data in any script when requires. A file can be opened for reading, writing, or appending. Many bash commands exist to open a file for reading or writing, such as `cat`, `less`, `more` etc. Any text editor can be used to open a file in bash. nano, vim, vi, etc., an editor is used to open a file from the terminal. Many GUI editors also exist in Linux to open a file, such as Gedit, Geany, etc. The file can be opened for reading or writing by using bash script also. The ways to open a file for various purposes have been shown in this tutorial.

Open file using Bash commands:

The uses of shell commands to open a file for creating or reading are shown in this tutorial. The uses of `cat`, `less`, and `more` commands have shown here.

Use of `cat` command:

The `cat` is a very useful command of bash to create or display the file’s content. Any file type can be created easily and quickly by opening the file using the `cat` command with the ‘>’ symbol. Run the following `cat` command to open a file named file1.txt for writing. If the filename already exists, then the previous content of the file will be overwritten by the new content; otherwise, a new file will be created.

Add the following content to the file.

A bash script is a command-line interpreted language.
Many automated tasks can be done easily using a bash script.

Читайте также:  Linux mount device to directory

Press Ctrl+D to finish the writing task. The following output will appear after creating the file.

Now, run the following `cat` command to open the file.txt file for reading.

The following output will appear after executing the above command.

Use of `less` command:

The `less` command is used to open a file for reading only. It is mainly used to read the content of the large file. The user can move backward or forward through the file by using this command. It works faster than other text editors.

Run the following command to open the file1.txt file for reading. Here, the content of the file is very small. So when the user presses the enter key, then the content will go upward. Press the character ‘q’ to return to the command prompt.

The following output will appear after opening the file using the `less` command and pressing the enter key.

Use of `more` command:

Like the `less` command, the `more` command is used to open a large file for reading only. This command is mainly used to read a file’s large content in multiple pages to help the readers read long files.

Run the following command to open the file1.txt file for reading by using the `more` command. It is a small file. So all content of the file has displayed on one page.

The following output will appear after opening the file using the `more` command.

Open file using command-line editors:

The uses of vi and nano command-line editors for opening the file to create and read have been shown in this part of this tutorial.

Use of vi editors:

One of the popular text editors of Linux is vi editors. It is installed on Ubuntu by default. The user can easily create, edit, and view any file by using this text editor. The advanced version of vi editors is called vim editor, which is not installed by default. This part of the tutorial shows how to use vi editor to open a file for creating and reading. Run the following command to open the file2.txt file for writing.

You have to press the character ‘i’ to start writing into the vi editor. Add the following content to the file.

Writing a file using vi editors.

You can do any of the following tasks after writing the content of the file.

  1. Type :wq to quit the editor after saving the file.
  2. Type :w to keep the file open in the editor after saving.
  3. Type :q to quit the editor without saving the file.
Читайте также:  Linux lost connection to server

The following output shows that ‘:wq’ has been typed to quit the editor after saving the file.

Run the following command to open the file2.txt file and check the content exists or not that was added in the file.

The following output shows that the file contains the data that was added before. Here,’:’ has typed to quit the editor.

Use of nano editor:

Another useful and popular editor of Linux is the nano editor that is used to open a file for writing and reading. It is easier to use than the vi editor and more user-friendly than other command-line editors. Run the following command to open the file3.txt file for writing using nano editor.

Add the following content to the file.

Writing a file using nano editor.

If you type Ctrl+X after adding the content to the file, it will ask you to save the file. The following output will appear if you press the character, ‘y’. Now, press the enter to quit the editor after saving the file.

Open file using GUI text editor:

The ways to use gedit and geany GUI-based text editor have shown in the part of this tutorial.

Use of gedit editor:

The gedit is mostly used GUI-based text editor that is installed by default on maximum Linux distributions. Multiple files can be opened by using this editor. Run the following command the open the existing file1.txt file using gedit editor.

The following output will appear after executing the command.

Use of geany editor:

Geany is a more powerful GUI-based editor than the gedit editor, and you have to install it to use it. It can be used to write code for many types of programming languages. Run the following command to install the geany editor.

After installing the editor, run the following command to open the file1.txt file.

The following output will appear after executing the command.

Conclusion:

Many ways to open a file for reading or writing have been shown in this tutorial by using bash command, command-line editors, and GUI-based editors. The Linux users can select any of the ways mention here to open a file in bash.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

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