Manage files

The Linux shell provides powerful tools to manipulating files. As with directories, you can move, rename or delete a file. If a file is a text file you can also use tools to edit it. In this lesson, we will describe a couple of command that can help you work with files in a Linux environment.

Creating files

There are several ways to create a file in Linux. The most common way to create a file in Linux is by using the touch command. Although the primary purpose of this command is to update the timestamp of a file, it is often instead used to create one. The syntax is simple: you just type touch followed by the name of the file. If the file doesn’t exist, a new empty file will be created.

Here is an example:

linux touch command

As you can see in the picture above, a file called new_file is created.

Deleting files

To delete a file you can use the rm command. The syntax: rm FILE_NAME

Example:

 linux rm command

In the picture above you can see that new_file has been deleted with the rm command.

Displaying the file content

There are many tools in Linux to display the content of a file. One of the most commonly used ones is the less program. This program displays a file’s content one screen at a time. You can navigate the file by using the arrow keys or the mouse wheel.

The usage is simple: simply type the less command, followed with the name of the file you would like to display.

linux less command

In the picture above we can see the content of the file named textfile.txt.

 

Geek University 2022