File management

You can use the terminal in Raspbian to manipulate files. In this section, we will describe some of the basic commands used to manage files in Raspbian.

Create files

Although there are several ways to create a file in the terminal, the most common way to create a file is by using the touch command. Although the primary purpose of this command is to update the timestamp of a file, it also can be used to create one. The syntax of this command is simple: just type touch, followed by the filename. If the file with that name doesn’t exist, a new empty file is created. For example, to create a new file called textfile.txt in the /home/pi/ directory, we can use the following command:

touch command

As you can see in the picture above, a file called textfile.txt was created.

Deleting files

The rm command is used to delete a file. For example, to delete the file we’ve just created (/home/pi/textfile.txt), the following command can be used:

rm command

Copy a file

To copy a file, use the cp command with two parameters: the source file you would like to copy and the destination directory. For example, to copy the /home/pi/textfile.txt to the /tmp/ directory, we would use the following command.

copy file

Displaying the content of a file

If a file is a text file, you can display its content in a text editor or text viewer. There are many programs in Raspbian to display the content of a file. One of the most commonly used is the less program. This program displays a file’s contents one screen at a time. The syntax of this command is simple: just type the less command, followed by the name of the file you would like to display. For example, to display the content of the /home/pi/textfile.txt, we can use the following command:

less command

The file content is displayed in the less program window:

less program

To quit the less program, press q.

Another way you can display the content of a text file is by using the cat command. This command displays the content directly at the terminal. For example, to display the content of textfile.txt, we can use the following command:

cat command

Geek University 2021