In windows, we usually create a file by right clicking in folder and choose “create a xxx file”, that’s it.
While in Linux, it’s totally different ways to create a file, and there are many ways we can create a file in linux.
1. vi / vim / nano, etc
In Linux, use file editor is the most basic way to create a file. When we use file editor like vi / vim / nano /touch… etc to open a new file (which is not exist) then save and quit. A new file has been created.
For example, when under folder: /var/demo, we use ls command we see there is no files, now we are going to create a file named: test.txt
So we use vi test.txt
This will open the test.txt and allow edit it if it’s exist already, if not, this will open a new file named test.txt
No matter if we have edited the content or not, just save it and exit this file (in vi / vim we use :wq), this is how we creating a file use editor.
Note: There are many editors in Linux OS, the principle is: open a new file and save it, that’s it.
2. cp / mv / unzip / tar, etc
We can also create a file using operation command cp / mv /unzip / tar … etc, these commands allow us create a new file from a existing file.
cp is used to copy a file, for example:
cp test.txt test2.txt
In this way, we use cp command created file test2.txt
mv is used to move a file, for example:
mv test.txt test3.txt
After this, there is no test.txt anymore, which has been renamed to test3.txt. Also we can move test.txt to a different directory, eg:
mv test.txt /var/scripts/test3.txt
unzip is used to unzip a zip file, for example, there is a zip file named happiness.zip, the code is:
This will unzip happiness.zip to extract all files of the archive happiness.zip into the current directory and subdirectories below it, creating any subdirectories as necessary.
tar is a program used to create, maintain, modify, and extract files that are archived in the tar format. for example, if there is a compressed file named: file0001.tar.gz need to be extracted.
tar zxvf file0001.tar.gz
It will extract all the files of the archive file0001.tar.gz into the current directory and subdirectories below it, creating any subdirectories as necessary.
Note: As you can see, these ways is not create a file from nothing, but from existing files, by changing their locations, format or names, etc. Please keep in mind that the new files created will overwrite all existing files as the same name, and this is very hard to recover.
3. Use variety programs
Last kind of creating files is using programs, there are so many programs in Linux can create a new file that we can’t enumerate all of them. The principle of these ways is generating or collecting data into a new file.
cat test.txt > /var/demo/file1
echo “This is a example” > /var/demo/file2
printf ‘This is a example\n’ > file3
tcpdump -i eth0 host 192.168.0.1 -s 3000 -n -w /var/demo/file4 (tcpdump is a tool to capture data on network interface)
grabserial -d /dev/ttyS0 -o /var/demo/file5 (grabserial is a tool which can listen and capture the data on serial ports)
minicom -D /dev/ttyS1 -C /var/demo/file6 (minicom is a tool which can listen send data or capture data on serial ports)
There are so many programs can generate files, even you can write your own program by shell scripts, C, python … – any languages of the world.
Creating files in Linux is so fun that we can use editors, operation commands and programs, etc. There are so many creative methods which makes Linux so handy and attractive.