Linux Basics
1. Introduction
Most users are already familiar with the graphical interface of Windows for their daily tasks, and Core3566 can also be operated through its graphical interface for basic applications. However, there are still many operations that cannot be done through the interface, such as system configurations. Almost all programs can be run by invoking commands through the command line. Therefore, a good understanding of commonly used Linux commands plays a significant role in getting started with Core3566 development. This chapter will introduce some commonly used Linux commands for Core3566 development to facilitate its usage.
2. Terminal Introduction
Open the built-in terminal in Core3566 by clicking on "System Tools" -> "LXTerminal" located at the bottom left corner of the desktop, or connect via SSH software. The default prompt in Core3566 terminal is as follows:
linaro@linaro-alip:~$
- "linaro" represents the current username/login name.
- "linaro-alip" represents the hostname.
- "~" indicates that the current user's directory is /home/linaro.
- The "$" character indicates that the user is currently logged in as a regular user.
- The "#" character indicates that the user is logged in as the root user.
Core3566 defaults to logging in as the regular "linaro" user. To switch to the root user, enter the following command in the terminal:
sudo su root # Switch from a regular user to the root user
su linaro # Switch from the root user to a regular user
3. Disk Management
3.1 Linux Directory Structure
In Windows, each partition forms a tree structure, so there are as many tree structures as there are partitions. In Linux, however, there is only one tree structure, and all files, partitions, and directories exist within this structure. In this structure, the top-level directory is the root directory, and all other directories, files, and partitions are created under the root directory. Common directories include:
- /bin: Contains binary executable files related to the Core3566 system, including those required to run the graphical interface.
- /boot: Boot directory that contains the Linux kernel and other software packages used to boot Core3566.
- /dev: Device directory. In the Linux system, all devices are treated as files, and this directory contains all the devices. For example, the first SATA hard drive or USB drive will be recognized as the sda file.
- /etc: System management and configuration files.
- /home: User directory. All user data, except for the root user, is stored in this directory.
- /lib: Location for basic system dynamic linking library files.
- /media: Mount point for removable storage devices such as USB drives, optical drives, etc.
- /root: Home directory for the system administrator, also known as the superuser.
3.2 Viewing File Directories
The
ls
command is used to display the contents of the specified working directory (list files and subdirectories in the current working directory).ls -a # Display all files and directories (including hidden files starting with .)
ls -l # Display detailed information such as file type, permissions, owner, file size
ls -lh # Display file sizes in a human-readable format, such as 4KThe pwd command immediately shows the absolute path name of the current working directory.
linaro@linaro-alip:~$ pwd
/home/linaro
linaro@linaro-alip:~$
3.3 Changing Directories
The cd command is used to change the current working directory.
cd # Return to the user's home directory
cd .. # Go back to the parent directory
cd /home/linaro # Enter the /home/lianro directory
cd ../.. # Go two levels up from the current directory
cd ~ # Enter the user's home directory
3.4 Viewing Disk Space
The df command is used to display statistics of file system disk usage on the Linux system.
df -h
4. File Management
4.1 Creating Files and Directories
The "touch" command is used to modify the time attributes of a file or directory, including the access time and modification time. If the file does not exist, the system will create a new file.
touch file.txt
The "mkdir" command is used to create a directory. In the working directory, create a subdirectory named "luckfox":
mkdir luckfox
Create a directory named "luckfox/test" in the working directory:
mkdir -p luckfox/test
- If the "luckfox" directory does not exist, create it. (Note: In this example, if the "-p" option is not used and the "luckfox" directory does not exist, an error will occur.)
4.2 Copying and Moving
The "cp" command is mainly used to copy files or directories. Use the "cp" command to copy all files in the current directory "test/" to a new directory "newtest", enter the following command:
sudo cp –r test/ newtest
The "mv" command is used to rename files or directories or move files or directories to another location. Use the "mv" command to copy the file "file1" in the current directory "test/" to the new directory "/home/linaro", enter the following command:
sudo mv file1 /home/lianro
4.3 Deletion
The "rm" command is used to delete a file or directory. To delete a file, simply use the "rm" command:
sudo rm test.txt
If deleting a directory, the "-r" option must be used:
sudo rm -r homework
4.4 Permission Settings
The "chmod" command is used to control the permissions of a file for users.
The file permissions in Linux/Unix are divided into three levels: owner, group, and other users.
In the terminal below, the detailed file information in the Linux root directory is displayed. The most important column is the first column, which describes the permissions of the file and directory in detail, while the third and fourth columns show which user or group the file and directory belong to.
linaro@linaro-alip:~$ ls -lh /
total 4.3M
drwxr-xr-x 2 root root 4.0K Jun 13 09:53 bin
drwxr-xr-x 3 root root 4.0K Jun 14 20:27 boot
lrwxrwxrwx 1 root root 8 Jun 13 09:56 data -> userdata
drwxr-xr-x 13 root root 4.4K Jun 17 15:48 dev
drwxrwxr-x 111 root root 4.0K Jun 17 16:08 etc
drwxr-xr-x 3 root root 4.0K May 9 10:29 home
drwxr-xr-x 15 root root 4.0K May 9 10:27 lib
drwx------ 2 root root 16K Jun 13 09:56 lost+found
-rw-r--r-- 1 root root 4.2M May 9 10:29 md5sum.txt
drwxr-xr-x 2 root root 4.0K May 9 10:13 media
drwxr-xr-x 4 root root 4.0K Jun 13 09:56 mnt
drwxr-xr-x 3 root root 1.0K Jun 17 15:48 oem
drwxr-xr-x 2 root root 4.0K May 9 10:13 opt
dr-xr-xr-x 249 root root 0 Jan 1 1970 proc
drwxr-xr-x 17 root root 4.0K May 9 10:32 rockchip-test
drwx------ 5 root root 4.0K Jun 17 15:48 root
drwxr-xr-x 23 root root 780 Jun 17 16:08 run
drwxr-xr-x 2 root root 4.0K Jun 13 09:49 sbin
lrwxrwxrwx 1 root root 10 Jun 13 09:56 sdcard -> mnt/sdcard
drwxr-xr-x 2 root root 4.0K May 9 10:13 srv
dr-xr-xr-x 14 root root 0 Jun 17 15:48 sys
drwxr-xr-x 4 root root 4.0K May 9 10:37 system
drwxrwxrwt 14 root root 440 Jun 17 16:08 tmp
lrwxrwxrwx 1 root root 9 Jun 13 09:56 udisk -> mnt/udisk
drwxr-xr-x 4 root root 4.0K Jun 17 15:48 userdata
drwxr-xr-x 10 linaro linaro 4.0K Jun 17 15:48 usr
drwxr-xr-x 11 root root 4.0K Feb 14 2019 var
drwxr-xr-x 3 root root 4.0K May 9 10:32 vendor
linaro@linaro-alip:~$- The file attributes in Linux can be divided into three types: read-only (r), write (w), and executable (x). However, the file attributes are displayed in 10 slots. This is because, in addition to the first slot indicating the directory, the other three groups of slots, each consisting of three slots, represent the permissions of the file owner, the permissions within the same group, and the permissions for other users. In the first column, if it displays "d," it means it is a directory. If it is a symbolic link, it is represented by "l" in this column. If it is a device file, it is represented by "c."
- The first "rwx" slot: "- rwx --- ---" represents the permissions of the file owner.
- The second "rwx" slot: "- --- rwx ---" represents the permissions of users within the same group.
- The third "rwx" slot: "- --- --- rwx" represents the permissions for other users.
- "rwx rwx rwx" indicates that any user can read, write, and execute the file.
- "rw- --- ---" indicates that only the file owner has read and write permissions, but not execute permission.
- "rw- rw- rw-" indicates that all users have read and write permissions.
Symbol mode.
who(User Type)
who User Type u user g group o others a all operator(Symbol Mode)
Operator Explanation + Adds permissions for the specified user type - Removes permissions for the specified user type = Sets the specified user type's permissions, resetting all permissions for that user type permission (symbol pattern table)
Symbol Mode Permission Explanation r Read Grants read permission w Write Grants write permission x Execute Grants execute (or access) permission X Special Execution Permission Only when the file is a directory or when other user types have execute permission, the file permission will be set as executable. s setuid/gid When the file is executed, it sets the setuid or setgid permissions based on the user type specified by the "who" parameter. t Sticky Bit Sets the sticky bit, which can only be set by the superuser. Only the file owner (u) can use this bit. 实例
sudo chmod a+r file # Add read permissions to all users of file
sudo chmod a-x file # Add read permissions to all users of file
sudo chmod a+rw file # Add read and write permissions to all users of file
sudo chmod +rwx file # Add read, write, and execute permissions to all users of file
sudo chmod u=rw,go= file # Set read and write permissions for the owner of the file, and clear all permissions of the user group and other users on the file (space means no permissions)
sudo chmod -R u+r,go-r LUCKFOX # Add read permissions to the user for all files in the directory LUCKFOX and its subdirectory hierarchy, and remove read permissions for the user group and other users
Numeric Mode
Octal Syntax
Octal Permission rwx Binary 7 Read + Write + Exec rwx 111 6 Read + Write rw- 110 5 Read + Exec r-x 101 4 Read Only r-- 100 3 Write + Exec -wx 011 2 Write Only -w- 010 1 Exec Only --x 001 0 None --- 000 For example: Interpretation of 765:
- Numeric permission for the owner: Sum of the numeric permission bits for the owner. For example, rwx, which is 4 + 2 + 1, should be 7.
- Numeric permission for the group: Sum of the numeric permission bit for the group. For example, rw-, which is 4 + 2 + 0, should be 6.
- Numeric permission for others: Sum of the numeric permission bits for others. For example, r-x, which is 4 + 0 + 1, should be 5.
Common Numeric Permissions
- 400 - r-- --- --- Owner can read, others have no permissions.
- 644 - rw- r-- r-- Owner can read and write, others can only read.
- 660 - rw- rw- --- Owner and group can read and write, others have no permissions.
- 664 - rw- rw- r-- All can read, owner and group can edit.
- 700 - rwx --- --- Owner can read, write, and execute, others have no permissions.
- 744 - rwx r-- r-- All can read, only owner can edit and execute.
- 755 - rwx r-x r-x All can read and execute, only owner can edit.
- 777 - rwx rwx rwx All can read, write, and execute (not recommended).
Example
sudo chmod 664 file # Adds read permission for all users, owner and group can edit permissions
4.5 Extracting and Compressing
The
zip
command is used to compress files.zip
is a widely used compression program, and the compressed files have the.zip
extension.# Compress all files and folders in the directory /home/linaro/luckfox to luckfox.zip in the current directory
sudo zip -q -r luckfox.zip /home/pi/luckfox
# Compress files in the current directory
sudo zip -q -r luckfox.zip *- The
-r
option is used for recursive processing, which includes all files and subdirectories in the specified directory. The-q
option suppresses the display of the command execution process.
- The
The
unzip
command is used to extract files from a zip archive.unzip
is the decompression program for zip files.sudo unzip luckfox.zip -d user/
- The
-d
option specifies the directory where the extracted files will be stored.
- The
The
tar
command is a tool program used to create and restore backup files. It can also be used to extract files from a backup file.# Compress files
sudo tar -cvzf luckfox.tar.gz *
# Extract files
sudo tar -xvzf luckfox.tar.gz
4.5 Searching
The
find
command is used to search for files in a specified directory. Any strings preceding the parameters are considered directory names to search. If no parameters are set, thefind
command will search for subdirectories and files in the current directory and display the results.Searching by filename. For example, to find the file
luckfox.py
:sudo find -name luckfox.py
Searching by the depth of nested files.
# Searching for .conf files in the /etc directory with a maximum depth of 1
sudo find /etc/ -maxdepth 1 -name *.conf
# Searching for .conf files in the subdirectories of /etc, excluding the /etc directory
sudo find /etc/ -mindepth 2 -name *.confListing all files in the current directory and its subdirectories that have been modified within the last 20 days:
sudo find . -ctime -20
Finding files in the current directory that have read and write permissions for the owner, and read permissions for the group and others:
sudo find . -type f -perm 644 -exec ls -l {} \;
Finding all empty regular files in the system and displaying their full paths:
sudo find / -type f -size 0 -exec ls -l {} \;
The
cat
command is used to display the contents of a file. It can be used to display the contents of multiple files as well.Displaying the contents of the file
test.txt
in the terminal:cat test.txt
Displaying the contents of all
.txt
files in the current directory:cat *.txt
The
grep
command is part of the trio of Linux text processing tools, along withsed
andawk
.grep
is used to search for specific patterns within files and can list combinations of special characters.Searching for files in a directory that contain the string "sys":
ls | grep sys
Searching for lines in the file
config.txt
that start with "arm" using regular expressions:grep ^arm config.txt
Searching for files with the word "test" in their names that have the suffix "file" and printing the lines containing the string:
grep test *file
awk is a powerful text processing tool. Here, we will cover some basic and commonly used features.
Each line is separated by spaces or TAB, and items 1 and 4 in the text are output:
awk '{print $1,$4}' luckfox.txt
Output the rows whose first column is greater than 2:
awk '$1>2' luckfox.txt
Output the rows whose first column equals 2:
awk '$1==2' luckfox.txt
Output rows where the first column is greater than 2 and the third column is equal to 'SQL':
awk '$1>2 && $3=="SQL"' luckfox.txt
Output lines containing the keyword "Python":
awk '/Python/ ' luckfox.txt
5. Software Management
Most popular Linux distributions provide a centralized package management mechanism to help users search, install, and manage software. Software is typically stored in repositories, and the usage and management of software packages are referred to as package management. This section will introduce the basic methods and quick references for searching, installing, and upgrading packages on Ubuntu and Debian systems, helping you quickly understand the fundamental operations and techniques of different Linux package management.
5.1 Software Installation
Most package management systems are built on collections of package files. Package files typically consist of pre-compiled binary files, installation scripts, metadata, and a list of dependencies. In Debian-based systems, the package format is .deb files. To directly install a generated .deb package, you can use the
dpkg
command as follows:sudo dpkg -i filename.deb
Since the package manager uses a local database to store the list of available remote repositories, it's recommended to update this database before installing or upgrading packages. You can use the following command:
sudo apt-get update
Once the update is complete, you can install software by specifying its name using the following command:
sudo apt-get install XXX -y # -y option skips the installation prompt
5.2 Software Upgrades
Without a package management system, upgrading and keeping installed software on Linux up to date would be a massive undertaking. Administrators and users would have to manually track upstream software version changes and security alerts. A package management system allows you to keep software up to date with just a few commands. Here are the specifics:
sudo apt-get upgrade # Update installed software packages
sudo apt-get dist-upgrade # Upgrade the system to the latest version (use with caution)Both
upgrade
anddist-upgrade
are used to upgrade installed software packages on the system. However, theapt-get upgrade
command cannot install new packages or remove already installed packages from the system, whereasdist-upgrade
can install new packages or remove existing ones if needed.
5.3 Software Uninstallation
Since the package manager knows which files are provided by each package, uninstalling unnecessary software packages can leave you with a clean system. You can uninstall an installed software package using the following commands:
# Remove the package
sudo apt remove <package_name>
# Clean up unused dependencies and library files
sudo apt autoremove
# Remove the package and its configuration files
sudo apt purge <package_name>
6. System Management
It is not recommended to directly unplug the power cord to shut down Core3566. If you are booting from an SD card, unplugging the power cord directly can cause data loss or damage to the data on the SD card, as some data in the memory might not have been written to the SD card. This can result in an inability to start the system. Instead, you can use the following commands:
sudo shutdown -h now # Shutdown immediately
sudo shutdown -r now # Restart the computer
sudo shutdown -h 10 # Schedule a shutdown in 10 minutes
sudo reboot # Reboot (commonly used)Regardless of which command you use to shut down the system, you need root user privileges. If you are using a regular user like "linaro," you can use the
sudo
command to temporarily obtain root privileges.
7. Networking
The
ifconfig
command is used to view and configure network devices. It allows you to configure the network when there are changes in the network environment. Note that the configurations made using theifconfig
command will not persist after the network card or machine restarts. To make the above configuration information permanent, you need to modify the configuration file of the network card. Here are some examples:sudo ifconfig eth0 # View the wired network IP address
sudo ifconfig wlan0 # View the wireless network IP address