Add new file in linux

How to Create a New File in Linux from Bash?

Before getting into the ways of creating a file using Bash, let’s first understand how Linux treats its files. Linux organizes all its data into files and files are organized into directories. Further, the directories are organized into tree-like structures called the filesystem. When you have to work in a Linux environment, you would definitely have to spend a lot of your time working on different types of files.

There are various ways in which one can create a file in Linux. You can create a file from the Bash Shell or you can use the Desktop File Manager to do so. In this article, we will focus on different Shell commands that you can use to create a file.

Using the «touch» command

The touch command is by far the most frequently used command for creating a new file in Linux. To create a new file, you need to run the touch command followed by the name of the file. For example,

It will create an empty file called «hello.txt» in the current directory. Use the «ls» command to verify if the file has been created or not.

Using the «cat» command

Normally we use the «cat» command to read the contents of a file; however, we can also use this command to create a new file. Let’s see how.

To create a new file, run the «cat» command and then use the redirection operator «>» followed by the name of the file. Now you will be prompted to insert data into this newly created file. Type a line and then press «Ctrl+D» to save the file.

$ cat > secondFile.txt Welcome to Tutorialspoint!

The above command will create a new file called «secondFile.txt» and save it with the content «Welcome to Tutorialspoint».

Again, use the «ls» command to verify if the new file has been created or not.

$ ls hello.txt newdirectory secondFile.txt

Next, use the «cat» command to see the contents of «secondFile.txt».

$ cat secondFile.txt Welcome to Tutorialspoint!

Using the Redirection Operator

You can simply use the redirection operator «>» to create a new blank file in the current directory. Run the «>» operator followed by the name of the file.

Now use the «ls» command again to verify −

$ ls hello.txt newdirectory secondFile.txt thirdFile.txt

Note that the «>» operator overwrites the contents of a file if it is already present. For example, the following command will overwrite the contents of «secondFile.txt» because the file already exists and we know it contains the line «Welcome to Tutorialspoint!»

Now use the «cat» command to check the contents of «secondFile.txt».

It will display nothing because the file is now empty.

You can use the redirection operator «>>» to append the contents of a file into another. For example,

$ cat hello.txt This is the first file. $ cat secondFile.txt This is the Second File.

Now we can use the following command to append the contents of «secondFile.txt» at the end of «hello.txt».

$ cat secondFile.txt >> hello.txt $ cat hello.txt This is the first file. This is the Second File.

Using the «echo» command

The «echo» command takes a string as argument and displays it as output. For example,

$ echo "This is the Fourth File" This is the Fourth File

We can redirect this output to a new file, such as −

$ echo "This is the Fourth File" > fourthFile.txt

The above command will create a new file (or overwrite the file if it already exists) with the string passed as the argument to «echo». Verify using the «cat» command −

$ cat fourthFile.txt This is the Fourth File

If you simply want to create a blank new file, use the «echo» command without any argument −

Читайте также:  Технология построения виртуальных защищенных сетей vipnet windows linux практикум

Using the «printf» command

The «printf» command works just like the «echo» command with the only exception that the «printf» command provides additional formatting options that you can use to pass a formatted string as the argument.

The following «printf» command redirects the input formatted string into a new file «fifthFile.txt». If the file already exists, then it will overwrite its contents.

$ printf "First Line.
Second Line.
" > fifthFile.txt $ cat fifthFile.txt First Line. Second Line.

Conclusion

In this brief tutorial, we highlighted five different ways that you can use to create a new file in Linux from the terminal. In case you don’t have Linux on your system, then use Windows Subsystem for Linux (WSL) to create a Linux environment on your Windows system.

Источник

4 Ways to Create New File in Linux

Linux based operating systems are known for their users’ heavy use of command line for performing not only complicated automation but also the most trivial of tasks. However, with the steady growth of Linux distributions in the home desktop market, the onus is on the developers to make the graphical interface as lay user friendly as possible.

Today, we will see various ways to perform a simple and trivial task; creating a new file, in Linux using the command line as well as the GUI.

Create File using Touch Command

The touch command in Linux is used to create an empty file and the syntax for creating an empty file is as easy as:

To create multiple files at once, pass multiple file names as arguments:

$ touch sample_file1 sample_file2 sample_file3

Create New File in Linux

The touch command will throw an error if any of the files already exist.

You can also create the file in another directory by specifying the full path to the directory location.

$ touch ~/Downloads/sample_file1 sample_file5 ~/Documents/sample_file6

Create File in Different Directory

Create File Using Vim and Nano Editors

Vim is a very popular text editor in Linux distributions. Although the command line utility nano is the one available by default, users generally prefer Vim.

Install Vim in Debian and its derived distributions by running:

In RedHat and its derived distributions, install Vim with Yum:

Creating a new file is quite easy using text editors. You can create an empty file by creating a new file for writing and save it without writing anything to the file.

The following syntaxes can be used to create a new file using Vim and Nano respectively:

$ vim new_filename $ nano new_filename

Once the editors open with ‘new_filename‘, users can now write to the file if needed. If writing to file is done, or if the file is to be left empty, do the following to save the file:

Читайте также:  Check file type linux

Create New File Using Editors

  • In Vim, press the ‘Escape‘ key, type ‘:wq’, and hit Enter to save the file and exit.
  • In Nano, simply press Ctrl + O and hit Enter to save the file. Press Ctrl + X to exit.

Create File Using Redirection Operator

A redirection operator is an operator used in the Linux command line to write output to a file or to read input from a file.

We use the former of these two operators (‘>’) to create a new file with output from a command. For example, to write the output of command ls to a new file, run:

$ ls > sample_newfile $ cat sample_newfile

Write Command Output to File

To create an empty file using this, simple echo command and empty string and redirect it.

Create File Using Redirect Operator

Create File Using File Manager

Lastly, we will see how to create a new file from the GUI. Open Nautilus either by running the command ‘nautilus’, from the left-hand side dock or from the Applications menu, depending on your Linux distribution.

Nautilus File Manager

Go to the folder you want to create the new file in. For example, let’s create a new file in the folder ‘Documents’.

Documents Directory

Right-click on the empty space in the folder display and click on ‘New Document’ -> ‘Empty Document’.

Create New Empty Document

Note: In newer versions of Ubuntu, the option ‘New Document’ might be missing. To fix this simply run the following:

$ touch ~/Templates/Empty\ Document

Right-click on the new file, click ‘Rename’, and enter a new name for the file.

Rename New Empty Document New Empty Document

Conclusion

We learned about multiple ways to create a new file in Linux. There are obviously, even more ways to create a new file, and every individual application usually has the ability to create a new file of its respective format. Eg. Image editors export files to image formats like JPEG and PNG, audio editors to MP3, M4A, etc.

Let us know in the comments below how you go about creating a new file in your Linux system!

Источник

How to Create a File in Linux Using Terminal/Command Line

Creating a new file in Linux is straightforward, but there are also some surprising and clever techniques.

In this tutorial learn how to to create a file from a Linux terminal.

create a file from linux terminal

  • Access to a command line/terminal window (CtrlAltF2 or CtrlAltT)
  • A user account with sudo privileges (optional for some files/directories)

Creating New Linux Files from Command Line

Linux is designed to create any file you specify, even if it doesn’t already exist. One smart feature is that you can create a file directly, without needing to open an application first.

Here are a few commands for creating a file directly from the command line.

Create a File with Touch Command

The easiest way to create a new file in Linux is by using the touch command.

In a terminal window, enter the following:

This creates a new empty file named test.txt. You can see it by entering:

The ls command lists the contents of the current directory. Since no other directory was specified, the touch command created the file in the current directory.

create a file with touch command

If there’s already a file with the name you chose, the touch command will update the timestamp.

Create a New File With the Redirect Operator

A redirection operator is a name for a character that changes the destination where the results are displayed.

Right angle bracket >

This symbol tells the system to output results into whatever you specify next. The target is usually a filename. You can use this symbol by itself to create a new file:

Читайте также:  Менеджер сети для линукс

This creates a new empty file.
Use the ls command to list the contents of the current directory and find the file test2.txt.

create a file with redirection operator

Create File with cat Command

The cat command is short for concatenate. It can be used to output the contents of several files, one file, or even part of a file. If the file doesn’t exist, the Linux cat command will create it.

To create an empty file using cat , enter the following:

Note the redirection operator. Typically, the command displays the contents of test2.txt on the screen. The redirection operator > tells the system to place it in the test2.txt file.

Verify that the file was created:

The system should now have test.txt, test2.txt, and test3.txt in the list.

create a file with cat command

Create File with echo Command

The echo command will duplicate whatever you specify in the command, and put the copy into a file.

echo 'Random sample text' > test4.txt

Verify that the file was created:

create a file with echo command

You should see the test4.txt file added to the list. Use the cat command to display the contents of the new file:

The system should display Random sample text (or whatever you entered with the echo command.)

echo command output

Create File with printf Command

The printf command works like the echo command, and it adds some formatting functionality. To add a single line of text, enter:

printf 'First line of text\n' test5.txt

To add two lines of text, separate each line with the \n option:

printf 'First line of text\n Second line of text' test6.txt

You can use the cat command on either of these files to display their contents.

Note: To use several terminal instances in a single window manager, consider using Linux screen. It enables additional features and an enhanced command line for working with Linux files.

Using Text Editors to Create a Linux File

All Linux distributions have at least one text editor. Some have multiple editors. Each editor has different strengths and features. This will show you three of the most popular.

Vi Text Editor

Vi is the oldest text editor in Linux. It was created alongside the Linux operating system for directly editing text files. Since it’s unlikely you’ll see a Linux distribution without it, it’s a safe editor to know.

To create a file using Vi, enter the following:

Your screen will change. Now you’re in the text editor. Press the letter i to switch to insert mode, then type a few words to try it out.

To save and exit press Esc 😡 and hit Enter .

vi text editor example

Vim Text Editor

You may have noticed that the Vi editor wasn’t very user-friendly. Vim is a newer version, which stands for Vi editor, Modified.

Use vim to create a new text file:

using vim to make a new file in Linux

This screen will look similar to the Vi editor screen. Press i to insert text, and type a few words. Save file and exit by entering:

(Escape, colon wq, then Enter.)

Nano Text Editor

Nano is a newer and much easier text editor to navigate.

Create a new file by entering the command:

By default, Nano puts you directly into editing mode. It also displays a helpful list of commands at the bottom of the screen.

nano text editor to create a new linux file

Enter some text, then press Ctrl+O to save the changes.

Press Ctrl+X to exit the editor.

Note: Learn all you need about Nano in the Install and Use Nano in Linux article.

Now you have several options to create new files in Linux from the command line. Next, learn how to copy files and directories in Linux to manage your files more efficiently.

Источник

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