Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of creating files in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you what methods to create files in Linux, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Create a file on Linux using the redirector (>)

The standard redirector allows us to create an empty file for 0KB. It is usually used to redirect the output of a command to a new file. When you use a redirect symbol without a command, it creates a file.

But it does not allow you to enter any text into the file when you create it. However, it is very simple and useful for administrators who are not very hardworking. Just enter the redirector followed by the file name you want.

$> daygeek.txt

Use the ls command to view the file you just created.

$ls-lh daygeek.txt-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:00 daygeek.txt uses the touch command on Linux to create a file

The touch command is often used to update the access and modification time of each file to the current time.

If the specified file name does not exist, a new file will be created. Touch does not allow us to enter some text into a file while creating it. It creates an empty file of 0KB by default.

$touch daygeek1.txt

Use the ls command to view the file you just created.

$ls-lh daygeek1.txt-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:02 daygeek1.txt uses the echo command on Linux to create a file

Echo is built into most operating systems. It is often used in scripts, batch files, and as part of a single command that inserts text.

It allows you to enter some text into a file when you create it. Of course, it also allows you to enter some text into it later.

$echo "2daygeek.com is a best Linux blog to learn Linux" > daygeek2.txt

Use the ls command to view the file you just created.

$ls-lh daygeek2.txt-rw-rw-r-- 1 daygeek daygeek 49 Feb 4 02:04 daygeek2.txt

You can use the cat command to view the contents of the file.

$cat daygeek2.txt2daygeek.com is a best Linux blog to learn Linux

You can use two redirects (> >) to add other content to the same file.

$echo "It's FIVE years old blog" > > daygeek2.txt

You can use the cat command to see what has been added.

$cat daygeek2.txt2daygeek.com is a best Linux blog to learn LinuxIt's FIVE years old blog uses the printf command on Linux to create a new file

The printf command can also be executed in a manner similar to echo.

The printf command is often used to display the string given in the terminal window. Printf can have format specifiers, escape sequences, or normal characters.

$printf "2daygeek.com is a best Linux blog to learn Linux\ n" > daygeek3.txt

Use the ls command to view the file you just created.

$ls-lh daygeek3.txt-rw-rw-r-- 1 daygeek daygeek 48 Feb 4 02:12 daygeek3.txt

Use the cat command to view the contents of the file.

$cat daygeek3.txt2daygeek.com is a best Linux blog to learn Linux

You can use two redirects (> >) to add other content to the same file.

$printf "It's FIVE years old blog\ n" > > daygeek3.txt

You can use the cat command to see what is added to this file.

$cat daygeek3.txt2daygeek.com is a best Linux blog to learn LinuxIt's FIVE years old blog creates a file in Linux using cat

Cat means concatenated concatenate. In Linux, it is often used to read data in a file.

Cat is one of the most commonly used commands in Unix-like systems. It provides three functions related to a text file: displaying the contents of one file, combining the contents of multiple files into one output, and creating a new file. LCTT note: if you do not bring any files with you after the cat command, the following command will not end immediately after entering the carriage return. You can press Ctrl-C or Ctrl-D to end the operation after entering the carriage return. )

$cat > daygeek4.txt2daygeek.com is a best Linux blog to learn LinuxIt's FIVE years old blog

Use the ls command to view the created file.

$ls-lh daygeek4.txt-rw-rw-r-- 1 daygeek daygeek 74 Feb 4 02:18 daygeek4.txt

Use the cat command to view the contents of the file.

$cat daygeek4.txt2daygeek.com is a best Linux blog to learn LinuxIt's FIVE years old blog

If you want to add something else to the same file, use the redirector of the two connections (> >).

$cat > > daygeek4.txtThis website is maintained by Magesh M, It's licensed under CC BY-NC 4.0.

You can use the cat command to see what has been added.

$cat daygeek4.txt2daygeek.com is a best Linux blog to learn LinuxIt's FIVE years old blogThis website is maintained by Magesh M, It's licensed under CC BY-NC 4.0. Use the vi/vim command on Linux to create a file

Vim is an up-compatible text editor for vi. It is usually used to edit all kinds of plain text. It is especially useful when editing programs.

There are many features in vim that can be used to edit a single file.

$vi daygeek5.txt 2daygeek.com is a best Linux blog to learn LinuxIt's FIVE years old blog

Use ls to view the file you just created.

$ls-lh daygeek5.txt-rw-rw-r-- 1 daygeek daygeek 75 Feb 4 02:23 daygeek5.txt

Use the cat command to view the contents of the file.

$cat daygeek5.txt2daygeek.com is a best Linux blog to learn LinuxIt's FIVE years old blog creates a file on Linux using the nano command

Nano is an editor, which is a free version of the pico clone. Nano is a small and user-friendly editor. It copies the appearance and advantages of pico, and is a free software that adds a series of features that pico lacks, such as opening multiple files, line-by-line scrolling, undo / redo, syntax highlighting, line numbers, and so on.

$nano daygeek6.txt 2daygeek.com is a best Linux blog to learn LinuxIt's FIVE years old blogThis website is maintained by Magesh M, It's licensed under CC BY-NC 4.0.

Use the ls command to view the created file.

$ls-lh daygeek6.txt-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:26 daygeek6.txt

Use the cat command to view the contents of a file.

$cat daygeek6.txt2daygeek.com is a best Linux blog to learn LinuxIt's FIVE years old blogThis website is maintained by Magesh M, It's licensed under CC BY-NC 4.0. Use the head command on Linux to create a file

The head command is usually used to output the beginning of a file. By default, it prints the first 10 lines of a file to standard output. If there are multiple files, each file will be preceded by a title to represent the file name.

$head-c 0K / dev/zero > daygeek7.txt

Use the ls command to view the created file.

$ls-lh daygeek7.txt-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:30 daygeek7.txt creates a file on Linux using tail

The tail command is usually used to output the end part of a file. By default, it prints the last 10 lines of each file to standard output. If there are multiple files, each file will be preceded by a title to represent the file name.

$tail-c 0K / dev/zero > daygeek8.txt

Use the ls command to view the created file.

$ls-lh daygeek8.txt-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:31 daygeek8.txt uses the truncate command on Linux to create a file

The truncate command is often used to reduce or expand the size of a file to a specified size.

$truncate-s 0K daygeek9.txt

Use the ls command to check the files you created.

$ls-lh daygeek9.txt-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:37 daygeek9.txt

I used these ten commands to create the following ten files.

Ls-lh daygeek*-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:02 daygeek1.txt-rw-rw-r-- 1 daygeek daygeek 74 Feb 4 02:07 daygeek2.txt-rw-rw-r-- 1 daygeek daygeek 74 Feb 4 02:15 daygeek3.txt-rw-rw-r-- 1 daygeek daygeek 148Feb 4 02:20 daygeek4.txt-rw-rw-r-- 1 daygeek daygeek 75 Feb 4 02:23 daygeek5.txt-rw-rw-r-- 1 daygeek daygeek 148Feb 4 02:26 daygeek6.txt-rw-rw-r-- 1 daygeek daygeek 148Feb 4 02:32 daygeek7.txt-rw-rw-r-- 1 daygeek daygeek 148Feb 4 02:32 daygeek8.txt-rw-rw-r-- 1 daygeek daygeek 148Feb 4 02:38 daygeek9.txt-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:00 daygeek.txt these are the ways to create files in Linux Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report