Tuesday, August 23, 2022

Linux Commands For Raspberry Pi Users - Part#1

If you are venturing into Raspberry Pi (RPi), without a good hold on Linux, this series of articles might be of interest to you. Let me present a list of useful Linux commands those will be handy for you to operate from the RPi Command Line Interface (CLI). Especially if you have loaded the Raspberry Pi OS Lite version of the OS image, which is the leaner version of the OS, you will be left with the only option of operating from the CLI.

First thought of mentioning a few basics about the directory structure for the new users. As the Raspberry Pi OS is based on Linux, everything is organized under the "root" directory, which is denoted as "/".  The working directory for an user is usually at a location "/home/<username>". If the username is "pi", then the working directory for that user would be "/home/pi". Note that, this working directory is under the root directory "/". 

The working directory for a particular user, which is know as the home directory for that user is denoted as "~". Once you login to the Raspberry Pi, you will be in your working directory "~". To see which directory you are in currently, type:

$ pwd 

The above command reveals the "present working directory" to you.

You could change directory to the root directory by typing: 

$ cd /

But it is worth cautioning here, avoid messing up with the root directory unless you know what you are doing. We could move back to the home directory by using the command:

$ cd ~

Or, simply, type:

$ cd

This might be useful to know that your previous working directory is stored in a variable $OLDPWD. In case, you want to change to the previous working directory, type the command:

$ cd $OLDPWD

Next, you might want to know the content of a particular directory. The following command is a quick one to know the other directories and files stored in a directory.

$ ls [flags] [directory path]

The flags, which are optional, control the format and the level of details that will be displayed about the content. The path for the directory for which this command is to be executed is also option. If the path is omitted, the list command will be executed for the present working directory.

To learn more about the options available for the flags, type the following command:  

$ man ls

The above command brings out the manual of the command "ls".

Let us now see a handful of frequently used flags and how powerful those could be. Executing the "ls" command without any flags, would just show the names of the directories and files stored under the present working directory. Note that, the hidden items are not shown. In order to show all directories and files including the hidden ones, you need to use the option "-a": 

$ ls -a

Any file that begins with a "." is considered a hidden file in the Linux environment.

To know more details about the file or the directory, type the following command:

$ ls -l

The flag "-l" reveals the detailed information such as the owners of the file/directory, permission, date of last modification, size in bytes etc. along with the name of the file or directory.

If you need to see the subdirectories under each of the directories in the present path, type:

$ ls -R

The "-R" (capital R) flag in the "ls" command will list all directories with their corresponding subdirectories down to the last file. Obviously, this command will also show the files in the current directory.

Now let us see how we could list the content of the present directory by sorting them by their size and time. Use the flag "-S" (capital S) to sort the content with respect to their sizes, in descending order (biggest to smallest):

$ ls -S

Similarly, use the flag "-t" (small t) to sort the content according to the time last modified, in descending order (most recent first).

$ ls -t

If you want to reverse the order, simply add "r" to the above two commands:

$ ls -Sr

$ ls -tr

The above two commands will reverse the order of sorting respectively. Now it is interesting to note here, you could combine multiple flags as shown above. However, the above commands will not show the size or time information. In order to show those information too, you could combine "-l" flag with the respecting commands, for example:

$ ls -S -l

OR:

$ ls -Sl

OR:

$ ls -lS

etc.

Is not it so flexible and powerful feature of Linux? I am sure you will love it eventually.

Now, could you please try the command yourself:

$ ls -lSr

What does the above command yield?

In the following part of this series we will see the commands to create, copy and move files/directories.

Saturday, July 16, 2022

Let's not trash a microSD card before we try this!

I came across a corrupted microSD the first time and therefore thought of documenting the story for me to remember later and sharing the problem along with the fix, in case it serves you in some way. If you have found a microSD card, that behaves like a healthy one but does not show all the disk space it was supposed to have, this article might help you. The good thing was that the 16 GB microSD card (shown in picture# 1) was detected but was only showing 256 MB as the full capacity. 


Picture# 1: The 16 GB microSD card, I had trouble with

First I tried formatting the card, which did not help. I have also tried to enable any hidden files so that I could delete them. I could not see any hidden files or folders which could have explained why the card was showing only a tiny piece of its entire capacity.

As I was getting frustrated and increasingly tempted to buy a new one for my RPi Zero W, found the recommendation in the webpages mentioned under references. All of those talk about using the DISKPART utility that comes with Windows. Details about the DISKPART utility and the commands could be found in the link below:



Picture# 2: The commands to use the DISKPART utility to restore a microSD to its full capacity

To launch the DISKPART command interpreter, I launched the "Command Prompt", typed: diskpart and hit Enter button. A message popped up asking if I wanted to allow the app to make changes to my device. Clicked on yes to launch the DISKPART interpreter window. Next, I followed the five steps in the sequence that are shown in picture# 2, and, voila!! it worked! My microSD was restored to its full capacity of 16 GB! It saved money for me! 

References: