Create a file in linux, you can do this!

We use the linux system daily through terminal commands, and often need to create files.

So what method (command) do you use to create files?

Let ’s share with you what commands I often use to create files in different scenarios

scene 1

When I write a blog, I need to create some files for example files.

At this time, I use the linux touch command create an empty file.

➜  touch example.file
➜  ls
example.file

scene 2

When we need to write a shell script, we need to open the file directly and write the script.

At this time, I will use the linux vim/vi command “Create file”.

➜  vim script.sh
# Enter the script code and save and exit.
➜  ls
script.sh

scene 3

Similar to scenario 2, when you need to rewrite existing code or optimize, you can use the linux cp command “Create file”.

Then, edit with vim command.

➜  cp ../vim/script.sh .
➜  ls
script.sh

scene 4

Redirect “>” or “>>”.

Creating files in this way is more used in text processing, such as filtering file contents, merging files, and so on.

1.After sorting the first column of the test.log file, create and write a new file.

2.Use echo, cat, head and other print commands to create a new file with redirection symbols and write the print.

➜  echo "hello, world" > test.log
➜  ls
test.log

The “>> symbol is an additional file content symbol, of course, it can also be used to create a new file.

➜  echo "hello, world" >> appendFile.log
➜  ls
appendFile.log test.log
➜ 

3.Some display commands, such as linux date command

➜  date +'%Y-%m-%d-%H-%M-%S' > date.log
➜  ls
appendFile.log date.log       test.log
➜  cat date.log
2019-12-28-11-02-38

Best Article:

Linux common commands tutorial and use examples
Sed command tutorials in linux/unix with examples and use cases
Awk command tutorials in linux/unix with examples and use cases

Add a Comment

Your email address will not be published.