Essential Linux Basics
1. Introduction
Most users are accustomed to Windows graphical interface operations, but due to the limited resources of the development board, the default system does not have a desktop environment, and many operations (such as system configuration) cannot be completed through the graphical interface.However, almost all programs can be run through the command line. Mastering commonly used Linux commands is therefore crucial for beginners. This chapter introduces frequently used Linux commands to enhance the experience of developing on the Luckfox Lyra.
2. Terminal Overview
On the Luckfox Lyra, you can access the terminal through a serial port ,ADB or via SSH.The Luckfox Lyra default terminal prompt looks like this:
root@luckfox:~#root: Current username or login name.luckfox: Hostname.- #: Denotes a root user login
3. Linux File Structure
In Windows, each partition is an independent tree structure, with one tree per partition. In Linux, however, there is only one tree structure, with all files and partitions existing under a single hierarchy. The root directory is at the top, and all other directories, files, and partitions are created beneath it. Common directories include:
- /bin: Contains binary executable files related to the Lyra system .
- /dev: Device directory; in Linux, all devices are treated as files. This directory contains device files, such as
sdafor the first SATA hard drive or USB stick. - /etc: System management and configuration files.
- /lib: Stores dynamic link libraries for the basic system.
- /media: Contains mount points for removable drives like USB sticks and optical drives.
- /root: The home directory for the system administrator (also known as the superuser).
4. File System
In Linux, all resources managed by the operating system—such as network cards, disk drives, printers, input/output devices, regular files, or directories—are treated as files. This concept, "everything is a file," is fundamental to Linux. The File System is a mechanism used by the operating system to manage and organize data on storage devices (like hard drives, SSDs, and flash memory). It defines how files are stored, retrieved, and managed. Common Linux file systems include:
- EXT2: An early Linux file system without journaling, suitable for small storage devices like USB drives.
- EXT3: An upgraded version of EXT2 with journaling, significantly improving data security and file recovery.
- EXT4: The most widely used Linux file system, supporting large files (up to 16 TB) and robust performance, stability, and security.
- XFS: A high-performance 64-bit journaling file system designed for large files and high-concurrency applications.
- JFFS2 and UBIFS: File systems designed for flash storage. They feature wear leveling and power failure protection, reducing storage wear.
4.1 Partition Table Configuration
The partition table information for the Luckfox Lyra is specified in the mtdparts parameter located in luckfox-lyra-sdk/device/rockchip/rk3506/parameter-*-.txt:
| Storage Medium | Partition Table |
|---|---|
| SDMMC | mtdparts=rk29xxnand:0x00002000@0x00002000(uboot),0x00006000@0x00004000(boot),-@0x00010000(rootfs:grow) |
- The size of each partition is specified in sectors, and each sector is 512 bytes.
uboot:0x00002000represents 8192 sectors, i.e., 8192 × 512 bytes = 4 MiB, used to store the U-Boot bootloader.boot:0x00006000represents 24576 sectors, i.e., 24576 × 512 bytes = 12 MiB, used to store boot files.rootfs: Starts at0x00010000and occupies the remaining available space, storing the root file system with support for dynamic expansion.
4.2 Partition Mount Information
The df -Th command displays the mounted file systems, providing details about their type, size, and usage. On a Buildroot system using eMMC, the file system defaults to the UBIFS format.
root@luckfox:~# df -Th
df -Th
Filesystem Type Size Used Available Use% Mounted on
/dev/root ext4 6.9G 175.4M 6.4G 3% /
devtmpfs devtmpfs 233.1M 0 233.1M 0% /dev
tmpfs tmpfs 233.1M 80.0K 233.1M 0% /var/log
tmpfs tmpfs 233.1M 8.0K 233.1M 0% /tmp
tmpfs tmpfs 233.1M 140.0K 233.0M 0% /run
tmpfs tmpfs 233.1M 0 233.1M 0% /dev/shm
/dev/rootfsis the root file system (/)devtmpfsandtmpfsare virtual file systems used for device nodes and temporary file storage, respectively. These are created in memory and do not consume actual flash storage space.
4.3 Symbolic Links vs. Hard Links
| Feature | Hard Link | Symbolic Link |
|---|---|---|
| Reference Method | Points to the same inode | Points to a file path |
| Cross-File System Support | Not supported | Supported |
| Effect of Source Deletion | Link remains valid | Link becomes invalid |
| Creation Command | ln filename linkname | ln -s target linkname |
| Supported Targets | Any file | Any file or directory |
5. Frequently Used Linux Commands
ls
Command Function
The ls command is used to display the contents of a specified working directory (i.e., list the files and subdirectories in the current working directory).
Command Format
ls [-ahl] directory]
Parameter Description
| Parameter | Description | Path |
|---|---|---|
| directory | Target directory to display | Can be a relative or absolute path. |
Usage Examples
ls -a # Display all files and directories, including hidden files (starting with .)
ls -l # List detailed information such as file type, permissions, owner, and size
ls -lh # Show file sizes in a human-readable format, e.g., 4K
ls -i # Display inode numbers of files
cd
Command Function
The cd command is used to change the current working directory. The default root directory for LuckFox lyra is the root directory.
Command Format
cd [ directory ]
Parameter Description
| Parameter | Description | Value |
|---|---|---|
| directory | Target directory to switch to. Can be a relative or absolute path. | A string without spaces; absolute path length is 1–64. |
Usage Examples
cd # Return to the user's home directory
cd .. # Navigate to the parent directory
cd /bin # Enter the /bin directory
cd ../.. # Move up two levels in the current directory
cd ~ # Enter the user's home directory
cd - # Switch to the last visited directory
pwd
Command Function
The pwd command displays the working directory. Running this command shows the absolute path of the current working directory.
Usage Examples
# pwd
/bin # Output
df
Command Function
The df command displays disk space usage statistics for the file systems in the Linux system.
Usage Examples
root@luckfox:~# df -Th
Filesystem Type Size Used Available Use% Mounted on
/dev/root ext4 6.9G 175.4M 6.4G 3% /
devtmpfs devtmpfs 233.1M 0 233.1M 0% /dev
tmpfs tmpfs 233.1M 80.0K 233.1M 0% /var/log
tmpfs tmpfs 233.1M 8.0K 233.1M 0% /tmp
tmpfs tmpfs 233.1M 140.0K 233.0M 0% /run
tmpfs tmpfs 233.1M 0 233.1M 0% /dev/shm
touch
Command Function
The touch command modifies the timestamp attributes (access and modification times) of a file or directory. If the file does not exist, it creates a new file.
Usage Examples
touch file.txt # Create a new file named "file.txt"
mkdir
Command Function
The mkdir command is used to create directories.
Command Format
mkdir [ options ] directory
Parameter Description
| Parameter | Description |
|---|---|
| options | -p Create parent directories if they do not exist |
| directory | Name of the directory to create. |
Usage Examples
mkdir luckfox # Create a subdirectory named "luckfox"
mkdir -p luckfox/test # Create a directory named "luckfox/test"
cp
Command Function
The cp command is mainly used to copy files or directories.
Command Format
cp [ options ] source dest
Parameter Description
| Parameter | Description |
|---|---|
| options | Options like -r, -f, etc. |
| source | Path and name of the file to copy. |
| dest | Path and name of the destination. |
Usage Examples
cp test.txt /bin # Copy "test.txt" to the /bin directory
cp –r test/ /usr/newtest # Copy "test" directory to /usr and rename it to "newtest"
mv
Command Function
The mv command is used to rename files or directories or move them to another location.
Command Format
mv [ options ] source dest
Parameter Description
| Parameter | Description |
|---|---|
| options | Options like -i, -f, etc. |
| source | Path and name of the file to move. |
| dest | Path and name of the destination. |
Usage Examples
mv file1 /userdata # Move "file1" to the /userdata directory
rm
Command Function
The rm command is used to delete files or directories.
Command Format
rm [ options ] file/directory
Parameter Description
| Parameter | Description |
|---|---|
| options | Options like -r, -f, etc. |
| file/directory | Name of the file or directory to delete. |
Usage Examples
rm test.txt # Delete the file "test.txt"
rm -r Luckfox # Delete the directory "Luckfox"
chmod
Command Description
The chmod command is used to control file permissions for users in Linux.
Command Explanation
File permissions in Linux/Unix are divided into three levels: file owner (Owner), group (Group), and other users (Other Users).
In the terminal output below, the most important part is the first column, which describes file and directory permissions in detail. The third and fourth columns show the user or group that the file or directory belongs to.
# ls -lh
drwxr-xr-x 2 1002 1002 160 media
lrwxrwxrwx 1 1002 1002 3 lib64 -> lib
lrwxrwxrwx 1 1002 1002 3 lib32 -> lib
drwxrwxr-x 6 1002 1002 664 rockchip_test
drwxrwxr-x 5 1002 1002 576 userdata
lrwxrwxrwx 1 1002 1002 11 linuxrc -> bin/busybox
drwxr-xr-x 2 1002 1002 160 root
drwxrwxr-x 2 1002 1002 2.2K sbin
dr-xr-xr-x 114 root root 0 proc
lrwxrwxrwx 1 1002 1002 8 data -> userdata
drwxrwxr-x 6 1002 1002 544 usr
drwxr-xr-x 4 1002 1002 672 var
dr-xr-xr-x 13 root root 0 sys
drwxrwxrwt 2 root root 60 tmp
drwxr-xr-x 3 root root 80 run
drwxr-xr-x 2 1002 1002 160 opt
drwxrwxr-x 3 1002 1002 296 oem
drwxr-xr-x 3 1002 1002 224 mnt
drwxrwxr-x 3 1002 1002 2.0K lib
drwxrwxr-x 6 1002 1002 1.5K etc
drwxr-xr-x 12 root root 1.9K dev
drwxrwxr-x 2 1002 1002 4.1K binFile attributes in Linux can be divided into three types: read (r), write (w), and execute (x). However, the file attributes shown above are divided into 10 small cells because, apart from the first cell that indicates the directory, the other three groups of three cells each represent the file owner's permissions, the permissions of the same group, and the permissions for other users. In the first column, if it shows "d," it means it is a directory; if it is a symbolic link, it shows "l"; if it is a device file, it shows "c".
- The first "rwx" column: "- rwx --- ---" represents the permissions owned by the file owner.
- The second "rwx" column: "- --- rwx ---" represents the permissions of users in the same group.
- The third "rwx" column: "- --- --- rwx" represents the permissions of other users.
- "rwx rwx rwx" means that any user can read, write, and execute this file.
- "rw- --- ---" means that only the file owner has read and write permissions, but no execute permissions.
- "rw- rw- rw-" means that all users have read and write permissions.
Symbolic Mode
who (user type)
who User Type Description u user File owner g group Users in the same group o others All other users a all All users, equivalent to ugo operator (symbolic mode table)
Operator Description + Add permissions for the specified user type - Remove permissions for the specified user type = Set the specified user's permissions (reset) permission (symbolic mode table)
Mode Name Description r Read Set read permission w Write Set write permission x Execute Set execute permission X Special Execute Set execute permission only if the file is a directory or if other users have execute permission s Setuid/gid Set setuid or setgid permission when the file is executed, depending on the who parameter specified t Sticky Bit Set the sticky bit; only the superuser can set this, and only the file owner can use it Examples
chmod a+r file # Add read permission for all users to the file
chmod a-x file # Remove execute permission for all users from the file
chmod a+rw file # Add read and write permissions for all users to the file
chmod +rwx file # Add read, write, and execute permissions for all users to the file
chmod u=rw,go= file # Set read and write permissions for the file owner and clear all permissions for the user group and other users (space indicates no permission)
chmod -R u+r,go-r LUCKFOX # Add read permissions for the user to the file and its subdirectory hierarchy, and remove read permissions for the user group and other users
Numeric Mode
Octal Syntax
# Permission rwx Binary 7 Read + Write + Execute rwx 111 6 Read + Write rw- 110 5 Read + Execute r-x 101 4 Read r-- 100 3 Write + Execute -wx 011 2 Write -w- 010 1 Execute --x 001 0 None --- 000 For example: Explanation of 765:
- Numeric representation of owner's permissions: The sum of the numbers of the owner's three permission bits. For example, rwx, which is 4 + 2 + 1, should be 7.
- Numeric representation of group's permissions: The sum of the numbers of the group's permission bits. For example, rw-, which is 4 + 2 + 0, should be 6.
- Numeric representation of other users' permissions: The sum of the numbers of the other users' permission bits. For example, r-x, which is 4 + 0 + 1, should be 5.
Common numeric permissions
- 400 - r-- --- --- Owner can read, others can't do anything.
- 644 - rw- r-- r-- Owner and group can read, only owner can edit.
- 660 - rw- rw- --- Owner and group can read and write, others can't do anything.
- 664 - rw- rw- r-- Everyone can read, but only owner and group can edit.
- 700 - rwx --- --- Owner can read, write, and execute, others can't do anything.
- 744 - rwx r-- r-- Everyone can read, but only owner can edit and execute.
- 755 - rwx r-x r-x Everyone can read and execute, but only owner can edit.
- 777 - rwx rwx rwx Everyone can read, write, and execute (not recommended).
Examples
chmod 664 file # Add read permission for all users, owner and group have edit permission
tar
Command Function
The tar command is a utility for creating and extracting backup files. It can unpack files contained in an archive. While tar itself does not compress files, it can be combined with compression tools (like gzip or bzip2) to create compressed archives (e.g., .tar.gz or .tar.bz2).
Command Format
tar [options] -f luckfox.tar [file/directory]
Parameter Descriptions
| Parameter | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| options | -x | Extract files from an archive | |||||||
| -c | Create a new archive | ||||||||
| -v | Show detailed operation progess | ||||||||
| -f | Specify the name of the archive file (must be the last option in the list) | ||||||||
| -z | Use gzip for compression or decompression | ||||||||
| -j | Use gzip for compression or decompression | ||||||||
| -J | Use xz for compression or decompression | ||||||||
| file/directory | The target file or directory to compress or extract | ||||||||
Usage Examples
Create Compressed Files
tar -czvf luckfox.tar.gz 1.txt 2.txt 3.txt directory # Compress files and directories using gzip
tar -czvf luckfox.tar.gz directory # Compress using gzipExtract Compressed Files
tar -xzvf luckfox.tar.gz # Extract gzip-compressed files
find
Command Function
The find command is used to search for files in a specified directory. Any string preceding the options is considered the directory name to search. If no options are specified, find searches the current directory for subdirectories and files, displaying all found items.
Command Format
find [path][match condition] [action]
Command Format
| Parameter | Description |
|---|---|
| path | The directory path to search. Can include multiple paths separated by spaces. Defaults to the current directory if unspecified. |
| match condition | Specifies search criteria, such as filename, file type, or size. |
| action | Specifies actions to perform on matched files, like deleting or copying. |
Usage Examples
Find a file by name (e.g., searching for
luckfox.py):sudo find -name luckfox.pySearch within specific directory depths:
sudo find /etc/ -maxdepth 1 -name *.conf # Search up to 1 level deep in /etc for .conf files
sudo find /etc/ -mindepth 2 -name *.conf # Search within subdirectories of /etc, starting from depth 2List files updated in the last 20 days:
sudo find . -ctime -20Find files with specific permissions:
sudo find . -type f -perm 644 -exec ls -l {} \;List zero-length files with their full paths:
sudo find / -type f -size 0 -exec ls -l {} \;
cat
Command Function
The cat command is used to concatenate files and display their contents on the standard output. Its primary purposes are viewing and combining files
Command Format
cat [options][file]
Parameter Description
| Parameter | Description |
|---|---|
| options | -n: Display line numbers, adding a number to each line of output. |
| file | File name. |
Usage Examples
Display the contents of the
test.txtfile in the terminal:cat test.txtList the contents of all
.txtfiles in the current directory:cat *.txtAppend standard input to the end of the
file:cat >> fileRedirect standard input to
file, overwriting its content:cat > fileCombine the contents of
file1andfile2intofile3:cat file1 file2 > file3
grep
Command Function
As one of the Linux "text-processing trinity" (along with sed and awk), the grep command searches for patterns in a file or stream, listing all occurrences of a specific string or pattern.
Command Format
grep [options] pattern [files]
Parameter Description
| Parameter | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| options | -i | Ignore case while matching. | |||||||
| -v | Perform inverse matching, displaying non-matching lines. | ||||||||
| -n | Display the line number of matching lines. | ||||||||
| -r | Recursively search files in subdirectories. | ||||||||
| -l | Display only the names of matching files. | ||||||||
| -c | Display only the count of matching lines. | ||||||||
| pattern | The string or regular expression to search for. | ||||||||
| files | File(s) to search, allowing multiple files. | ||||||||
Usage Examples
Search for files containing the string
sysin the current directory:ls | grep sysSearch
config.txtfor lines starting witharmusing a regular expression:grep ^arm config.txtFind and display lines containing
testin files with names ending infile:grep test *fileRecursively search the
dirfolder for lines matching the regular expressionluckfox, displaying filenames and line numbers:grep -r -n luckfox dir/
ifconfig
Command Function
The ifconfig command is used to view and configure network devices. It allows you to adapt network settings in response to environmental changes. Note: Configurations made using ifconfig are not persistent and will be lost after a restart unless written to the appropriate network configuration files.
Usage Examples
sudo ifconfig eth0 # View wired network IP address
sudo ifconfig wlan0 # View wireless network IP address
poweroff & reboot
For Lyra, it is also not recommended to unplug the power cord directly. If booting from a TF card, unplugging the power cord directly may result in data loss or corruption on the TF card, leading to system startup failures. Use the following commands instead:
poweroff # Shut down immediately
reboot # Reboot (commonly used)
6. Rockchip Common Commands
Check the unique chip serial number:
cat /proc/cpuinfo | grep SerialCheck the CPU temperature.
echo "$(cat /sys/class/thermal/thermal_zone0/temp) / 1000" | bc