This following text is an edited copy from - https://app.simplenote.com/publish/TD4bBS – written by is Jason Oleham, KM4ACK.

It describes some basic Linux commands and a brief explanation of their function and use.

Published here with permission of the author.


This will be helpful for Hams entering the new world of Raspberry Pi’s and suffering the Linux invasion of the Shack!
Please visit Jason’s YouTube channel at:
https://www.youtube.com/channel/UCSQhXfGo_68Ta8-2wStAWkw
Enjoy it!
Jorge VE3EAD

 ~~~~~~~~~~~~~~~~~~~~~~~

Bits & Bytes 2022
By Jason KM4ACK

This is a collection of the Bits & Bytes section from each week's newsletter. Check back as this page updates weekly. Not on the newsletter list? What are you waiting for? No spam! Just a weekly newsletter chocked full of helpful information for radio operators and Linux enthusiasts. Signup today!

ls - The ls command is used for listing out the contents of a directory. To see even more details there are switches available as well. Want to know who owns a particular file? Try ls -l Looking for a hidden file? Try ls -a To find out more about the ls command, run man ls

Something that is handy to know is that you can often combine switches in Linux. So instead of using ls -a or ls -l we can combine this into one command ls -al. This command will show us all of the files (including hidden files) as well as giving us more details the size of the file and who owns it.

 

pwd - Stand for Print Working Directory and does exactly what it says. If you open your terminal, it will open in your home directory but doesn't indicate the full path. By running the pwd you will see that you are actually in the /home/pi directory assuming your user name is pi. Now move into your downloads directory by running cd Downloads. Next run the pwd command again and you will get /home/pi/Downloads which is the full path of the directory your are currently viewing.

 

cd - stands for change directory. Seems simple enough but there are a few things you might not know when first starting with Linux. Open your terminal and you will be in your home directory (/home/pi). Now you can move to your Downloads directory with cd Downloads. You can also move deeper into the file system without having to run the cd command for each directory change. Try running cd /usr/share/applications. When it is time to return to the home directory you can run either cd or cd ~ Both accomplish the same thing. Once last trick with the cd command. You can run cd - to jump back to the directory you were just in. Go ahead and try it. If you have followed each command in order so far, you will be returned to the /usr/share/applications directory. Run it once more and you will be back in your home directory.

 

mkdir - Is the command used for creating a new directory. As you might have guessed, it stands for "make directory" and is super simple to use. Open your terminal window and give it a try by running mkdir test Now if you run the ls command, you will see your new test directory in the listing. You can move into the new directory with cd test To move back to your home directory, just type cd and press enter.

touch - The touch commands primary function is to update the timestamp of a file. However, we can also use the touch command to create a new file. Open the terminal and run touch my-test-file Now if we run the ls command, you will see your new file called "my-test-file".

 

rm - Is the command used to remove a file. Over the last two weeks I have shown you how to create a directory using the mkdir command and a file using the touch command. Today, let's look at removing a file. First, create a new file by running touch my-test-file. We can see our new file by running the ls command. Now we can delete that same file by running rm my-test-file Running the ls command again will confirm that we deleted the file.

rm -r We have already covered removing a file with the rm command but if you tried that same command on a directory, you got an error indicating that you can't remove the file because it is a directory. We just need to use the "-r" switch to make it happen. First, open the terminal and create a new directory by running mkdir test. You can see the new directory by running the ls command. Ready to delete the new directory? Just run rm -r test and it will vanish. You can confirm by running the ls command again. Be careful though, this command doesn't ask "Are you are really sure you want to". It simply does as you command. Once you press enter, there's no going back!

 cp - is the command we use to copy a file. First, let's create a new file by running touch test. Now that we have a new file, let's create a copy of that file. This time run cp test test.bkup Now run the ls command and you will see both your test file and the new test.bkup file. This is a command I use on a regular basis when creating a backup copy of a file before making changes to the original. This is really useful before changing config files or modifying scripts. It can save your bacon if the changes you make don't work out and you need to get back to the original. Let's assume you made some changes to the test file and decided that it was a bad idea and needed to restore from your backup. Simply run cp test.bkup test and you will be back to where you started :-)

cp -r :: This command may seem like a repeat of last week but it is used when copying directories. The "-r" switch tells the system that you want to do a recursive copy. This means that you not only want to copy the directory but also everything the directory contains. So if you have files and sub directories inside the directory, everything gets copied to the new location. It is used like this cp -r /path/to/old/directory /path/to/new/directory

After last week's tip, I got an email from Michael, KI7QIB, who reminded me about the "-p" switch when using the cp command. The "-p" switch will preserve the date/time of the file(s) that you are copying so that they truly look identical. Without the "-p" switch, your copies will have a newer date/time than the originals.

 mv - Is the move command and is used to move a file(s) to a new location. Open the terminal and let's create a new file by running touch move-test. We can see the new file by running the ls command. Now let's move our new file to the Documents directory with mv move-test ~/Documents/ To confirm the move we could either list the Documents directory with ls ~/Documents/ or we could move to the Documents folder with cd ~/Documents/ and then run the ls command. We also use the move command to rename a file. Try running mv ~/Documents/move-test ~/Documents/move-test1 Now if you run ls ~/Documents/ you will find the file has been renamed. To remove the test file, use rm ~/Documents/move-test1

cat - is short for concatenate and is the command we use to show the contents of a file on the screen. There is a file on your Pi called "os-release" that lives in the /etc/ directory. To view the contents, run cat /etc/os-release and you will be presented with some information about the operating system running on your pi. Try it with other text files on your Pi. Just run cat <filename>

 head - is the command we use when we only need to see the first part of a file. By default, it will present the first 10 lines of a file. Try running head /etc/os-release Now if you remember from last week, we used the cat command on the same file. There is no difference in this case when using head as the file only contains 10 lines so the entire file is presented with both commands. However, we can also use a switch with the head command to limit the results even more. This time try head -5 /etc/os-release and you will only see the first 5 lines of the file.

 tail - is the command we use to only show the bottom of a file. It is the inverse of the head command we covered last week. Just like with head, we can use a switch with the tail command. Give it a try by running tail -5 /etc/os-release Want to see the entire file? Run tail /etc/os-release This works in this case because the os-release file is only 10 lines long and tail shows us the last 10 lines by default.

tail -f When we use the tail command and the -f switch, we can monitor a file "live" for updates. This can especially be useful when monitoring log files. Today, let's do something super simple to show you how it works. Open the terminal and navigate to your desktop by running cd ~/Desktop Next, let's create a text file by running touch test.txt Now that the text file is created we can use the tail command by running tail -f ~/Desktop/test.txt When you run that command it will be "watching" for new lines of text to appear in the file so let's give it some new lines. Open another terminal window and run echo "Hello World" >> ~/Desktop/test.txt As soon as you press enter, you should see "Hello World" appear in the terminal window running the tail command. Anytime the test.txt file is updated, tail will show you the updates. You can add another update by running echo "tail is a cool command" >> ~/Desktop/test.txt To end the tail command, use ctrl+c

 less - less is a useful command when you want to look through a long document. If we used the cat command on a long document, the entire file scrolls off the screen before you have a chance to read it. Less gives us control to scroll the file as needed. We will keep it simple for this tutorial, just run less filename substituting "filename" with the name of the file you wish to view. Once it opens, you will see the beginning of the file. Pressing the space bar will move you forward by one screen. Pressing "q" will exit. There are a lot of other commands you might find useful. Run man less to see more commands. If you have direwolf installed on your Pi, run less ~/direwolf.conf

 date - is a command we can run from the command line that will spit out today's date and time. We can also modify the output of the date command. Try running date +%d%b%y To see additional output options, run man date. The date command can prove helpful when writing scripts where you need to modify the filename by adding the date to the end of the file.

 uptime - The uptime command will show you how long the system has been online and the load averages for the system. A help switch is "-p". Run uptime -p

cat /proc/cpuinfo - This isn't so much of a command but rather displays cpu information about the system. Go ahead and run cat /proc/cpuinfo to see what it will tell you about your Pi.

cat /proc/meminfo - Similar to the information we covered last week, this will display memory information about the system.

 

df - is the command we use that will tell us how much space is taken/available on our Pi and connected drives. It's ok by itself but running it with the -h switch makes is much easier to read. Try running just the df command and then try it with df -h to see the difference. -h makes it human readable :-)

 

du - is similar to the df command that we covered last week. Instead of giving us the drive space data, he du command will give you the size of of the contents of a directory. It is also helpful to use the "-h" switch to give us the information in a human readable format. Try running it on your home directory with du -h ~/ and on your Downloads directory with du -h ~/Downloads

free - Over the last couple of weeks, we have learned how to find drive information and directory information. The free command is used when we need information about the memory usage of the Pi. Try running free at the terminal. We can also use it with the "-h" switch to make it easier to read. Try free -h

 $PATH - This week we are taking a look at a variable instead of a specific command. The $PATH variable holds a list of directories from which our Pi searches when it is looking for a command we try to run from the command line. Want to see where your Pi is searching? Run echo $PATH which will return the full path of each directory. The list of directories is separated by a ":"

 whereis - is a handy command to know. It will tell you where an application lives provided it is in your $PATH. Try it now by running whereis direwolf On my Pi, it returned direwolf: /usr/local/bin/direwolf but maybe you don't have direwolf installed. If not, you will only get direwolf: in return. Try it with other applications. Run: whereis flrig or whereis js8call

 which - will tell you which application is opened by default. You may have both python2 and python3 installed on your system. By running which python you can see which application is opened/used by default.

 nano - The Pi and most all Linux distributions have built in text editors that can be accessed from the command line. Nano is just one of the editor included with the Pi OS. These are useful for creating new documents or editing existing ones. Give it a try by opening the terminal and running nano testdoc.txt. Once the editor opens, add some text. Press ctrl+s to save and then ctrl+x to exit. We can see the contents of the file by running cat testdoc.txt. Want to add some additional text, just run nano testdoc.txt again and edit until your heart's content. Ready to remove the file from your system? Running rm testdoc.txt will delete it forever.

apt-update
apt-upgrade

Undoubtedly, if you have played with the Pi for very long, you have seen the two commands above but have you wondered what they do exactly? apt-update will update the package index. The package index is a database of all of the packages available in the repository. We always run this first to be certain we are working with the latest information. After running the update, we run apt-upgrade. This command will look at the packages you have installed from the repository and verify they are the latest versions. If anything is found out of date, it will install the latest versions. Keep in mind that this only updates the software installed from the repository. This will not work for software built from source. So why would we build from source? Building from source will get you the very latest version of an application which often isn't available from the repository.

 

apt-cache search - Last week we discussed apt update and apt upgrade. This week let's talk about a lesser known command apt-cache search. Have you ever tried to install something on your Linux box only to discover you were missing a dependency? Or maybe you just weren't quite sure of a package name you needed to install. Well this is where apt-cache search can come to the rescue. This command will search the repository and help you locate the package you need. It still may take a bit of research on your part, but this command can help speed the process. Let's take a look at using this to help us locate the Firefox browser package name. If we run sudo apt install firefox, you will get a package not found error. Run apt-cache search firefox in the terminal and you will get a long list of packages. Scroll up in the list and you will find "firefox-esr". Just to the right of the name will be the description of the package. Now that we know the correct package name, we can now install firefox by running sudo apt install firefox-esr.

 

> This is more of a tip than an actual command. The > symbol allows us to redirect the output of a command to a file. Last week, we learned how to use the apt-cache search command. Sometimes this can produce a very long list that may not allow you to scroll back through the entire list in the terminal. If this happens, then the > can be used. Try running apt-cache search firefox > ~/Desktop/firefox.txt. You will see no output in the terminal but it will create a text file on your desktop called firefox.txt which can be opened with your text editor. If you have Pat Winlink installed, try it with this command: pat rmslist > ~/Desktop/gateways.txt which will produce a file on your desktop that contains every Winlink gateway for every band.

 

>> I showed you how to use the redirect ">" last week but what if you want to add to a file instead? Well that is easy enough with ">>". Let's give it a test. Run: echo "This is my test" > ~/Desktop/testfile.txt which will create a file on your desktop called testfile.txt with "This is my test" as the first line. Now let's add to the file we just created. Run: echo "This is a cool demo!" >> ~/Desktop/testfile.txt Next run cat ~/Desktop/testfile.txt and you will find that both lines of text are included. The ">>" can be used in many applications. I use it often in scripts for Build a Pi when I want to append something to the bottom of a log file.

 

/etc/os-release Up until now we have taken a look at basic Linux commands. Over the next month or so, we will look at ways to get specific information about your Pi. Today's information can be accessed with cat /etc/os-release and will give you some basic information about the OS loaded onto your Pi.

 /proc/cpuinfo Want to know everything about the CPU in your machine? Today's information can be accessed by running cat /proc/cpuinfo and will give you more information that you need about your CPU.

/proc/meminfo Similar to last week's command, by running cat /proc/meminfo you will find more information that you need about your memory.

/proc/partitions - The last in this series will give you details about the partitions on your Pi. Run cat /proc/partitions. Next week we start looking at vcgencmd

 

vcgencmd measure_temp - For the next few week's we will look at how to get some measurements from your Pi. We will start off by looking the the CPU temp. To see it, run vcgencmd measure_temp

vcgencmd measure_clock arm - Today's command will show you the speed at which the CPU is running. To see it, run vcgencmd measure_clock arm. Want to see it update in real time? Try running watch -n 1 vcgencmd measure_clock arm. This will update the info every second. Use ctrl+c to escape.

vcgencmd measure_volts core - Today's command will show you how many volts your Pi is using. To see the volts run vcgencmd measure_volts core. Just like last week we can watch it in real time by running watch -n 1 vcgencmd measure_volts core. This will update the info every second. Use ctrl+c to escape.

hostname - Running this week's command will net you the hostname of your Linux box. Not really helpful most of the time in my opinion. However, if we include the "-I" (that's a capital i) switch it becomes much more useful. Try running hostname -I and it will return the Pi's current IP address. If you are connected to both ethernet and WiFi, the command will return the current IP address of both.

 locate - is a great search tool for Linux but it may not be installed on your Pi yet. To install, run sudo apt install locate. Now we need to update the data base with sudo updatedb. At this point you should be ready to go. To search for a file, simply run locate <FILENAME>. Try it with locate flrig.desktop. Assuming you have FLRIG installed, it should show you where the desktop file for FLRIG is located.

 grep - is a fantastic search tool that can be used to search for something inside a file. Give it a whirl by running grep VERSION /etc/os-release Keep in mind that by default, grep is case sensitive. However, with the -i switch, it will ignore the case. Running grep version /etc/os-release returns nothing. Now try it with grep -i version /etc/os-release