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
On the Omni3576, you can access the terminal through a serial port or via SSH. For local display logins, press
Ctrl+Alt+Tto 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
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
sdafor 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.
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 unknownroot@luckfox:/home/luckfox# cat /sys/block/mmcblk1/queue/hw_sector_size
512To 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)
- Access:
4.2 inode Considerations
The number of inodes is finite. If inodes are exhausted, new files or directories cannot be created, even if disk space is available.
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
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.
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
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
| 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 -cjvf luckfox.tar.bz2 directory # Compress using bzip2
tar -cJvf luckfox.tar.xz directory # Compress using xzExtract 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
| 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
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
| Parameter | Description |
|---|---|
| options | Options like -h (help), -y (auto-confirm), and -q (suppress output). |
| command | The action to perform (e.g., install, remove). |
| package | The name of the package to manage. |
Usage Examples
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 updateAfter updating, you can install software by its name using the following command:
sudo apt-get install XXX -y # -y skips the confirmation promptWithout 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
upgradeanddist-upgradeupgrade installed packages. However, theapt-get upgradecommand cannot install new packages or remove existing ones, whiledist-upgradecan install new packages or remove existing ones if necessary.
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
Install a package:
sudo dpkg -i package.debUninstall a package:
sudo dpkg -r package_name
shutdown & reboot
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)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
sudocommand.
6. Rockchip Common Commands
Check the NPU driver version:
cat /sys/kernel/debug/rknpu/versionCheck the unique chip serial number:
cat /proc/cpuinfo | grep SerialCheck the CPU temperature.
echo "$(cat /sys/class/thermal/thermal_zone0/temp) / 1000" | bc