In this lesson, you will learn how to create files in Linux.
In the previous lesson, we have looked at how to perform some basic tasks with basic Linux commands. We have also learned how to create directories.
Just as we had a chart that helped in the understanding of how to use basic Linux commands at the end of the previous lesson , the chart below will also help in the understanding of this subject matter.
From the chart above, we are going to create the files in circles. We have our directories in rectangle already created in our previous module. But before we do that, let’s understand some file related commands.
File related Commands In Linux
File related commands are listed below
This command is used to create a non-empty file
This command is used to create an empty-file
This command is used to append into an existing file
This command is used to view a file. (ASCII file)
This command is used to delete files
vi, vim, and nano
These commands are text editors. they are used to edit texts in a file.
Let’s start creating files with examples
How To Create Files Using The Basic Commands In Linux With Examples
Using the chart above, let’s create some files with examples
1. In the NETWORK_TEAM directory, create the N_mangers file and view the content. The contents in the N_managers file will include the following. Muhammadu Buhari, Donald Trump, and Vladimir Putin.
# cd IT_DEPARTMENT/NETWORK_TEAM
# cat > N_managers Muhammadu Buhari (press the enter key) Donald Trump (press the enter key) Vladimir Putin (press the enter key) Press ctrl+c
view the file content,
# cat N_managers
Also, see steps in screen-shot below.
2. In the CISCO_SWITCH_TEAM directory, create the full_staff file. The full staff names are Bayo ogulesi, Lionel Messi, Cristiano Ronaldo
# cd IT_DEPARTMENT/NETWORK_TEAM/CISCO_SWITCH_TEAM
# cat > full_staff
Also, see steps in screen-shot below
3. We forgot to Include Yemi Osibanjo among the full staff. Please include his name.
# cat >> full_staff Yemi Osibanjo (press enter) Press ctrl+c
Also see steps in the screen-shot below
4. In the IT_DEPARTMENT directory, create an empty file called cloud_team
# cd # cd IT_DEPARTMENT # touch cloud_team
(NOTE: cd takes you back to the home directory irrespective of where one is)
How To Delete Files In Linux With Examples
To delete a file in Linux, use the command,
# rm -f <file-name>
Sometimes, it is best to use the rm command with the -f option so you will not be prompted if you want to delete the file or not.
If it is a directory, you can use (rm -rf) command.
Don’t forget as discussed in our previous lesson, to get to see the option of a command, you can use the (–help).
For example, to see the options of the “rm” command, use the command,
[root@HQDEV1 ~]# rm --help Usage: rm [OPTION]... [FILE]... Remove (unlink) the FILE(s). -f, --force ignore nonexistent files and arguments, never prompt -i prompt before every removal -I prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes --------------------
Class Activity 2.1
1. In the CISCO_SWITCH_TEAM directory, create the temp_staff file. Their names are Dino Melaye, Bukola Saraki, and Andy ruiz.
2. In the cloud_team file, add any three names.
3. Create the COOs and CTOs file in the IT_DIRECTORS directory and add any three names each
4. Create the full_staff file in the NEXUS_STORAGE_TEAM directory
NEXT MODULE: COPYING AND MOVING FILES USING THE BASIC LINUX COMMANDS
Your feedback is welcomed. If you love others, you will share with others.
Leave a Reply