This article is a collection of basic Linux commands and procedures relevant to using your embedded system.
Note: This article is meant as an overview to get you started, thus you may find useful to search for more comprehensive tutorials, guides and manuals over the internet.
The following contents will be presented:
This section aims to introduce you to the basic Linux commands touch, cp, cd, ls, mv, mkdir, rm and pwd, as well as some concepts regarding directory structure.
Note: Whenever you want a quick help for a specific command, use --help option, such as in the example below:
The pwd (present working directory) command output tells you the full path to the directory you are inside:
The touch command creates an empty file:
By using the pwd command, you already know the present directory is /home/root. Let's create two directories named dir1 and dir2 inside it:
mkdir dir1 dir2
Note: This step explains the directory structure for a Linux environment. You might skip it once this is a theoretical step. Click the button below to show.
In the Linux environment, there is the notion of absolute or full path and relative path. See the following directory structure:
There are some notations for describing a directory path:
The full path points to a location in a file system without depending on the current directory. The picture below presents the absolute paths for the previously presented directory structure:
The relative path points to a location in a file system relative to the current path. The picture below presents the relative paths for the previously presented directory structure, considering /home/root the present directory:
Change to the recently created dir1:
#relative pathcd dir1#absolute pathcd /home/root/dir1
Go back to the parent directory:
#relative pathcd ..#from anywhere (three possibilities)cdcd ~cd /home/root
Go to the root directory:
Please go back to the home directory before proceeding to the next step.
Use the cp command to copy files. Let's copy the sample_file.txt around:
#copy with the same name to dir1cp sample_file.txt dir1#copy to dir2 and rename file to copy_dir2_file.txtcp sample_file.txt ./dir1/copy_dir2_file.txt#make a copy to the same directory using the name copy_file.txtcp sample_file.txt copy_file.txt
In order to copy a whole directory and its contents, preserving file attributes:
cp -a dir1 dir1_copy
If you want to copy the directory contents inside an already created directory:
mkdir dir1_contentscp -a dir1/. dir1_contents
Note: Use the ls command in the next step to verify the directories contents.
Use the ls command to see the directory contents:
lsls /homels /bin
You can see detailed information about the files and directories inside the one being inspected:
ls -l /binls -lh /bin
The mv command can be used to rename or move a file/directory:
touch sample_file.txtmv sample_file.txt renamed.txtmv dir2 directory2
Use the rm command to delete a file or directory.
Warning: The process is not reversible, so be careful to not delete something important!
#Delete filerm renamed.txt#Delete directoryrm -r dir1_copy#Ask before deletingrm -ri dir1_contents
This section explains how to read and write to a file directly from command-line and also using a text editor.
Use the echo command to write to a file from the command-line.
Note: This command will be useful to set/configure hardware features from the command-line, although it is limited for text files editing.
#create or overwrite fileecho "Hello World" > readwrite.txt#create or append to fileecho "Hello Again" >> readwrite.txt
Use the cat command to read a file from the command-line.
cat readwrite.txt#print line numbers, ignoring or not the empty linescat -b readwrite.txtcat -n readwrite.txt
Note: Vi and also the Vim editor have many features and commands. Search the web if you want to dive deep into learning how to use them.
To create or edit a file using the Vi text editor:
When you open the application, you are in the normal mode and cannot edit the text. Press the "i" key to enter the "insert" mode. Then you can edit the file as you will.
After you make some changes, press the "ESC" key to return to the "normal" mode. In this mode you can save changes and exit Vi. To do so, type the following:
This is the command for write -> quit.
To go to specific line:
# :<line>, for instance::4
To search for a specific text:
# /<text>, for instance/world
Press the "n" key to browse through the
This sections describes how to search for a specific file and how to search a directory for specific file text contents.
To search for a specific file, use the find command. The search is made inside a specific directory and all of its children directories:
#find <path> -name "<filename>"#search for a specific file namefind . -name "readwrite.txt"#search all files with a specific extensionfind / -name "*.txt"#search all files containing "read" in the name, for the current directoryfind . -name "*read*"#search all files containing "read" in the name, for the user directoryfind /home/root/ -name "*read*"
To search for a file that has a specific text as content, you can use the grep command. Grep is useful not only for file contents search, but also as a filter to the output of other commands, for instance.
#search for files containing the "hello world" string in the current directorygrep -nre "hello world" ./*#case insensitive search with absolute pathgrep -nrie "hello world" /home/root/*
This section describes how to compress and decompress folders using the tar command. It does not cover all the possibilities, only some of the most commonly used.
#compress folder and contentstar -cjvf compressed.tar.bz2 dir1tar -cvf compressed.tar.gz dir1#compress only the folder contents (notice that there is a dot after the directory)tar -cjvf compressed.tar.bz2 -C ./dir1 .tar -cvf compressed.tar.gz -C ./dir1 .
#decompress to current directorytar -xjvf compressed.tar.bz2tar -xvf compressed.tar.gz#decompress to specific directorytar -cjvf compressed.tar.bz2 -C ./dir1tar -cvf compressed.tar.gz -C ./dir1
There are two relevant date/time clocks in Linux: the hardware date/time, usually referred to as real-time clock (RTC) and the system date/time. While the command timedatectl takes care of synchronizing both of them, usually the date command is used to get the system time, due to its simpler output.
The hwclock command is commonly used in systems that don't have a program such as timedatectl to synchronize the system and hardware clocks.
Get the clock times and system configuration:
# timedatectlLocal time: Thu 2017-01-12 13:34:00 BRSTUniversal time: Thu 2017-01-12 15:34:00 UTCRTC time: Thu 2017-01-12 13:34:01Time zone: America/Sao_Paulo (BRST, -0200)Network time on: yesNTP synchronized: noRTC in local TZ: yes
Set the system clock and synchronize changes with the hardware clock:
timedatectl set-ntp falsetimedatectl set-time "2017-01-12 13:40:11"
Change the time zone:
#list available time zonestimedatectl list-timezones#change time zonetimedatectl set-timezone America/Sao_Paulo
Get the system date/time:
# dateThu Jan 12 13:56:43 BRST 2017
Note: Hwclock usage will not be presented. For hwclock and also more date and timedatectl usage, please use the option --help, as in hwclock --help.
In this chapter you will learn how to create a shell script, run it in the background and kill it. A simple script that prints a string to the terminal every 5 seconds will be used as an example.
Create a file named print.sh using the vi editor, with the following contents:
#!/bin/sh while true do echo "Hello world!" sleep 5 done
Warning: The line #1/bin/sh is not a commented line! It tells that the application used to execute the script is /bin/sh, therefore you must not erase this line.
Before running it, you have to make the file executable:
chmod +x print.sh
Run the script in the foreground:
Note: Press CTRL+C to stop the script, or any command-line application, from running. Sometimes there are applications that don't exit on CTRL+C such as the vi text editor, but in general it works.
To run the script in the background, just add the "&" character to the end of the line:
./print.sh &#Notice that you can use the terminal, but CTRL+C won't stop the script from running:datels -lpwd
Use the command ps to get a snapshot of all the processes running. Combine it with the grep command to search for a specific task/application:
#list all processesps aux#find only the processes containing "print" in the nameps aux | grep print
The second output column will print the process ID (PID).
Use kill to stop the script
See the figure below for reference regarding the previous steps:
Note: For this specific case, you could use the fg command to bring the process to the foreground and then use CTRL+C to stop it.
Note: Behind the kill command, there is the concept of Unix/Linux signals. You may want to search in the web for more information.
Htop is an interactive process viewer for Unix systems. It is not the only option available (there is also top) but it was chosen for this how-to because of its user-friendly interface.
To run htop:
You should see a screen similar to the one presented in the figure below:
It presents system status and statistics, such as: CPU and RAM usage (the pre-built images have swap disabled by default); system tasks; load average and; uptime. Below the overall status, all of the system tasks are listed, as well as their individual resources usage and other relevant information.
Note: To exit htop press F10.
As an exampĺe of htop functionality, run the stress command in the background to see the CPU usage go up:
./print.sh &stress -c 1 &htop
Use the arrow keys to browse through the processes until you find the stress task. You can also use the search functionality pressing F3 or use your mouse to click on the process line:
Select SIGTERM and press ENTER to kill the process.
Note: When using the kill system call without parameters, as in the previous step, it sends a SIGTERM to the process.
In this section, a service that runs a script at system startup will be created.
Create the file /etc/systemd/system/print.service with the following contents. You can use the vi text editor, previously presented:
[Unit] Description=Start the print.sh script After=multi-user.target [Service] Type=simple ExecStart=/home/root/print.sh [Install] WantedBy=multi-user.target
Reload the configurations and enable the service at startup:
systemctl daemon-reloadsystemctl enable print.service
Start the script manually for the first time and see its status. Notice that the script doesn't print directly to the terminal, since a new shell session is created by the service.
systemctl start print.servicesystemctl status print.service
Reboot the system and verify that the script started:
rebootps aux | grep print.sh
This section describes how to change the login password.
Use the command passwd to change the system password:
# passwdEnter new UNIX password:Retype new UNIX password:passwd: password updated successfully
This section shows how to download files from the web using wget.
Warning: Your embedded system must have internet access.
Use the wget command to download a file from the web:
wget -c https://docs.toradex.com/102075-colibri-imx6-datasheet.pdf
The file will be downloaded in the present directory.
OPKG is the package manager for the Ångström distribution. This section shows how to list, install and remove packages.
Warning: Your embedded system must have internet access.
First of all, update the packages list:
List the available packages and filter your search using grep:
opkg listopkg list | grep libpython
Note: The Python programming language has many packages available. If you filter using python instead of libpython, there will be many results to browse through
To install a package:
opkg install libpython2.7-1.0