gestionar-directorios-y-ficheros-desde-consola-comandos-en-raspberry-pi

Managing directories and files from command line on Raspberry Pi

  • 3 min

In this post from the Raspberry Pi section, we are going to see how to work with files and folders from the command line in Raspberry Pi.

Handling files and folders is absolutely essential, and something you will almost inevitably encounter 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, let’s dedicate a post to reviewing and compiling the necessary commands for managing files and folders in Raspbian.

Remember that the autocomplete feature for paths is very useful, simply by typing the first few letters and pressing the tab key.

The first thing we need to know is how to move between directories, for which we will use the ‘cd’ command (Change Directory).

So, to enter a directory within the current path we use,

cd nombreDirectorio

If we want to go up one level we do

cd ..

On the other hand, to go to the user’s home folder we use

cd ~

If at any time we want to see the current path, we use the pwd command (Print Working Directory).

pwd

Listing Files and Directories

Another essential skill is showing the content of the current path, listing the files and folders it contains. For this we use the ‘ls’ command (list). So, to show all files and directories we simply do

ls

We can also use an additional view with more details by doing,

ls -l

If we want to also show hidden files and directories we will use,

ls -a

Manipulating Directories

Now that we know how to navigate between directories and show their content, it’s time to see how to manipulate them.

To create a new directory we use the ‘mkdir’ command as follows,

mkdir directorio

We can also create more than one directory simultaneously by doing,

mkdir directorio1 directorio2

If instead we want to create a path of several directories nested within each other, we do the following,

mkdir -p /directorio1/directorio2

To delete a directory, only if it is completely empty, we do,

rmdir nombreDirectorio

On the other hand, if we want to delete a directory and its contents we use the command,

rm -r nombreDirectorio

To move a directory to another path we use this command,

mvdir nombreDirectorio destino

The mv command is also used 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 nombreDirectorio destino

Manipulating Files

Now we will focus on file management. To create a new empty file we can use the command,

touch archivo

To delete a file we use the command,

rm archivo

If we want to move a file to another destination, we use the command,

mv archivo destino

Similar to the case of directories, the ‘mv’ command is also used to rename files.

We can also copy the file to another location by doing,

cp origen destino

That easy! As you use your Raspberry Pi more, you will see that you use these commands very frequently. But, as we can see, it is very simple and you will soon internalize them and use them without even noticing.

In the next post we will see file permission management, a topic that can cause more than a few headaches. See you soon!