The ability to navigate a file systems is one of the most important requirements needed to manage a computer. To move, copy, view, create, delete, and manage various files and directories are a daily task of any system administrator. In this article, we will discuss some of the basic skills necessary to navigate through the Raspberry Pi Linux file system.
Basic Navigation Commands
Once you connect to your Raspberry Pi, you will automatically be located in your Home Directory. Think of the Directory as a large file cabinet containing other directories and files. Directories are sometimes referred to as folders. To confirm your directory location, enter the command
pwd (print working directory).
Note: Linux command system is case sensitive. So a command like
pwd will work, but if you entered
PWD you would get an error.
After you enter
pwd, it will respond with /home/pi unless of course you changed your username to something other than the default pi. In my case, the prompt is pi@CircuitBasics. The
$ is the prompt indicating normal user level. To list all the files and directories in any directory, use the command
ls (list). The result will be a listing of everything contained in the directory. Files are shown in white text, and directories will appear in blue text.
ls command displays everything in the current directory. Many Linux commands include optional flags that modify the output in more useful forms. For example, if you want a long list of the contents of a directory, enter
Using the ‘l’ flag, (ls -l) gives a long list, as shown above. “Total 40” indicates the total size of the directory in 1024 byte blocks. In the lines below it, “d” means that the entry is a directory. The succeeding letters (rwxr-xr-x, etc.) show the directory’s permissions, followed by the owner and size of the directory or file. The date and time when the file was created or opened, and the file’s name are also shown.
Here’s are some of the other flags available in Linux:
ls -a– lists all files including hidden files starting with “.”
ls -l– lists with the long format, showing permissions
ls -la– lists with the long format including hidden files
ls -lh– lists with the long format, and includes the readable file size
ls -r– lists in reverse order
ls -R– provides a recursive directory listing
ls -s– lists the file size
ls -S– sorts by file size
ls -t– sorts by time & date
ls -X– sorts by extension name
Working with Directories
Once logged in you’re ready to begin exploring. Now, let’s see how you might want to organize your Documents directory.
To move into your Documents directory, enter
cd Documents (change directory). Your prompt will change to
/Documents $ to indicate you are now in the Documents directory. If you type
ls for a listing of your Documents directory, it will most likely indicate that there is nothing there. So let’s create some directories you can use to store files.
Start by creating a directory called correspondence. Enter
mkdir Correspondence, then enter
ls to view the contents of your new directory. You should see a new directory Correspondence shown in blue. Next, make a second directory called Personal by entering
mkdir Personal. A sub-directory is also called a child directory. Its parent directory would be the one above it. In this case, the Documents directory is the parent directory of the Personal and Correspondence directories.
Note that making directories beginning with a capital letter is optional, you could use all upper case, all lower case or any combination of letters, numbers, periods, or underscores, up to 255 characters in length. If you make a file name that begins with a period, for example
.configuration, the file will be hidden, and not be visible unless you add the
-a flag to the
ls command. For example,
ls -al would list everything in a directory (including hidden files) in long format.
Move into the Correspondence directory by entering
cd Correspondence. A listing of the directory with
ls will show that the new directory is empty, which is no surprise since it’s new. So under the Correspondence directory, create a few more directories. You might want to make one for Family, Friends, Work, Legal, or whatever you feel you need. Now, when you write a letter to your mother that you want to save, you can save it in the Family directory.
The full path name of that directory would be /home/pi/Documents/Correspondence/Family/letter_to_mom. You can create as many sub-directories as you want, but it could become harder to manage.
To move back to your home directory from anywhere, simply enter
cd and you will be moved back to /home/pi. The diagram below depicts an abbreviated view of a Linux system. Linux is considered a hierarchical operating system. If represented graphically, it would resemble an inverted tree. The top of the tree is where you find the root directory. The name root came from the idea of an inverted tree. Most of the actual directories are not displayed, the clouds are to represent various directories and sub-directories associated with their parent directory.
Drawing the entire Linux systems directory would be like trying to write a complete map of New York on a matchbook cover!
Move, Copy and Delete Files
Change to the Documents directory by entering
cd Documents, and create a new file by entering
touch smallfile. Listing the directory contents with
ls will show a file named “smallfile”. The
touch command creates an empty file, which we will use to demonstrate how to move, copy, and delete files.
To move the file, use the command
mv smallfile .. The double periods (
..) indicate the directory above the current directory, also referred to as the parent directory. The “smallfile” file should now reside in /home/pi. You could have accomplished the same by using the full path name, or
cp /home/pi/Documents/smallfile /home/pi/. There are quite a few shortcuts, and you’ll become accustomed to them the more you use them.
A listing of the Document directory shows “smallfile” was moved. Now enter your home directory by entering
cd .., or
cd. Remember the command
cd and it moves you to home. A listing of your home directory will show that the “smallfile” has been placed there.
mv (move) command can also be used to rename a file, or directory. For example,
mv filename newfilename changes the name from “filename” to “newfilename”.
mv directory newdirectory renames the directories.
Comparing the copy (
cp) command to the move (
mv) command, the move command removes the file from the current directory and places it in the target directory. On the other hand, the copy command will make a copy of the file and place it in the target directory without removing it from the original directory. From your home directory, enter
cp smallfile Documents/Correspondence. This will make a copy of the “smallfile” file you made earlier, leaving you with two copies of the file.
You can also use the
mv command to relocate entire directories. From your home directory, you move the Correspondence directory by entering
mv Documents/Correspondence /home/pi/. This moves Correspondence to your home directory. Or, since you are currently in your home directory, you can achieve the same with the command
mv Documents/Correspondence .. Note the use of the single period
. instead of
. means the current working directory, so here, you’re moving Correspondence to your current directory.
.. means the directory above your current directory (the parent directory), and
. means the directory you are currently in. So if you are in the Document directory and there is a file that you want to move to the home directory, you should enter
mv file .. since the home directory (the parent directory) is above the Document directory.
Notice the difference between moving and copying a file or directory outside your home directory. Say you are in your Documents directory and you want to move or copy a file or directory from your Download directory to the /bin directory. The command you would enter is
copy /home/pi/Download/file /bin/. The source is /home/pi/Download/file and the destination is /bin.
Deleting Files and Directories
To delete a file or directory, use the remove command
rm. To delete a file in your Correspondence directory while being located in that directory, simply enter
rm filename. To remove the same file while in your home directory, enter
If you need to delete a file somewhere else on the system, for example /etc/xar.conf, you will need to use the full path name to the file. Assuming you have proper permission to erase a file in the /etc directory, enter
rm /etc/xar.conf to delete the xar.conf file. Notice here the use of the
/ symbol. Whenever you start a path name with
/, you need to provide a fully qualified path name.
A fully qualified path always starts at the root directory. A few examples of fully qualified path names would be /home/pi/Documents, /var/backups, or /etc/vim. Since all fully qualified path names start at the root directory, you can access them from anywhere on the system, assuming you have access privileges to the directory.
Deleting directories is similar to the way we delete files. From your home directory, if you decide to delete an empty directory, use the command
rm /path_to_the_directory. Of course if it’s immediately under the directory you’re currently in (a child directory), simply enter
rm Directoryname, unless there’s something stored in it. To delete a directory that has files or other directories under it, it will require the “-r” flag. Use caution when using the command
rm -r since everything under the directory and all the files can be deleted.
However, trying to delete a directory that you are currently occupying is not possible.
There are other methods for indicating the path to a file you want to target. For example, if you are in the Fiction directory, and want to delete the Mountains directory, use
rm ../../Pictures/Vacation/Mountains. The first instance of
.. moves up to the Bookshelf directory. The next
.. moves to the home directory, then Pictures, and Vacation. Finally, the Mountains directory is deleted. An easier way is to first enter
cd to move to the home directory, then enter
rm -r Pictures/Vacations/Mountains.
Using External Storage Devices
Your Raspberry Pi supports external storage devices, which makes it a convenient way to store and move large amounts of data. To simplify working with USB drives on Raspberry Pi, first, format the drive using a FAT32 file system. In the “Volume label” field shown in the image below, give the drive a simple name. Otherwise, Linux will assign a long serial number as the drive name.
Insert the USB drive into one of the available USB slots on your Raspberry Pi, and it will automatically mount the drive in the folder /media/pi/drive_name. “Drive_name” will be what you entered as the volume label when you formatted the drive.
To enter into the USB drive from your home directory, enter
cd /media/pi/drive_name (use the drive name you created). You can move or copy files and or directories into and out of the USB drive using the
cp commands discussed earlier. For example, if you want to copy all files from the Correspondence directory to the drive named “drive_name”, enter
cp /home/pi/Documents/Correspondence/* .. The
. means the current directory, and the
* symbol means everything in the directory. So you are copying all of the files from your Correspondence directory into the drive_name directory where you currently reside. Or if you want to move all the files from your drive leaving it empty, enter
mv * /home/Documents/Correspondence/.
Once you finish using the USB drive, unmount it before physically removing it. If you are currently in a directory inside the drive, you won’t be able to unmount it. Use the
cd command to move back to your home directory, then enter the command
umount /dev/sda1 to unmount it before removing the USB cable. Note that the command is
umount, not unmount.
Other Useful Commands
We will finish up by introducing you to some other useful Linux commands.
Wildcards are very handy when attempting to locate and work with files. Suppose you’re in your home directory and you want to move to the Personal directory you created earlier, enter
cd Documents/Personal to move there. Not terribly challenging, but what if you have to move to a sub-directory like this:
cd Documents/Personal/Letters/Friends? That’s a lot of typing! Using wildcards can simplify this task.
From your home directory, entering
cd Doc*/P*/L*/F* would get you to the same place. The
* means match anything after,
Doc*. This tells the operating system anything that starts with
Doc would match.
If you look at all the directories under your home directory you’ll see that the only thing that matches
Doc would be Documents. However, if you enter
cd D* you will get an error because it would try to match anything starting with D and there are two directories that start with D— Documents and Downloads. If you tried
cd Do*, the operating system will also fail to find a match since both Documents and Downloads start with “Do”. When you enter
cd Doc*, it matches a unique directory (Documents). Therefore,
cd Doc*/P*/L*/F* will match with Documents/Personal/Letters/Friends and will work fine.
Another wildcard used for matching is the ‘?’ that matches a single character. Say you have a directory with the following files: Sales1, Sales2, Sales3, Sales4, Sales5, SalesOct, SalesNov, SalesDec, Results1, Results2, Results3, Results4, Results5, and SalesTot. You only want to copy Sales1, Sales2, Sales3, Sales4 and Sales5 to a mounted USB drive within the path /media/pi/USB-Drive. Using the command
cp Sales? /media/pi/USB-DRIVE, you can now copy anything starting with “Sales” followed by a single character.
Another very useful command is the find command. Given the overall size of the Linux file system, how could you ever locate a file called ucf.conf? To do a universal search of the entire file system, you have to have admin permission to search every file. The command to use is
find / -name ucf.conf -print. But when you attempt this, you might end up with a lot of “Permission denied” statements. Remember, you need to have admin permissions to search the entire file system.
So instead, use
sudo find / -name ucf.conf -print. You don’t need to retype the entire command again, just hit arrow up on your keyboard to see the last command you entered. Then add the command
sudo to the beginning of the command and press enter.
Try the command again using the up arrow and change
ucf.*, and press enter. What do you think would happen if you tried
sudo find / -name ?cf.conf -print? To see a history of the commands you’ve entered, enter the command
One last shortcut you may find useful is autocomplete. Return to your home directory by entering
cd. Then enter
cd Dow, and press the Tab key. The Tab key will autocomplete the command you started, so you end up with the command
cd Downloads. But if you try to enter
cd Do followed by the Tab key, the autocomplete will not work since there are multiple directories that begin with the letters “Do”.
Sign Up & Get at Least Two $5 Coupons Now: https://jlcpcb.com/quote