In this post of the Raspberry Pi section, we are going to see how to work with files and folders from the command line on Raspberry Pi.
Managing files and folders is something absolutely essential, and you will almost inevitably encounter it when working with Raspberry Pi.
Fortunately, it is quite simple, and in all operating systems (Windows, Linux, Mac…), the commands are similar or even the same.
So, we are going to dedicate an entry to remember and put together the necessary commands to manage files and folders in Raspbian.
Remember that the ability to auto-complete paths by simply typing the first letters and pressing the tab key is very useful.
Navigate between directories
The first thing we need to know is how to move between directories, for which we will use the ‘cd’ (Change Directory) command.
So, to enter within a directory within the current path, we use,
If we want to go up a level, we will do
On the other hand, to go to the user’s root folder, we use
If at any time we want to see the current path, we use the ‘pwd’ (Print Working Directory) command.
List files and directories
Another important knowledge is to display the contents of the current path, listing the files and folders it contains. To do this, we use the ‘ls’ (list) command. So, to display all the files and directories, we simply do
We can also use an additional visualization with more details by doing,
If we want to show, in addition, the hidden files and directories, we will use,
Now that we know how to navigate between directories and display their contents, it’s time to see how to manipulate them.
To create a new directory, we use the ‘mkdir’ command as follows,
We can also create more than one directory simultaneously by doing,
mkdir directory1 directory2
If instead we want to create a path of several directories one inside another, we do the following,
mkdir -p /directory1/directory2
To delete a directory, only if it is completely empty, we do,
On the other hand, if we want to delete a directory and its contents, we use the command,
rm -r directoryName
To move a directory to another path, we use this command,
mvdir directoryName destination
The ‘mv’ command also serves to rename directories, a function that does not have its own command in Linux. It is simply considered that ‘moving’ a directory is the same function as renaming it.
On the other hand, to copy a directory to another path we will use,
cp -r directoryName destination
Now we will focus on file management. To create a new empty file, we can use the command,
To delete a file, we use the command,
If we want to move a file to another destination, we use the command,
mv file destination
Similar to the case of directories, the ‘mv’ command also serves to rename files.
We can also copy the file to another location by doing,
cp source destination
That’s it! As you use your Raspberry more, you will see that you use these commands very frequently. But, as you can see, it is very simple and you will internalize them and use them without even realizing it.
In the next entry, we will see file permission management, a matter that can cause more than one headache. See you soon!