Creating A File in LinuxCommandline
Creating A File in LinuxCommandline
Creating A File in LinuxCommandline
Table of Content
1. Creating file using the `cat` command in Linux
2. Creating file using the ` touch` command in Linux
3. Creating File using ` vi` or `vim` command in Linux
4. Creating file using ` nano` command in Linux
5. Creating a file using ` gedit` command in Linux
6. Using `mv` command to Create File in Linux
7. Creating file using ` printf` command in Linux
cat >file1
This command creates a new file file1 (in write mode) if it doesn’t exist in the present working
directory. If any file with file name file1 exists in the current directory, it is overwritten.
Note: After writing the text into the file, press ctrl+d to save and exit from the writing mode
cat >file1
cat file1
This command simply prints the contents of file1 on the terminal screen.
cat file1
1 of 6
Creating multiple files at same time using `touch` command in Linux
For example: If we want to change the timestamp of a file_name `file_1`. We use command as
follows:
touch file_1
Note: To save and exit from the vi-text editor, press the Escape key and then type :wq and hit
enter.
This command creates a new file file_1 and opens it on the vi-text editor if it doesn’t exist in
the present working directory. If a file with the file name file_1 exists in the current directory,
then this command just opens the file on the vi-text editor.
Use `ls` command to list files in the current directory and use `cat` command to see the content
inside a file.
2 of 6
Used `ls` command to list files in the current directory and used `cat` command to see the
content inside a file.
Create a file.
gedit file_2
This command creates a new file file_2 (in write mode) on the gedit text editor if it doesn’t
exist in the present working directory. If any file with file name file_2 exists in the current
directory, then it is opened (in edit mode) on the gedit text editor.
3 of 6
This command creates a new file file_2 (in write mode) on the gedit text editor if it doesn’t
exist in the present working directory. If any file with file name file_2 exists in the current
directory, then it is opened (in edit mode) on the gedit text editor.
Note: To use the terminal again, press ctrl + c.
This command crafts a new file named file_3 in the present working directory, incorporating
the contents of file_2. If file_3 doesn’t already exist, it is created. However, if a file with the
name file_3 is present in the current directory, its contents are replaced with those of file_2.
Importantly, it’s crucial to note that this command essentially involves copying the content of
file_2 to file_3 and subsequently deleting file_2.
mv command
Note: This command copies the content of file_2 to file_3 and deletes file_2.
4 of 6
Example:
If we want to write “hello connections” and want to create a file_name “file_1”
printf "hello connections"> file_1
Using touch and cat commands to create well formatted file content
Example
touch universities.txt
cat > universities.txt <<_EoF_
>Jooust Siaya
>MMUST Kakamega
>Kibabii Bungoma
>Alupe Busia
>Maseno Kisumu
>_EoF_
Output:
Jooust
MMUST
Kibabii
Alupe
Maseno
To number the multi-text lines, we use cat –n file2.txt as shown in the example below:
Output:
1. Jooust
2. MMUST
3. Kibabii
4. Alupe
5. Maseno
5 of 6
Useful Link(s)
https://www.geeks.org/man-command-in-linux-with-examples
6 of 6