Skip to main content

Linux Basics

1. Introduction

Most users are accustomed to the graphical interface of Windows. While the Omni3576’s basic daily applications can be managed through its graphical interface, certain operations—such as system configurations—must be performed outside of it. 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 Omni3576.


2. Terminal Overview

  1. On the Omni3576, you can access the terminal through a serial port or via SSH. For local display logins, press Ctrl+Alt+T to open the default terminal. The Omni3576's default terminal prompt looks like this:

    luckfox@luckfox:~$ 
    • luckfox: Current username or login name.
    • luckfox: Hostname.
    • ~: Indicates the current directory is /home/luckfox.
    • $: Denotes a normal user login.
    • #: Denotes a root user login
  2. Omni3576默认登录普通 luckfox 用户,想要切换 root 用户,在终端输入:

    sudo su root   # Switch from a regular user to the root user.
    su luckfox # Switch from the root user to a regular user.

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 Omni3576 system (including those needed for running the graphical interface).
  • /boot: Stores the Linux kernel and other packages required to boot the Omni3576.
  • /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.
  • /home: User directories; data for all users (except the root user) are stored here.
  • /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 inode and block Overview

inode is a metadata structure in the Linux file system that records a file's attributes (e.g., size, permissions, timestamps) but does not store the file name or data. Each file or directory is assigned a unique inode when created. Data is stored in blocks (the smallest unit of storage on a disk), which typically have a size of 4 KB.

  1. To check sector size:

    root@luckfox:/home/luckfox# fdisk -l /dev/mmcblk1
    Disk /dev/mmcblk1: 29.13 GiB, 31272730624 bytes, 61079552 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: 46680000-0000-454D-8000-121D00005F9A

    Device Start End Sectors Size Type
    /dev/mmcblk1p1 16384 24575 8192 4M unknown
    /dev/mmcblk1p2 24576 32767 8192 4M unknown
    /dev/mmcblk1p3 32768 163839 131072 64M unknown
    /dev/mmcblk1p4 163840 425983 262144 128M unknown
    /dev/mmcblk1p5 425984 491519 65536 32M unknown
    /dev/mmcblk1p6 491520 61079518 60587999 28.9G unknown
    root@luckfox:/home/luckfox# cat /sys/block/mmcblk1/queue/hw_sector_size
    512
  2. To view inode information:

    luckfox@luckfox:~$ tee 1.txt <<EOF
    > 1111
    > 1111
    > 1111
    > EOF

    luckfox@luckfox:~$ stat 1.txt
    File: 1.txt
    Size: 15 Blocks: 8 IO Block: 4096 regular file
    Device: 179,6 Inode: 95246 Links: 1
    Access: (0644/-rw-r--r--) Uid: ( 1000/ luckfox) Gid: ( 1000/ luckfox)
    Access: 2024-08-26 12:51:16.816002017 +0000
    Modify: 2024-08-26 12:51:35.924002026 +0000
    Change: 2024-08-26 12:51:35.924002026 +0000
    Birth: 2024-08-26 12:51:16.816002017 +0000
    • Access: (0644/-rw-r--r--): File permissions
    • Access: 2024-08-26 12:51:16.816002017 +00: Last access time of the file
    • Modify: 2024-08-26 12:51:35.924002026 +0000: Last modification time of the file content
    • Change: 2024-08-26 12:51:35.924002026 +0000: Last change time of the file's metadata (e.g., permissions or owner)
    • Birth: 2024-08-26 12:51:16.816002017 +0000: Creation time of the file (not supported by all file systems)

4.2 inode Considerations

  1. The number of inodes is finite. If inodes are exhausted, new files or directories cannot be created, even if disk space is available.

  2. Files with special characters in their names may be difficult to delete. Deleting such files by inode can resolve the issue:

    find ./* -inum <inode-number> -delete
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.

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

luckfox@luckfox:~$ df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/root ext4 29G 3.7G 24G 14% /
devtmpfs devtmpfs 1.9G 8.0K 1.9G 1% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 781M 1.5M 779M 1% /run
tmpfs tmpfs 5.0M 8.0K 5.0M 1% /run/lock
tmpfs tmpfs 391M 32K 391M 1% /run/user/1000

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 -cjvf luckfox.tar.bz2 directory # Compress using bzip2
    tar -cJvf luckfox.tar.xz directory # Compress using xz
  2. Extract Compressed Files

    tar -xzvf luckfox.tar.gz                              # Extract gzip-compressed files
    tar -xjvf luckfox.tar.bz2 # Extract bzip2-compressed files
    tar -xJvf luckfox.tar.xz # Extract xz-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

apt

Command Function

Popular Linux distributions provide centralized package management tools to search, install, and manage software stored in repositories. The apt command allows efficient management of software packages in Ubuntu and Debian-based systems.

Command Format

apt [options][command] [package ...]

Parameter Description

ParameterDescription
optionsOptions like -h (help), -y (auto-confirm), and -q (suppress output).
commandThe action to perform (e.g., install, remove).
packageThe name of the package to manage.

Usage Examples

  1. The local database stores the list of available remote package repositories. Therefore, it is recommended to update this database before installing or upgrading packages using the following command:

    sudo apt-get update
  2. After updating, you can install software by its name using the following command:

    sudo apt-get install XXX -y     # -y skips the confirmation prompt
  3. Without a package management system, keeping Linux-installed software up-to-date is a significant task. Administrators and users must manually track upstream software updates and security warnings. With a package manager, you can use just a few commands to ensure software remains current:

    sudo apt-get upgrade            # Updates installed packages
    sudo apt-get dist-upgrade # Upgrades the system to the latest version (use cautiously)
  • Both upgrade and dist-upgrade upgrade installed packages. However, the apt-get upgrade command cannot install new packages or remove existing ones, while dist-upgrade can install new packages or remove existing ones if necessary.
  1. Since the package manager tracks which files are provided by which packages, you can achieve a clean system after uninstalling unnecessary packages with the following commands:

    # Command to remove a package
    sudo apt remove <package_name>
    # Clean up unused dependencies and library files
    sudo apt autoremove
    # Remove a package and its configuration files
    sudo apt purge <package_name>

dpkg

Command Function

The Debian system package format is .deb. To install .deb packages directly, the dpkg command is used.

Usage Examples

  1. Install a package:

    sudo dpkg -i package.deb
  2. Uninstall a package:

    sudo dpkg -r package_name

shutdown & reboot

  1. It is not recommended to unplug the power cord directly for Omni3576. If you boot from a TF card, unplugging the power cord directly might cause data loss or corruption on the TF card as some data in memory may not have been written to the TF card. This could result in system startup failures. Use the following commands instead:

    sudo shutdown -h now         # Shut down immediately
    sudo shutdown -r now # Reboot the system
    sudo shutdown -h 10 # Shut down after 10 minutes
    sudo reboot # Reboot (commonly used)
  2. Regardless of the command used to shut down the system, root user privileges are required. If using a regular user account like "luckfox," you can temporarily gain root privileges by using the sudo command.

6. Rockchip Common Commands

  1. Check the NPU driver version:

    cat /sys/kernel/debug/rknpu/version
  2. Check the unique chip serial number:

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

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