In Linux, there are various methods to create files. Here are some common methods:
1. touch Command
touch filename is used to create an empty file. If the file already exists, it only updates its access and modification time.
The touch command is typically used to update the access and modification times of a file to the current time. If the specified filename does not exist, a 0KB
empty file will be created. No text input is allowed while creating the file.
2. echo Command
echo “content” > filename is used to create a file containing the specified content. If the file exists, it will overwrite the original content.
This command is often used in scripts and batch files. It allows text input during file creation and also allows text input after the file is created. You can also use two redirection symbols ( >> ) to append content to the same file.
3. Text Editors
For example, text editors like vi, nano can open a new file for editing and saving, which will automatically create the file.
Using vi / vim to create a file. vim is a text editor compatible with vi, commonly used to edit plain text. The command vi filename can edit a file; if the filename does not exist, it will create a new one. Press esc->:wq to exit editing. Using nano to create a file.
nano is an editor. The command nano filename can edit a file; if the filename does not exist, it will create a new one.
nano is a text editor in Unix and Unix-like systems, a clone of Pico.
1. GNU nano is a small, friendly text editor. 2. Compared to basic text editors, nano offers many additional features, such as: interactive search and replace, navigating to specified lines and columns, automatic indentation, feature toggles, internationalization support, and filename completion. 3. The goal of nano is to be a full-featured yet easy-to-use editor similar to Pico. nano is free software that complies with the GNU General Public License, and since version 2.0.7, the license has been upgraded from GPLv2 to GPLv3.
After entering nano filename, you will enter the nano editor. Press ctrl+x to exit, then select y to save the file.
Ctrl + O: Save content without exiting; you can continue editing. Ctrl + X: Save and exit.
4. cat Command
cat > filename, then enter content and press Ctrl + D to save and exit, which will create a file containing the input content. cat stands for concatenate. The command cat filename can display the contents of a file; cat > filename can create a new file; this command will not end immediately after pressing enter; you can input content to be added to the new file and then press Ctrl+C or Ctrl+D to finish, or use cat >> filename to add other content to the same file.
5. cp Command
cp source_file destination_file is used to copy an existing file to a specified location and create a new file copy.
6. dd Command
dd if=/dev/zero of=filename bs=1M count=1 is used to create an empty file of specified size. The dd command: dd copies a file with a specified block size while performing specified conversions. Note: If the specified number ends with the following characters, multiply by the corresponding number: b=512; c=1; k=1024; w=2.
Parameter annotations:
if=filename: input filename, default is standard input, i.e., specify the source file. < if=input file > of=filename: output filename, default is standard output, i.e., specify the destination file. < of=output file > ibs=bytes: read bytes at a time, specifying a block size of bytes. obs=bytes: output bytes at a time, specifying a block size of bytes. bs=bytes: read and write up to BYTES bytes at a time (default: 512); overrides ibs and obs. cbs=bytes: convert bytes at a time, specifying the conversion buffer size. skip=blocks: skip blocks from the beginning of the input file before starting to copy. seek=blocks: skip blocks from the beginning of the output file before starting to copy.
Note: This is usually only effective when the output file is a disk or tape, i.e., effective when backing up to disk or tape.
count=N: copy only N input blocks. conv=conversion: convert the file with specified parameters. ascii: convert ebcdic to ascii. ebcdic: convert ascii to ebcdic. ibm: convert ascii to alternate ebcdic. block: convert each line to a length of cbs, filling insufficient parts with spaces. unblock: make each line length equal to cbs, filling insufficient parts with spaces. lcase: convert uppercase characters to lowercase characters. ucase: convert lowercase characters to uppercase characters. swab: swap every pair of input bytes. noerror: do not stop on error. notrunc: do not truncate the output file. sync: fill each input block to ibs bytes, padding insufficient parts with null (NUL) characters.
7. Scripting with Programming Languages
For example, use scripting languages like Python, Bash to write a program to create files.
Other articles discuss this; you can use chatGPT to create scripts, which is quite convenient.
8. Redirection Symbol ( > )
Usually used to redirect the output of a command to a new file; if there is no command, it will create a 0KB empty file. No text input is allowed while creating the file.
9. printf Command
Same as echo.
10. head Command
The head command prints the first 10 lines of a file to standard output by default.
If there are multiple files, each file will have a header indicating the filename.
You can also specify the size of the output file.
11. tail Command
The tail command prints the last 10 lines of a file to standard output by default, similar to the head command. tail -f extends this. Here’s an additional note: the tail command can be used to view the content of a file, and a commonly used parameter -f is often used to check changing log files. tail -f is equivalent to --follow=descriptor, tracking based on the file descriptor; tracking stops when the file is renamed or deleted. tail -F is equivalent to --follow=name --retry, tracking based on filename and keeping retrying; if the file is deleted or renamed, and a file with the same name is created again, it will continue tracking. tailf is equivalent to tail -f -n 10 (it seems tail -f or -F also defaults to printing the last 10 lines and then tracking the file); unlike tail -f, if the file does not grow, it will not access the disk file, making tailf particularly suitable for tracking log files on portable machines, as it reduces disk access and saves power.
Common Operations:
At this point, to pause refreshing, use 【Ctrl】+【S】 to pause the terminal. S stands for –sleep. To continue the terminal, use 【Ctrl】+【Q】. Q stands for –quiet. To stop sleeping keep trying, if you want to exit the tail command, simply use 【Ctrl】+【C】. If you want to exit the tail command, simply use 【Ctrl】+【Z】. Ctrl+c and ctrl+z are both interrupt commands, but their functions are different. The usage is the same in secureCRT.
Ctrl+c forcibly interrupts the execution of the program. Ctrl+z interrupts the task, but this task does not end; it remains in a suspended state. The user can use fg/bg operations to continue the task in the foreground or background; the fg command restarts the interrupted task in the foreground, and the bg command places the interrupted task in the background.
Print Content
2023-07-11 17:43:06.488 CST [0000000028] info
Meaning of Each Column in the Log
12. truncate Command
The truncate command is typically used to shrink or expand the size of a file to a specified size.

