Linux Basics
1. Introduction
Most users are accustomed to the graphical interface of Windows, but due to the limited resources of development boards, the default system does not come with a desktop environment, making certain tasks (such as system configuration) impossible to complete through a graphical interface. However, almost all programs can be called and run via the command line, so mastering common Linux commands is crucial for getting started. To make it easier to use the Luckfox Pico, this chapter will introduce the commonly used Linux commands during development on the Luckfox Pico.
2. Introduction to the Terminal
On the Luckfox Pico series development boards, you can access the terminal via ADB, serial, or SSH login. The default prompt for Luckfox Pico is as follows:
[root@luckfox ]#
root
indicates the current username / login name.luckfox
indicates the hostname.$
indicates a normal user login.#
indicates a root user login.
3. Linux File Directory
In Windows, each partition is a separate tree structure, and the number of partitions corresponds to the number of tree structures. However, in Linux, there is only one tree structure, where all files and partitions are part of this single hierarchy. At the top of this structure is the root directory, and all other directories, files, and partitions are created under it. Common directories include:
- /bin: Contains binary executable files related to the Luckfox Pico system (including those required to run the graphical interface).
- /boot: The boot directory, which contains the Linux kernel and other packages needed to start Luckfox Pico.
- /dev: The device directory, where all devices in the Linux system are treated as files. For example, the first SATA hard drive or USB flash drive will be recognized as the
sda
file. - /etc: System management and configuration files.
- /lib: Location of dynamic libraries for the basic system.
- /media: Mount point for removable storage devices such as USB drives and optical drives.
- /root: The home directory for the system administrator, also known as the superuser.
4. Storage Media and File System Types
In the Linux operating system, all resources managed by the system, such as network interface cards, disk drives, printers, input/output devices, regular files, or directories, are treated as files. This is an important concept in Linux, known as "everything is a file." The file system (File System) is the mechanism used by the operating system to manage and organize data on storage devices such as hard drives, SSDs, and flash memory. Its core function is to define how files are stored, retrieved, and managed on these devices. Linux supports a wide range of file systems, with common ones including the EXT series (EXT2, EXT3, EXT4), XFS, and specialized file systems for NAND-type devices like UBIFS, JFFS2, and YAFFS2.
Storage Media | Supported Read-Write File System Formats | Supported Read-Only File System Formats |
---|---|---|
eMMC | ext4 | squashfs |
spi nand 或slc nand | ubifs | squashfs |
spi nor | jffs2 | squashfs |
- EXT2: An early Linux file system without journaling, suitable for small storage devices like USB drives.
- EXT3: An upgrade to EXT2 with journaling support, significantly enhancing data safety and file recovery.
- EXT4: Currently the most commonly used Linux file system, supporting large files (up to 16 TB files and 1 EB file systems), with excellent performance, stability, and security.
- XFS: A high-performance 64-bit journaling file system designed for handling large files and high-concurrency applications.
- JFFS2 and UBIFS: Filesystems specifically designed for flash storage with wear leveling and power failure protection to reduce storage wear. JFFS2 (Journaling Flash File System v2) provides a log structure and bad block management, but performance and memory consumption increase with capacity. UBIFS (Unsorted Block Image File System) supports dynamic file management and direct mounting, suitable for large-capacity NAND flash. It works with the UBI layer to achieve more efficient wear leveling and bad block management, along with data compression.
4.1 Partition Table
The SDK uses the env
partition to set up the partition table. The partition table information is configured in <SDK/>/project/cfg/BoardConfig*.mk
under the RK_PARTITION_CMD_IN_ENV
parameter.
Storage Media | Partition Table |
---|---|
eMMC | RK_PARTITION_CMD_IN_ENV="32K(env),512K@32K(idblock),256K(uboot),32M(boot),512M(oem),256M(userdata),6G(rootfs)" |
spi nand | RK_PARTITION_CMD_IN_ENV="256K(env),256K@256K(idblock),512K(uboot),4M(boot),30M(oem),10M(userdata),80M(rootfs)" |
Notes:
- Partitions are separated by commas
","
. - Partition sizes can be specified in units such as K/M/G/T/P/E, and case-insensitivity applies. If no unit is provided, it defaults to bytes; a "-" indicates the partition will take up the remaining space.
- If the first partition starts at the 0x0 address, no offset is required. Otherwise, an offset must be provided. Subsequent partitions can optionally include an offset.
- The offset and size of the idblock partition are fixed and should not be modified.
- It is not recommended to modify the
env
partition name. (If you need to change the address and size ofenv
, you must modify the corresponding U-Boot defconfig settingsCONFIG_ENV_OFFSET
andCONFIG_ENV_SIZE
, recompile the firmware, erase theenv
data from the 0 address on the board, and reflash the new firmware.)
4.2 Partition Mount Information
The df -Th
command displays the currently mounted file systems, showing which partitions have been mounted, along with the type, size, and usage of each mount point.
[root@luckfox ]# df -Th
Filesystem Type Size Used Available Use% Mounted on
ubi0:rootfs ubifs 67.1M 62.0M 5.1M 92% /
devtmpfs devtmpfs 16.4M 0 16.4M 0% /dev
tmpfs tmpfs 16.5M 0 16.5M 0% /dev/shm
tmpfs tmpfs 16.5M 168.0K 16.4M 1% /tmp
tmpfs tmpfs 16.5M 520.0K 16.0M 3% /run
/dev/ubi4_0 ubifs 22.4M 17.1M 5.3M 76% /oem
/dev/ubi5_0 ubifs 4.5M 24.0K 4.5M 1% /userdata
ubi0:rootfs
is the root filesystem (/
), mounted on UBIFS (UBI Flash File System).devtmpfs
andtmpfs
are virtual file systems used for device nodes and temporary file storage, created in memory and do not occupy actual flash space./dev/ubi4_0
is theoem
partition, mainly storingRK
-related drivers./dev/ubi5_0
is theuserdata
partition, primarily storingrkipc
configuration files andMAC
addresses.
5. Common Commands Guide
ls
Command Description
The ls command is used to display the contents of a specified working directory (lists 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 # List all files and directories (including hidden files starting with .)
ls -l # List detailed information about file types, permissions, owners, file sizes, etc.
ls -lh # List file sizes in a more human-readable format, e.g., 4K
cd
Command Description
The cd command is used to change the current working directory. On LuckFox Pico 、LuckFox Pico Mini A/B and LuckFox Pico Plus/Pro/Max, the default working directory is the root directory.
Command Format
cd [ directory ]
Parameter Description
Parameter | Description | Values |
---|---|---|
directory | The target directory to switch to, either relative or absolute path. | String, no spaces, absolute path length: 1 to 64. |
Usage Examples
cd # Return to the user's home directory
cd .. # Go up one level in the directory hierarchy
cd /bin # Enter the /bin directory
cd ../.. # Go up two levels in the current directory
cd ~ # Enter the user's home directory
cd - # Switch to the last visited directory
pwd
Command Description
The pwd command is used to display the current working directory. Executing the pwd command immediately reveals the absolute path of your current working directory.
Usage Example
# pwd
/bin # Output result
df
The df command is used to display statistics on file system disk usage on a Linux system.
Usage Example
# df -h
Filesystem Size Used Available Use% Mounted on
ubi0:rootfs 24.2M 5.4M 18.7M 23% /
devtmpfs 16.8M 0 16.8M 0% /dev
tmpfs 16.8M 0 16.8M 0% /dev/shm
tmpfs 16.8M 8.0K 16.8M 0% /tmp
tmpfs 16.8M 124.0K 16.7M 1% /run
/dev/ubi5_0 38.5M 17.1M 21.4M 44% /oem
/dev/ubi6_0 24.2M 40.0K 24.2M 0% /userdata
touch
Command Description
The touch command is used to modify the time attributes of a file or directory, including access time and modification time. If the file does not exist, a new file is created.
Usage Example
touch file.txt # Create a new file named file.txt
mkdir
Command Description
The mkdir command is used to create a directory.
Command Format
mkdir [ options ] directory
Parameter Description
Parameter | Description |
---|---|
options | -p: Create parent directories if they do not exist |
directory | Specifies the name of the directory to be created. |
Usage Examples
mkdir luckfox # Create a subdirectory named luckfox
mkdir -p luckfox/test # Create a directory named luckfox/test and create the parent directory luckfox if it doesn't exist
cp
Command Description
The cp command is primarily used to copy files or directories.
Command Format
cp [ options ] source dest
Parameter Description
Parameter | Description |
---|---|
options | -r, -f, etc. (options) |
source | Specifies the path and source filename to be copied. |
dest | Specifies the path and destination filename. |
Usage Examples
cp test.txt /bin # Copy test.txt to the /bin directory
cp –r test/ /usr/newtest # Copy the test directory to /usr and rename it as newtest
mv
Command Description
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 | -i, -f, etc. (options) |
source | Specifies the path and source filename to be moved. |
dest | Specifies the path and destination filename. |
Usage Example
mv file1 /userdata # Move file1 to the /userdata directory
rm
Command Description
The rm command is used to delete a file or directory.
Command Format
rm [ options ] file/directory
Parameter Description
Parameter | Description |
---|---|
options | -r, -f, etc. (options) |
file/directory | The name of the file or directory to be deleted. |
Usage Examples
rm test.txt # Delete the file test.txt
rm -r Luckfox # Delete the Luckfox directory and its contents
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