- Create text file and fill it using bash
- 9 Answers 9
- 3 Ways to Create a Text File Quickly Through the Linux Terminal
- The cat Command
- The touch command
- Create multiple files at once through the touch command
- Using the Standard Redirect Symbol
- Search
- About This Site
- Latest Tutorials
- 4 Ways to Create a Text File in Linux Terminal
- Create file in Linux command line
- 1. Create an empty file using touch command
- 2. Create files using cat command
- 3. Create new file using echo command
- 4. Create a new file using a text editor like Nano or Vim
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
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.
3 Ways to Create a Text File Quickly Through the Linux Terminal
Being a Terminal-savvy person, you may always be looking for ways to ditch the mouse. Creating a text file is one task for which you can depend only on your keyboard on an Ubuntu system. Three commands from the Linux command line are at your service for creating text files. These include:
Let us explore these commands in this article to create some sample text files. The commands and procedures mentioned in this article have been run on an Ubuntu 20.04 LTS system. Since we will be creating the text files using Ubuntu command line-the Terminal; you can open it either through the system Dash or the Ctrl+Alt+T shortcut.
The cat Command
The cat command is very helpful when dealing with text files in Linux. It helps you in achieving three basic purposes:
- Creating a text file
- Printing contents of a text file in your Terminal
- Printing contents of a text file to another text file
Here, we will explore the first use of the cat command; creating a text file through the command line.
Enter the following command in your Terminal:
After entering this command, the next prompt will not appear; rather the cursor will display for you to enter the text for the file you just created.
In this example, I have created a text file through the following command and then entered some sample text:
Once you have entered all the text, hit enter to move to the next line and then use the Ctrl+D control to tell the system that you are done with entering the text. The usual command prompt will then appear for you to move on with further operations.
You can then use the ls command to see that your newly created text file will be there in the system.
Through the cat command, you can then view the contents of the file as follows:
You can see that the cat command shows the text I wrote while creating my sample file:
The touch command
Another way of quickly creating a text file through the Terminal is by using the touch command. The touch command, however, does not let you enter text in the file at the time of creation. After creating the file, you can enter the text through your favorite text editor. You might prefer the touch command over the cat command in one scenario; when you want to create multiple files at once through one command.
Let us first see how to create a single file first through the Linux touch command:
Use the ls command to see if the recently created file now exists on your system.
Create multiple files at once through the touch command
As mentioned above, the touch command takes the lead on the cat command on the basis that you can create multiple files simultaneously through the former. Use the following syntax to do so:
$ touch filename1.txt filename2.txt filename2.txt ….
For example, in the following command, I have created three files at once through the touch command:
$ touch sampletouchfile1.txt sampletouchfile2.txt sampletouchfile2.txt
I also checked the presence of the three files through the ls command in the above example.
If you want to edit any of the files you created through the touch command, you can use any of your favorite text editors. Here I am using the Nano editor to enter text to one of the files I created. I used the following command to open the file through the Nano editor.
I then entered the text and saved it by pressing Ctrl+X and then by hitting Enter.
The touch command can also be used to change the access and modification time of a file.
Change the access time of a file:
Set the modification time of a file:
You can view the access and modification time of files with the stat command:
Using the Standard Redirect Symbol
The standard redirect symbol is usually used when redirecting the output of a command to a file. However, it can also be used to create a single text file. The only difference is that while creating a new file we do not specify any command before the redirect symbol.
The difference between using the standard redirect symbol for creating a text file is that, unlike the cat command, you can not enter text this way. Also, unlike the touch command, you can only create one file at a time through the redirect symbol.
Use the following syntax in order to create a text file through this symbol:
When you save and exit the file, your text file will have those contents saved.
Through this article, we have learned three basic ways to create text files quickly through the Linux command line. You can now avoid the mouse and use only the keyboard in order to perform the simple task of creating a text file in Ubuntu.
Search
About This Site
Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.
Latest Tutorials
4 Ways to Create a Text File in Linux Terminal
In this Linux beginner series, you’ll learn various methods to create a file in Linux terminal.
In this Linux beginner series, you’ll learn various methods to create a text file in Linux terminal.
If you have used the desktop oriented operating system such as Windows, creating file is a piece of cake. You right click in the file explorer and you would find the option of creating new file.
Things won’t look the same when you are in a command line environment. There is no right click option here. So how do you create a file in Linux then? Let me show you that.
Create file in Linux command line
There are various ways of creating a new file in Linux terminal. I’ll show you the commands one by one. I am using Ubuntu here but creating files in Ubuntu terminal is the same as any other Linux distribution.
1. Create an empty file using touch command
One of the biggest usages of the touch command in Linux is to create a new empty file. The syntax is super simple.
If the file doesn’t exist already, it will create a new empty file. If a file with the same name exists already, it will update the timestamps of the file.
2. Create files using cat command
Another popular way of creating new file is by using the cat command in Linux. The cat command is mostly used for viewing the content of a file but you can use it to create new file as well.
You can write some new text at this time if you want but that’s not necessary. To save and exit, use Ctrl+D terminal shortcut.
If the file with that name already exists and you write new text in it using the cat command, the new lines will be appended at the end of the file.
3. Create new file using echo command
The main use of the echo command is to simply repeat (echo) what you type on the screen. But if you use the redirection with echo, you can create a new file.
To create a new file using echo you can use something like this:
echo "This is a sample text" > filename.txt
The newly created filename.txt file will have the following text: This is a sample text. You can view the file in Linux using cat or other viewing commands.
You are not obliged to put a sample text with echo. You can create an (almost) empty file using the echo command like this:
This will create a new file with just one empty line. You can check the number of lines with wc command.
4. Create a new file using a text editor like Nano or Vim
The last method in this series is the use of a text editor. A terminal-based text editor such as Emacs, Vim or Nano can surely be used for creating a new file in Linux.
Before you use these text editors, you should make sure that you know the basics such as saving an existing from the editor. Unlike the GUI tools, using Ctrl+S in the terminal won’t save the file. It could, in fact, send your terminal into a seemingly frozen state from which you recover using Ctrl+Q.
Let’s say you are going to use Vim editor. Make sure that you are aware of the basic vim commands, and then open a new file with it like this:
What’s your favorite command?
So, I just shared 4 different ways of creating a file in Linux. Personally, I prefer using touch for creating empty file and Vim if I have to edit the file. On a related note, you may want to learn about the file command in Linux that is helpful in determining the actual type of the file.
Which command do you prefer here? Please share your views in the comment section below.