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

Managing directories and files from command line on Raspberry Pi

  • 3 min

Managing files and directories from the command line means creating, moving, copying, deleting, and navigating through the file system using commands.

Handling files and folders is absolutely essential, and you will run into it almost inevitably when working with Raspberry Pi or any Linux system.

Fortunately, it is quite simple, and in many operating systems (Windows, Linux, macOS…) the commands are similar or even the same.

So, let us gather the basic commands for managing files and folders in Linux, which will be useful both in Raspberry Pi OS and in any similar distribution.

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

With this, we have now covered the basic operations on files and directories. They are very simple commands, but you are going to use them constantly, so it is worth getting them well internalized.