Skip to main content

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

  1. 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 sda for 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 MediumPartition Table
SDMMCmtdparts=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: 0x00002000 represents 8192 sectors, i.e., 8192 × 512 bytes = 4 MiB, used to store the U-Boot bootloader.
  • boot: 0x00006000 represents 24576 sectors, i.e., 24576 × 512 bytes = 12 MiB, used to store boot files.
  • rootfs: Starts at 0x00010000 and 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/rootfs is the root file system (/)
  • devtmpfs and tmpfs are 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.
FeatureHard LinkSymbolic Link
Reference MethodPoints to the same inodePoints to a file path
Cross-File System SupportNot supportedSupported
Effect of Source DeletionLink remains validLink becomes invalid
Creation Commandln filename linknameln -s target linkname
Supported TargetsAny fileAny 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

ParameterDescriptionPath
directoryTarget directory to displayCan 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

ParameterDescriptionValue
directoryTarget 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

ParameterDescription
options-p Create parent directories if they do not exist
directoryName 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

ParameterDescription
optionsOptions like -r, -f, etc.
sourcePath and name of the file to copy.
destPath 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

ParameterDescription
optionsOptions like -i, -f, etc.
sourcePath and name of the file to move.
destPath 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

ParameterDescription
optionsOptions like -r, -f, etc.
file/directoryName 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

  1. File permissions in Linux/Unix are divided into three levels: file owner (Owner), group (Group), and other users (Other Users).

  2. 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 bin
  3. File 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.
  4. Symbolic Mode

    • who (user type)

      whoUser TypeDescription
      uuserFile owner
      ggroupUsers in the same group
      oothersAll other users
      aallAll users, equivalent to ugo
    • operator (symbolic mode table)

      OperatorDescription
      +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)

      ModeNameDescription
      rReadSet read permission
      wWriteSet write permission
      xExecuteSet execute permission
      XSpecial ExecuteSet execute permission only if the file is a directory or if other users have execute permission
      sSetuid/gidSet setuid or setgid permission when the file is executed, depending on the who parameter specified
      tSticky BitSet 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
  5. Numeric Mode

    • Octal Syntax

      #PermissionrwxBinary
      7Read + Write + Executerwx111
      6Read + Writerw-110
      5Read + Executer-x101
      4Readr--100
      3Write + Execute-wx011
      2Write-w-010
      1Execute--x001
      0None---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

ParameterDescription
options-xExtract files from an archive
-cCreate a new archive
-vShow detailed operation progess
-fSpecify the name of the archive file (must be the last option in the list)
-zUse gzip for compression or decompression
-jUse gzip for compression or decompression
-JUse xz for compression or decompression
file/directoryThe target file or directory to compress or extract

Usage Examples

  1. 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 gzip
  2. Extract 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

ParameterDescription
pathThe directory path to search. Can include multiple paths separated by spaces. Defaults to the current directory if unspecified.
match conditionSpecifies search criteria, such as filename, file type, or size.
actionSpecifies 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.py 
  • Search 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 2
  • List files updated in the last 20 days:

    sudo find . -ctime -20
  • Find 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

ParameterDescription
options-n: Display line numbers, adding a number to each line of output.
fileFile name.

Usage Examples

  1. Display the contents of the test.txt file in the terminal:

    cat test.txt
  2. List the contents of all .txt files in the current directory:

    cat *.txt 
  3. Append standard input to the end of the file:

    cat >> file
  4. Redirect standard input to file, overwriting its content:

    cat > file
  5. Combine the contents of file1 and file2 into file3:

    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

ParameterDescription
options-iIgnore case while matching.
-vPerform inverse matching, displaying non-matching lines.
-nDisplay the line number of matching lines.
-rRecursively search files in subdirectories.
-lDisplay only the names of matching files.
-cDisplay only the count of matching lines.
patternThe string or regular expression to search for.
filesFile(s) to search, allowing multiple files.

Usage Examples

  1. Search for files containing the string sys in the current directory:

    ls | grep sys
  2. Search config.txt for lines starting with arm using a regular expression:

    grep ^arm config.txt
  3. Find and display lines containing test in files with names ending in file:

    grep test *file
  4. Recursively search the dir folder for lines matching the regular expression luckfox, 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

  1. 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

  1. Check the unique chip serial number:

    cat /proc/cpuinfo | grep Serial
  2. Check the CPU temperature.

    echo "$(cat /sys/class/thermal/thermal_zone0/temp) / 1000" | bc