Linux command line create files

How to Make a File in Linux from the Command Line – Create a File in the Terminal

Zaira Hira

Zaira Hira

How to Make a File in Linux from the Command Line – Create a File in the Terminal

Managing files from the command line is one of the most common tasks for a Linux user.

Files are created, edited, deleted, and used by many of the background OS processes. Files are also used by regular users to accomplish daily tasks such as taking notes, writing code, or simply duplicating content.

In this article, we will see three methods through which we can create files using the terminal. The three commands that we’ll discuss are touch , cat and echo .

Pre-requisites:

You should have access to the Linux terminal to try out the commands mentioned in this tutorial. You can access the terminal in either of the following ways:

  • Install a Linux distro of your choice on your system.
  • Use WSL (Windows Subsystem for Linux), if you want to use Windows and Linux side by side. Here is a guide to do that.
  • Use Replit which is a browser-based IDE. You can create a Bash project and access the terminal right away.

Method #1: How to Create Files Using the touch Command

The touch command creates empty files. You can use it to create multiple files as well.

Syntax of the touch command:

Examples of the touch command:

Let’s create a single empty file using the syntax provided above.

Next, we’ll create multiple files by providing the file names separated with spaces after the touch command.

touch mod.log messages.log security.log

The above command will create three separate empty files named mod.log , messages.log , and security.log .

Method #2: How to Create Files Using the cat Command

The cat command is most commonly used to view the contents of a file. But you can also use it to create files.

Syntax of the cat command:

This will ask you to enter the text that you can save and exit by pressing ctrl+c .

When I enter the above command, my terminal output looks like this:

zaira@Zaira:~$ cat > sample.txt This is a sample file created using the "cat" command ^C

Note the ^C sign, which corresponds to Ctrl+c and signals to the terminal to save and exit.

Here are the contents of the created file:

zaira@Zaira:~$ more sample.txt This is a sample file created using the "cat" command

Method #3: How to Create Files Using the echo Command

The echo command is used to add and append text to files. It also creates the file if it doesn’t already exist.

Читайте также:  Игровая приставка от линукс

Syntax of the echo command:

echo "some text" > sample.txt
echo "some text" >> sample.txt

The difference between > and >> is that > overwrites the file if it exists whereas >> appends to the existing file.

If you would like to follow along with the video tutorial of this article, here is the link:

Conclusion

In this article, we discussed three different methods to create files in the Linux command line. I hope you found this tutorial helpful.

What’s your favorite thing you learned from this tutorial? Let me know on Twitter!

You can read my other posts here.

Источник

How to Create a File in Linux

Knowing how to create a new file is an important skill for anyone using Linux on a regular basis. You can create a new file either from the command line or from the desktop file manager.

In this tutorial, we’ll show you various ways to quickly create a new file in Linux using the command line.

Before you Begin #

To create a new file you need to have write permissions on the parent directory. Otherwise, you will receive a permission denied error.

If you want to display the contents of a directory use the ls command .

Creating a File with touch Command #

The touch command allows us to update the timestamps on existing files and directories as well as creating new, empty files.

The easiest and most memorable way to create new, empty files is by using the touch command.

To create a new file simply run the touch command followed by the name of file you want to create:

If the file file1.txt doesn’t exist the command above will create it, otherwise, it will change its timestamps.

To create multiple files at once, specify the file names separated by space:

touch file1.txt file2.txt file3.txt

Creating a File with the Redirection Operator #

Redirection allows you to capture the output from a command and send it as input to another command or file. There are two ways to redirect output to a file. The > operator will overwrite an existing file, while the >> operator will append the output to the file.

To create an empty zero-length file simply specify the name of the file you want to create after the redirection operator:

This is the shortest command to create a new file in Linux.

When creating a file using a redirection, be careful not to overwrite an important existing file.

Creating a File with cat Command #

The cat command is mainly used to read and concatenate files, but it can also be used for creating new files.

To create a new file run the cat command followed by the redirection operator > and the name of the file you want to create. Press Enter type the text and once you are done press the CRTL+D to save the files.

Creating a File with echo Command #

The echo command prints the strings that are passed as arguments to the standard output, which can be redirected to a file.

To create a new file run the echo command followed by the text you want to print and use the redirection operator > to write the output to the file you want to create.

Читайте также:  Zabbix agent linux настройка

If you want to create an empty simply use:

Creating a File using Heredoc #

Here document or Heredoc is a type of redirection that allows you to pass multiple lines of input to a command.

This method is mostly used when you want to create a file containing multiple lines of text from a shell script.

For example, to create a new file file1.txt you would use the following code:

cat file1.txtSome lineSome other lineEOF

The body of the heredoc can contain variables, special characters, and commands.

Creating a Large File #

Sometimes, for testing purposes, you might want to create a large data file. This is useful when you want to test the write speed of your drive or to test the download speed of your connection.

Using dd command #

The dd command is primarily used to convert and copy files.

To create a file named 1G.test with a size of 1GB you would run:

dd if=/dev/zero of=1G.test bs=1 count=0 seek=1G

Using fallocate command #

fallocate a command-line utility for allocating real disk space for files.

The following command will create a new file named 1G.test with a size of 1GB:

Conclusion #

In this tutorial, you learned how to create a new file in Linux from the command line using various commands and redirection.

If the command line is not your thing you can easily create a blank text file using the right-click menu in the File Manager.

If you have questions, feel free to leave a comment below.

Источник

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.

Читайте также:  Linux in action david clinton

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