mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-13 22:30:37 +08:00
Merge pull request #28812 from lkxed/20230307-2-Terminal-Basics-Series-4-Creating-Files-in-Linux
[手动选题][tech]: 20230307.2 ⭐️ Terminal Basics Series 4 Creating Files in Linux.md
This commit is contained in:
commit
4a4fe03ff8
@ -0,0 +1,133 @@
|
||||
[#]: subject: "Terminal Basics Series #4: Creating Files in Linux"
|
||||
[#]: via: "https://itsfoss.com/create-files/"
|
||||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: " "
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Terminal Basics Series #4: Creating Files in Linux
|
||||
======
|
||||
|
||||
So far, in this Terminal Basics series, you have learned to:
|
||||
|
||||
- [Change directories][1]
|
||||
- [Make new directories][2]
|
||||
- [List directory contents][3]
|
||||
|
||||
Let's now learn about creating files in the Linux command line. I'll briefly discuss adding content to the file. However, details on editing text files will be covered later.
|
||||
|
||||
### Create a new empty file with touch command
|
||||
|
||||
Using the touch command is pretty straightforward.
|
||||
|
||||
```
|
||||
touch filename
|
||||
```
|
||||
|
||||
Switch to your home directory and create a new directory called `practice_files` and switch to this directory:
|
||||
|
||||
```
|
||||
mkdir practice_files && cd practice_files
|
||||
```
|
||||
|
||||
💡
|
||||
|
||||
The && is a way to combine two commands. The second command only runs when the first command is executed successfully.
|
||||
|
||||
Now, create a new file named new_file:
|
||||
|
||||
```
|
||||
touch new_file
|
||||
```
|
||||
|
||||
That's it. You have just created a new empty file.
|
||||
|
||||
List the directory content and check the properties of the file with ls -l command.
|
||||
|
||||
![Using touch command to create new files][4]
|
||||
|
||||
💡
|
||||
|
||||
The touch command's original purpose is to 'touch' a file and change its timestamp. If the provided file does not exist, it creates a new file with the name.
|
||||
|
||||
### Create a new file using the echo command
|
||||
|
||||
I should have introduced you to the echo command long back. Better late than never. The echo command displays whatever you provide to it. Hence the name echo.
|
||||
|
||||
```
|
||||
echo Hello World
|
||||
```
|
||||
|
||||
You can use redirection and route the output to a file. And hence creating a new file in the process:
|
||||
|
||||
```
|
||||
echo "Hello World" >> other_new_file
|
||||
```
|
||||
|
||||
This way, you create a new file named `other_new_file` with the text `Hello World` in it.
|
||||
|
||||
![Using echo command to create new file][5]
|
||||
|
||||
Remember, if the provided file already exists, with >> redirection, you add a new line to the file. You can also use > redirection but then it will replace the existing content of the file.
|
||||
|
||||
More on redirection can be found in the below tutorial.
|
||||
|
||||
### Create new files using the cat command
|
||||
|
||||
The original purpose of the cat command was to concatenate files. However, it is primarily used for displaying the contents of a file.
|
||||
|
||||
It can also be used to create a new file with the option to add content. For that, you can use the same > and >> redirections.
|
||||
|
||||
```
|
||||
cat >> another_file
|
||||
```
|
||||
|
||||
But this one will create a new file and allow you to add some text to it. Adding text is optional. **You can exit the cat entering mode by using Ctrl+d keys.**
|
||||
|
||||
![Using cat command to create new file][6]
|
||||
|
||||
Again, the append mode >> adds new text at the end of file content while the clobber mode > replaces the existing content with new.
|
||||
|
||||
🖥️
|
||||
|
||||
Use the long listing display with ls -l and notice the timestamps. Now touch the file
|
||||
|
||||
```
|
||||
touch other_new_file
|
||||
```
|
||||
|
||||
. Do you see the difference in the timestamps?
|
||||
|
||||
### Test your knowledge
|
||||
|
||||
You have learned about creating new files. Here are a few simple exercises to practice what you just learned. It includes a little bit of the previous chapters as well.
|
||||
|
||||
- Use the touch command to create three new files named file1, file2 and file3. Hint: You don't need to run touch three times.
|
||||
- Create a directory called files and create a file named my_file in it.
|
||||
- Use the cat command to create a file called `your_file` and add the following text in it "This is your file".
|
||||
- Use the echo command to add a new line "This is our file" to your_file.
|
||||
- Display all the files in reverse chronological order (refer to chapter 3). Now use the touch command to modify the timestamp of file2 and file3. Now display the content in reverse chronological order again.
|
||||
|
||||
That's pretty fun. You are making good progress. You have learned to create new files in this chapter. Next, you'll learn about viewing the contents of a file.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/create-files/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://itsfoss.com/change-directories/
|
||||
[2]: https://itsfoss.com/make-directories/
|
||||
[3]: https://itsfoss.com/list-directory-content/
|
||||
[4]: https://itsfoss.com/content/images/2023/03/touch-example.svg
|
||||
[5]: https://itsfoss.com/content/images/2023/03/echo-example.svg
|
||||
[6]: https://itsfoss.com/content/images/2023/03/cat-example.svg
|
Loading…
Reference in New Issue
Block a user