Linux create file with cat

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.

Читайте также:  Поднять linux dhcp сервер

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.

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.

Источник

The Cat Command in Linux – How to Create a Text File with Cat or Touch

The Cat Command in Linux – How to Create a Text File with Cat or Touch

The cat command is a very popular and versatile command in the ‘nix ecosystem. There are 4 common usages of the cat command. It can display a file, concatenate (combine) multiple files, echo text, and it can be used to create a new file.

Displaying a file

The most common use of the cat command is to output the contents of a file. The following is an example that you can try.

echo "Dance, Dance" > cat_create #create a file cat cat_create

In this simple example, we’re using a combination of echo and a redirect to create a file containing «Dance, Dance». We then use the cat command to display the contents.

image-11

(Con)cat

The previous example is actually a specific case of the cat command’s main function, which is to concatenate files for display. If we use the command the same way, but give it two or more files, then it outputs the concatenation for the files.

If we run the following commands:

echo "This is how we do it" > test1 #create 1st file echo "*This is how we do it*" > test2 #create 2nd file cat test1 test2 

The output is the contents of the 1st file, followed by the contents of the 2nd file. You can give cat many files and it will concatenate (combine) all of them. Notice however, that the cat command automatically inserts a line break between outputs.

Читайте также:  Как залогиниться в linux

image-12

cat also provides some switches to to do things such as show non-print characters (-v), or number your lines (-n). A complete breakdown can be found in the man pages.

Echoing

This is a less common usage of cat , but is the basis for the next section. If you run the cat command with no commands, cat will run in interactive mode and echo anything you type until you exit the command.

image-13

In the example here, I’ve typed a single word per line. Each time I hit enter, the line was echoed.

You can also pipe text to cat , in which case that text is echoed. For example:

This will result in the following output:

image-14

Creating a File

In the previous examples, we’ve been using the echo command redirected to a file to create new files. Cat can be used in a similar way. In fact, we can use cat ‘s concat and echo functionality to create files.

We can create a file containing the concatenation of multiple files like this:

echo "File 1 Contents" > file1 echo "File 2 Contents" > file2 echo "File 3 Contents" > file3 cat file1 file2 file3 > combined_file cat combined_file

In the above example, we’re creating 3 files using echo , combining the 3 files into one using cat , and then displaying the new combined file using cat .

image-15

We can also use cat ‘s interactive mode to create a file with the text that we type into the terminal.

image-16

Each time you hit enter, it commits the text to the file. If you have uncommitted text and exit, it won’t be captured in the file.

This is a fantastic way to create a file quickly with the ability to enter the content of the file.

Using Touch to create a file instead

Sometimes you just need a file to exist. As an alternative to using cat to create a file, you can use the touch command.

The touch command was designed to update the modified timestamp of a file, but is commonly used as a quick way to create an empty file. Here is an example of that usage:

The touch command can create multiple files, update the modification and/or creation timestamps, and a bunch of other useful things. The full man pages can be found here.

Touch is commonly used to ensure that a file exists, and is a great command if you need an empty file quickly.

Summary

Cat is a very useful command. You can use it to create, display, and combine text files very quickly and easily.

If you only need a file to exist, but don’t mind (or require) it being empty, using touch is a great alternative.

Hughie Coles is a lead developer at Index Exchange. He writes about software architecture, scaling, leadership, and culture. For more of his writing, check out his blog on medium.

Читайте также:  Команда linux изменить права файлу

Источник

How to Create a File in Linux Using cat Command

The cat command is one of the most popular and frequently used commands in Linux-like operating systems. This command allows Linux users to create single or multiple files, display the content of the file and concatenate the files.

The cat command in Linux is mostly used for displaying the content of the files and appending the content of one file to the other file. In this tutorial, we will cover the procedure of creating files in Linux using the cat command.

How To Create a Text File in Linux Using cat Command

You can create a file and add the text into the file through the cat command. Use the following syntax for creating the text file in Linux:

Let’s create a text file named a newfile.txt using the following command:

Once you executed the cat command the cursor will move to the next line where you can begin adding the text in the file:

Once you are done with writing the text in the file, press Ctrl + D to save the file in the device. Verify the created file by using the following command, it will read the content of the file and display it on the terminal:

How To Create a Bash File in Linux Using cat Command

The cat command can also be used for creating the bash file in Linux. Use the following command syntax on the terminal:

I have created a file2.sh via the cat command using the above given syntax:

Next, type the script you want to add in the bash file and display the file via the below given command:

If you want to run the script, use the following command:

How to Redirect the Content of File Using cat Command

The cat command can also be used for redirecting the content from the source file to the destination file. The following command will copy the content of newfile.txt to the file2.txt. If the file2 does not exist this command will create it:

How to Display Line Number with cat Command

Through the cat command you can also display the line number at the start of each line. Use the following command:

Conclusion

The cat is one of the most useful and powerful commands of Linux. It is used in Linux for creating, and displaying content of the files. Other than that, cat command can also be used for appending the content of one file to another. In this tutorial, we have discussed the creation of bash and text files in Linux using the cat command and we have also discussed the appending of content from the source to the destination file.

About the author

Zainab Rehman

I’m an author by profession. My interest in the internet world motivates me to write for Linux Hint and I’m here to share my knowledge with others.

Источник

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