The directory representation in the text interface is "/www/xxx/yyy/zzz/...", which is called the directory structure or "path." If the current directory is "xxx," the current directory is called the "working directory." The parent directory of "xxx" is "www" and is called the "parent directory," while the next-level directory of "xxx" is "yyy" and is called the "subdirectory." The parent and subdirectories are separated by "/", and they are relative names. The leftmost part, which is the top-level "/", represents the "root directory." If there is no parent directory, it is called the "root directory."
For example, if I have created two user accounts, "aaa" and "bbb," the actual directory structure would be as shown in the diagram below:
When logged in with the "aaa" account in the text interface, the working directory would be "/home/aaa" (highlighted in red). Its parent directory is "home," and "Desktop" and "Documents" are both subdirectories of "aaa."
However, in the text-based interface of Linux, the default environment usually only displays the working directory without the path. To find out the current working directory, you can use the command pwd (print working directory), as shown below:
Syntax:pwd [-otpiton] | ||
Command name/Function/Command user | Ooptions | Function |
pwd/ print working directory/ /Any |
-P (uppercase)) | If the working directory is a link, it will display the actual working directory, not the link itself |
[aaa@localhost ~]$ cd ~ ←Enter your home directory [aaa@localhost ~]$ pwd ←Display the complete working directory for the account "aaa" /home/aaa [aaa@localhost ~]$ cd /var/mail ←Enter the directory "/var/mail" [aaa@localhost mail]$ pwd ←Enter the directory "/var/mail" /var/mail [aaa@localhost mail]$ pwd -P ←List the actual working directory for non-symbolic links /var/spool/mail |
Syntax:tree [-otpiton][--option] [path] | ||
Command name/Function/Command user | Options | Function |
tree/ display directory tree/ Any |
-a | Expand and list all files and directories. Include hidden files starting with "." |
-C | Use colored listings (different colors for directories and files) | |
-d | Only list directories and not files | |
-F | Adds special characters to indicate different file types "/": Directory "=": socket file "*": Execution file "|" : FIFOfile "@": link file |
|
-g | List the groups you belong to | |
-p | List permissions | |
-u | List owner | |
-I pattern | Do not list files and directories matching PATTERN+ wildered characters | |
-P pattern | List files and directories matching PATTERN + wildcard combination | |
-l | If the directory is a link file , directly list its original directory | |
-L level | List only a few levels of directory order | |
-x | Only list the current file system (for example, the current file-system is ext3 , but some directories may be ext2 or FAT, etc. will be excluded) |
|
--help | Displays the command's built-in help and usage information |
$ tree -L 1 / ←lists the first-level directories starting from the root directory ("/") / |-- bin |-- boot |-- dev |-- etc |-- home |-- lib |-- lost+found |-- media . . . |
$ tree ← Without specifying a directory, it will list the files and subdirectories in the current working directory . |-- Dektop |-- Documents |-- Download |-- Music |-- Pictures |-- Public |-- lost+found |
$ tree -d /home ←it will list the subdirectories within the "/home" directory, and only directories will be displayed (excluding files) /home |-- aaa | |-- Desktop | |-- Documents | |-- Music | |-- Pictures | |-- Public | `-- Videos `-- bbb [error opeing dir] ← indicates that the "bbb" directory could not be accessed, likely due to insufficient permissions |
[aaa@localhost ~]$ cd /bin [aaa@localhost bin]$ ← The prompt has changed, indicating that you have entered the /bin directory. [aaa@localhost bin]$ pwd ←Check with the pwd command /bin [aaa@localhost bin]$ cd /root ←Let's take a look at the root home directory -bash: cd: /root: Permission denied ←You can't access it because you don't have sufficient permissions |
$ cp -v /usr/share/dict/linux.words /tmp ←Copy the file to the directory /tmp (using absolute path) |
[aaa@localhost /]$ cd /usr/share/dict ← Use an absolute path to enter the directory "/usr/share/dict" first [aaa@localhost dict]$ pwd ←Check with the pwd command /usr/share/dict [aaa@localhost dict]$ cd .. ←Use a relative path ".." to go to the parent directory "/usr/share" (one level up) [aaa@localhost share]$ pwd ←Check with pwd again /usr/share [aaa@localhost share]$ cd dict ← Use a relative path to enter the directory "/usr/share/dict" again [aaa@localhost dict]$ cd ../../.. ←← You can keep going back to the previous directory! (Go back three times to the root directory) [aaa@localhost /]$ pwd ← pCheck with pwd again / [aaa@localhost /]$ cd /usr/share ←Use an absolute path to enter the directory "/usr/share" first [aaa@localhost share]$ cd ../sbin ←用Use a relative path to go back one level and enter the subdirectory "sbin" [aaa@localhost sbin]$ pwd ← Check the current location /usr/sbin |
$ mv -v ../../fileA ../ ←Move the file "fileA" two levels up to its parent directory (one level up) $ rm ../* ←Delete all files in the parent directory |
$ cp -v /usr/share/dict/linux.words ~/← Copy the file "linux.words" to the home directory $ cd ~ ←Go back to your own home directory |
# cp ~bbb/fileA ~aaa/ ←複← Copy the file from the "bbb" home directory to the "aaa" home directory # cd ~aaa ←Enter the home directory of the "aaa" account # ls ~bbb/ ←List files in the "bbb" home directory |
$ cd /bin ←Go to the /bin directory $ cd /etc ←Go to the /etc directory $ cd - ← Go back to the previous directory (/bin) /bin |
$ cp ./file /tmp ←Copy a file from the current working directory to /tmp $ ./backup.sh ←Execute a file in the current working directory |
$ mkdir ./-dir ←Create a directory named "-dir" $ cd ./-dir ←進Enter the subdirectory "-dir" $ cd .. ← Go back to the previous directory $ rmdir ./-dir ←Delete the directory "-dir" |
Syntax:mkdir [-otpiton][--option] directory | ||
Command name/Function/Command user | Options | Function |
mkdir/ make directory / Any |
-m | Sets the permission mode for the created directory |
-p | Create nested directories (several layers of directories can be created at a time) | |
--verbose | Show created directory | |
--help | Displays the command's built-in help and usage information |
[aaa@localhost ~]$ cd /tmp ←Switch to the /tmp directory [aaa@localhost tmp]$ mkdir dir_test ←Create a directory /tmp/dir_test |
$ mkdir /tmp/dir_test ←Create the directory /tmp/dir_test |
$ cd /tmp ←Switch to the /tmp directory $ mkdir level_1/level_2 $ mkdir: canot create directory 'level_1/level_2': No such file or directory ←Why can't we create the directory? By default, directories need to be created one level at a time and cannot create multiple levels at once. $ mkdir level_1 ←Create the directory /tmp/level_1 $ cd level_1 ←Enter /tmp/level_1 $ mkdir level_2 ← Once inside, create the next level directory /tmp/level_1/level_2 |
$ mkdir -p --verbose /tmp/111/222/333/444 mkdir: create directory 'tmp/111' mkdir: create directory 'tmp/111/222' mkdir: create directory 'tmp/111/222/333' mkdir: create directory 'tmp/111/222/333/444' |
$ mkdir -m 770 /tmp/jason_dir ←Create a directory and set permissions at the same time |
rmidir is a considerate command that prioritizes caution over accidental deletion. While the rm command can also be used to remove directories, it is more powerful and has the potential to cause unintended harm if used incorrectly. Therefore, it is recommended to use the dedicated rmdir command for removing directories. Here is how it is used:
Syntax:rmdir [-otpiton][--option] directory | ||
Command name/Function/Command user | Options | Function |
rmdir/ remove directory / Any |
-p | Remove the nested directory (you can remove several layers of directories at a time, but the directory must be empty) |
--verbose | Show removed directories | |
--help | Displays the command's built-in help and usage information |
$ mkdir -p --verbose /tmp/111/222/333/444 ←Create multiple levels of directories $ rmdir /tmp/111 ←Remove the directory /tmp/111 $ rmdir: /tmp/111: Directory not empty ←The directory contains subdirectories or files and cannot be removed $ rmdir /tmp/111/222/333/444 ←Even if all directories are empty, they need to be removed one level at a time (start from the outermost) $ rmdir /tmp/111/222/333 $ rmdir /tmp/111/222 $ rmdir /tmp/111 |
$ rmdir -p --verbose /tmp/111/222/333/444 ←If the directories are empty, you can remove multiple levels at once $ rmdir dir1 dir2 dir3 ←Remove directories dir1, dir2, dir3 |
$ file /bin/su ←Check what type of file /bin/su is /bin/ls: ELF 64-bit LSB executable(....)← 64-bit ELF executable (Executable and Linkable Format) $ file /usr/share/man/man1/gawk.1.gz /usr/share/man/man1/gawk.1.gz: gzip compressed data, from Unix, max compression ←Gzip-compressed file $ file /etc/fstab /etc/fstab: ASCII text ←ASCII plain text file $ file /tmp /tmp: sticky directory ← A directory with Sticky bit special permission $ file /etc/grub.conf /etc/grub.conf: symbolic link to `../boot/grub/grub.conf' ←Symbolic link |
The basic function of ls (list) is to display files. However, in Linux, "everything is a file." Therefore, Linux files contain a lot of information such as time, permissions, owners, and executability. That's why there are so many options available. But don't worry, there are only a few commonly used options, and if you need to use more obscure options, you can refer to the man page or use ls --help. It's like the complicated roads in Taipei. I'm terrible with directions, but it doesn't affect my daily life because I only take a few routes every day. If I happen to go to an unfamiliar place, I can just use GPS.
Here are some commonly used functionalities and options of ls:Syntax:ls [-otpiton][--option] [path/file/directory] | ||
Command name/Function/Command user | Options | Function |
ls/ list files/ Any |
-a | List all files and directories. Include hidden files starting with "." |
-A | Same as -a but does not include the current directory symbol "." and its parent directory symbol ".." | |
-B | Excludes backup files (files ending with "~") | |
-d | Only list directories, without listing the files within the directories | |
-F | Appends identifying characters for different file types: "/": denotes directories "=": denotes socket files "*": denotes executable files "|": denotes FIFO files |
|
-l (小寫 L) | Lists detailed file information, including ownership , permissions, date, size, etc | |
-g | Same as "ls -l" but does not include the owner information | |
-G | Works in conjunction with "ls -l" but does not display the group information | |
-h | Works in conjunction with "ls -l" to display file sizes in human-readable format using units such as KiB (kibibytes), MiB (mebibytes), and GiB (gibibytes). | |
-c | Works in conjunction with "ls -l" to list files based on their ctime | |
-u | Works in conjunction with "ls -l" to list files based on their atime |
|
-t | Sorts files by time, usually used in conjunction with "ls -l" (default sorting is based on #mtime) | |
-i | Lists files and their corresponding inode numbers | |
-n | Similar to "ls -l" but displays the GID and UID as numeric values instead of their corresponding names | |
-m | Displays files in a horizontal comma-separated list format | |
-o | Does not display the group information (same as "ls -lG") | |
-p | If the entry is a directory, it will be displayed with a trailing "/" character | |
-R | Lists files recursively, including all files within directories | |
-X | Lists files sorted by extension, displaying them in alphabetical order based on their file extensions. | |
--help | Displays the command's built-in help and usage information | |
--time==ctime | Same as "ls -c" | |
--time==atime | Same as "ls -u" | |
--color=never | Lists files in a black and white format, disabling colorized output |
$ ls /home ←Lists the files and directories in the "/home" directory aaa bbb john lost+found $ ls -p ~/ ← Uses the "-p" or "-F" option to distinguish between files and directories (directories will have a trailing "/") Desktop/ Download/ Pictures/ Public/ Videos/ Documents/ Music/ project Publish $ ls -a ← Files starting with "." are hidden files. Using the "-a" option will show these hidden files . Download .ICEauthority Templates .. .esd_auth .local .tomboy .bash_history .gconf .metacity .tomboy.log . . . |
$ ls -l ←Lists files in long format | |||||||
drwxr-xy-x | 2 | aaa | aaa | 4096 | 2011-09-07 | 11:44 | Desktop |
drwxr-xy-x | 2 | aaa | aaa | 4096 | 2011-09-07 | 11:44 | Documents |
drwxr-xy-x | 2 | aaa | aaa | 4096 | 2011-09-07 | 11:44 | Music |
drwxr-xy-x | 2 | aaa | aaa | 4096 | 2011-09-07 | 11:44 | Pictures |
drwxr-xy-x | 2 | aaa | aaa | 4096 | 2011-09-07 | 11:44 | Public |
↑ | ↑ | ↑ | ↑ | ↑ | ↑ | ↑ | ↑ |
file types and Permissions |
# of links |
owner | group | size | date | time | Files or directories |
$ ls -al ←With the -a option, it lists hidden files and also displays the attributes of the current directory and its parent directory. total 300 drwx------ 1 aaa aaa 4096 2011-08-02 11:23 . ←Displays the permissions, owner, and group of the current directory. drwxr-xr-x 26 root root 4096 2010-08-04 01:23 .. ←Displays the permissions, owner, and group of the parent directory -rw------- 1 aaa aaa 5849 2011-08-02 22:20 .bash_history ← Hidden file starting with "." -rw-r--r-- 1 aaa aaa 5849 2007-03-10 22:20 .bash_logout drwxr-xr-x 2 aaa aaa 4096 2010-08-04 01:23 Desktop . . . |
$ ls -d */ ← List the directories in the current working directory Desktop/ Download/ Pictures/ Templates/ Documents/ Music/ Public/ Videos/ $ ls -ld /home ← List the detailed information of a directory drwxr-xr-x 6 root root 4096 2011-08-02 11:23 /home |
The possible file types in Linux are as follows:
atime (access time): The atime of a file is updated whenever it is read or executed (since executing requires reading). Updating atime for every file access can consume hardware resources, so some older machines, slower laptops, or high-performance computers may disable atime updates. You can disable atime updates when mounting a file system using the command mount -o noatime or by modifying /etc/fstab.
ctime (change time): ctime is updated whenever any change occurs to the file, including changes to permissions, ownership, or file content.
mtime (modify time): mtime represents the time when the file content was last modified. It is similar to the file time in Windows, and the time displayed by ls -l corresponds to the mtime. Among the three timestamps, mtime is the most important.
The table below shows the impact of different file actions on the timestamps in Linux:
File Action | atime | ctime | mtime |
---|---|---|---|
Read or execute | Updated | - | - |
Change permissions | Updated | Updated | - |
Change ownership | Updated | Updated | - |
Change file content | Updated | - | Updated |
$ ls -lc ←List files with their ctime (change time) $ ls -l --time=ctime ←Same as above (ls -lc) $ ls -ltc ←List files with their ctime and sort by ctime $ ls -lu ←List files with their atime (access time) $ ls -ltu ←List files with their atime and sort by atime $ ls -l ←List files with their mtime (modify time) $ ls -lt ←List files with their mtime and sort by mtime |
$ alias ←Entering the "alias" command without parameters lists the commands with aliases alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias ls='ls --color=tty' alias vi='vim' |
$ alias md='mkdir' $ alias rd='rmdir' $ alias cls='clear' $ alias copy='cp' $ alias del='rm' |
In the above example, entering the DOS command "md" is equivalent to entering the Linux command mkdir. Both "md" and mkdir can be used interchangeably and have the same functionality.
A more complex example involves commands with options. For instance, the alias rmdir='rmdir -p' adds the "-p" option. But if you want to execute rmdir without the "-p" option, you can temporarily disable the alias by adding the escape character "\" before the command, like \rmdir.
$ unalias md ←Removes the alias "md" $ unalias -a ←Removes all aliases |
$ stat /etc/fstab File: `/etc/fstab' Size: 532 Blocks: 16 IO Block: 4096 regular file Device: 802h/2050d Inode: 846151 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2011-08-11 01:22:13.000000000 +0800 ←atime Modify: 2011-08-03 23:52:41.000000000 +0800 ←mtime Change: 2011-08-04 00:52:16.000000000 +0800 ←ctime |
Syntax:cat [-otpiton][--option] file | ||
Command name/Function/Command user | Options | Function |
cat / file read / Any |
-A | Same as -vET |
-b | Adding Line Numbers to Non-Blank Lines | |
-e | Same as -vE | |
-E | Display "LF" (ASCII = 0AHEX) as "$" (Linux newline is displayed as "$") | |
-n | Add line number |
|
-s | Squeezing Multiple Blank Lines into One | |
-t | Same as -vT | |
-T | Horizontal positioning "TAB" (ASCII = 09HEX) is displayed with "^I" | |
-v | Control characters, except for Line Feed (LF) and "TAB," are displayed as "^", while ASCII characters > 0x7F(HEX) are displayed as "M-" | |
--help | Displays the command's built-in help and usage information |
Before providing practical examples, let's first understand the concept of "control characters" in ASCII (ASCII # 0-31), using Windows' Notepad as an example.
In Notepad, if you input a Tab character (ASCII=09HEX), it will display as eight spaces. Although the visual result is the same as typing eight spaces on the keyboard, the underlying meaning is entirely different. Each application software may interpret ASCII=09 (HEX) differently.
Therefore, control characters in ASCII are primarily used to control output behavior, such as newline, horizontal tab, backspace, etc. They may not necessarily have visible characters displayed on the screen, and different applications may interpret control characters differently.
In most cases, when using the cat command to view file contents, no additional options are needed. The purpose of adding options is usually to understand which characters in the text file are true spaces and which are results of control characters. Another use case is when different platforms or applications store files with "control characters" definitions that may conflict with Linux's tty (terminal) control characters. Adding options allows you to force the display of control characters using special symbols, thus avoiding garbled output resulting from conflicts.$ cat /etc/hosts.allow ←Read the file /etc/hosts.allow # # hosts.allow This file contains access rules which are used to # allow or deny connections to network services that # either use the tcp_wrappers library or that have been # started through a tcp_wrappers-enabled xinetd. # # See 'man 5 hosts_options' and 'man 5 hosts_access' # for information on rule syntax. # See 'man tcpd' for information on tcp_wrappers # $ cat -TE /etc/hosts.allow ←Display "TAB" (ASCII=09HEX) as "^I" and "LF" (ASCII=0AHEX) as "$" #$ # hosts.allow^IThis file contains access rules which are used to$ #^I^Iallow or deny connections to network services that$ #^I^Ieither use the tcp_wrappers library or that have been$ #^I^Istarted through a tcp_wrappers-enabled xinetd.$ #$ 以下略 |
Syntax:head [-otpiton][--option] file | ||
Command name/Function/Command user | Options | Function |
head/ Read the first few lines of the file/ Any |
-c[#bkm] | The output is not in units of lines, but in units of bytes. The available options are: "#":output #l bytes ("#"is a number,if omitted, it represents 1) "[#] b" b for Block (represents 512 bytes) "[#]k" k for Ki "[#]m" m for Mi |
-n# | Only read the first # lines of the file ("#" is a number) |
|
+# | Skip the first # lines ("#" is a number) | |
--help | Displays the command's built-in help and usage information |
$ head text_file ←Read the first 10 lines of the file "text_file" $ head -c50 text_file ←read the first 50 bytes of the file $ head +5 -n20 text_file ←read the file, start reading from the 5th line Read 20 lines $ head -cb text_file ←read the file "text_file", the first 512 bytes $ head -c2k text_file ←read the file "text_file", the first 2k byte |
語法:tail [-otpiton][--option] file | ||
Command name/Function/Command user | Options | Function |
tail/ Read the last few lines of the file/ Any |
-c[#bkm] | The usage is the same ashead |
-n# | Only read the last # lines of the file ("#" is a number) |
|
-f | Continue to read the file until the file disappears (press <Ctrl+C> to end) | |
-F | Read files continuously, even if they disappear (press <Ctrl+C> to end) | |
--help | Displays the command's built-in help and usage information |
$ seq 1 100 | tail -n5 ←uses a pipeline to read the last 5 lines from the output of the seq command 96 97 98 99 100 |
As mentioned, the tail command is often used in conjunction with pipelines. One particular use case is the tail -f command, which is primarily used for reading continuously growing files, such as log files or files with append redirection as well as FIFO files. tail -f allows you to monitor changes in real-time.
When you use tail -f to read a file, it will continue reading the file until it is deleted or until you manually terminate the command by pressing Ctrl+C.
The main difference between tail -f and tail -F is that tail -F will continue attempting to read the file even if it has been deleted. If the file is recreated after being deleted, tail -F will resume reading it.
This distinction is useful in scenarios where log files or output redirection files are rotated or periodically deleted and recreated. By using tail -F, you can ensure that you continue monitoring the file even if it gets deleted and recreated.
In summary, while tail -f will stop reading a file once it's deleted, tail -F will persistently attempt to read the file, making it useful for cases where the file may be rotated or temporarily removed.
$ seq 1 100 | tail -n5 | tac ← Same as the previous example, but reverse read 100 99 98 97 96 $ tac file.txt ←Reverse read file |
As an octal dump tool, od is particularly useful for programmers and developers who need to analyze binary files, examine file structures, or interpret the data stored in non-text files. It provides a way to view the raw data in a file in octal format, which can be helpful for debugging or understanding the file's content at a low-level.
For most general users, the opportunity to use od may be limited. However, it's good to be aware of its existence and purpose as a powerful tool for developers when working with non-text files or performing low-level analysis.
Syntax::od [-otpiton][--option] file | ||
Command name/Function/Command user | Options | Function |
od/ octal output / Any |
-A[doxn] | The way of displaying the leftmost address, the available options are: "d" is decimal, "o" is octal, "x" is hexadecimal, "n" is not displaying the address |
-t[cdfoux][#] | Display format, the available options are: "c" ASCII character "d" is decimal "f" is floating point "o" is octal " u" is unsigned decimal "x" is hexadecimal "#" is To display several bytes as a group ("#" is a number), the default is 8 |
|
-j# | Skip the first # bytes ("#" is a number) | |
-N# | The number of bytes read ("#" is a number) | |
-w# | How many bytes are displayed in a column ("#" is a number) | |
-v | o not omit the same column (by default, if the two columns are the same, only "*" will be displayed for the same column) | |
--help | Displays the command's built-in help and usage information |
$ od -j50 -N20 -Ad -tx2 /bin/mkdir ←reads 20 bytes starting from the 50th byte in the file /bin/mkdir. It displays the addresses in decimal, and the output format is two-byte hexadecimal representation. The output will look something like this: 0000050 ←Address 001f 0006 0000 0034 0000 8034 0804 8034 0000066 0804 0100 0000070 $ od -tc -Ax /etc/hosts.allow ←Output in ASCII, address in hexadecimal 000000 # \n # h o s t s . a l l o w \t 000010 T h i s f i l e c o n t a i 000020 n s a c c e s s r u l e s 000030 w h i c h a r e u s e d t . . . |
Most software interprets the newline character in ASCII-recorded text files as the end of a line, causing the subsequent text to be displayed on the next line. In Unix/Linux, for the convenience of writing and displaying, "\n" is commonly used to represent the input newline character, while "$" is used to display the newline character (e.g., the commands cat -E or sed -n l display the newline character as "$").
However, not all platforms use a single-byte "LF" to represent a newline. In DOS/Windows, a newline consists of two bytes: "CR" (carriage return) followed by "LF" (line feed).
Therefore, when opening Unix/Linux text files with Windows' Notepad, the format may appear distorted due to the inability to interpret the Linux newline character.
Different platforms use the following newline characters:
Syntax:unix2dos [-otpiton][--option] file [backup file] | ||
Command name/Function/Command user | Options | Function |
unix2dos/ UNIX to DOS format/ Any |
-k | Do not changemtime |
-n [ori-file new-file] | Keep the original old file and save the new file | |
-q | Quiet mode (the conversion process will not be displayed on the screen) | |
--help | Displays the command's built-in help and usage information |
$ unix2dos -n FileA dos.txt ←Convert UNIX format "FileA" to DOS format file "dos.txt" $ unix2dos FileA ←If there is no option, the new file (DOS format) will convert the original file (Linux format) cover |
The usage of dos2unix is similar to unix2dos.
In Linux commands, when dealing with strings, it is recommended to use single quotation marks (') to enclose the string. Although double quotation marks (") or even no quotation marks at all can work, it is important to develop the habit of using the correct syntax. Not using single quotation marks around the string can sometimes lead to unexpected behavior. Additionally, in the BASH shell, double quotation marks (") serve a different purpose.
Here are some examples of using the echo command:
:$ echo 'Hello World' ←The first and second examples are the most conventional Hello World $ echo "Hello World" Hello World $ echo Hello World Hello World |
If the string contains single quotation marks ('), you should use double quotation marks (" to enclose the string. Conversely, if the string contains double quotation marks ("), you should use single quotation marks (') to enclose the string.
Here are some examples:$ echo I'm a student ←Incorrect usage since the (') marks are not paired >Ctrl+C ←Press <Ctrl+C> to stop the current operation or input another single quotation mark to make the quotation marks paired $ echo "I'm a student" ←Use (") when the string contains single quotation marks I'm a student $ echo "I'm Lovin' You" ←(') or (") can be nested I'm Lovin' You $ echo I saw a "saw" I saw a saw ←The output looks strange since the (") disappeared; the following example shows the correct usage $ echo 'I saw a "saw" ' ←It is important to develop the habit of using correct syntax. When dealing with strings, the basic syntax is to use single quotation marks (') to enclose the string. I saw a "saw" |
Syntax::echo [-otpiton][--option] string | ||
Command name/Function/Command user | Options | Function |
echo/ display text/ Any |
-n | No newline at the end of the string |
-e[ \abfntx0] | Extended display format, the available options are: \a issue "beep" (ASCII = 07HEX ) \b "backspace" (ASCII = 08 HEX ) ( Backspace key) \f "page change" (ASCII = 0C HEX ) ( The position of the cursor remains unchanged, but it is displayed on the lower line) \n"Line feed" (ASCII = 0A HEX ) ( Enter key) \t"Horizontal positioning" (ASCII = 09 HEX ) ( Tab key) \x## : ## is ten Hexadecimal ASCII code \0### :### Octal ASCII code (\0 is the zero ) |
|
--help | Displays the command's built-in help and usage information |
$ echo -e 'I\x27m a student, I saw a \x22saw\x22' ←(')ASCII code is 27HEX ,(")ASCII code is 22HEX I'm a student, I saw a "saw" |
$ echo $'\x41\x42' ←List ASCII=41(hex) & 42(hex) if char AB |
$ echo -e Linux is\f a PC OS Linux isf a PC OS ←Is the output weird? "\f" (horizontal positioning) doesn't work? Sometimes if you don't use single quotation marks (') to enclose the string in pairs, There will be an error $ echo -e 'Linux is\f a PC OS' ←use single quotes (') to enclose the string in pairs and it will be OK Linux is a PC OS $ echo -e 'Linux is\n a PC OS' ←Use newline to use echo to display more than one line of strings Linux is a PC OS $ echo -e 'Linux is\t a PC OS' Linux is a PC OS $ echo -e 'Linux is\x9 a PC OS' ←Directly input the ASCII code"09HEX" instead of horizontal positioning Linux is a PC OS $ echo -e '\xe' ←Accidentally use the wrong ASCII control code, it may cause garbled characters! [▒▒▒@┌⎺␌▒┌⎺⎽├ ·]$ ←When garbled characters are generated, remember to input the reset screen instruction "reset" |
語法:more [-otpiton][+ option][+/ option] file | ||
Command name/Function/Command user | Options | Function |
more/ paging file viewer / Any |
-# | "#" is a number, representing the number of columns to be displayed on the screen |
+# | "#" is a number, representing the output from the first line | |
+/<string> | Search for a string in an archive and output from there |
$ more +3 /etc/services ←Start reading the file "/etc/services" from the third line # Network services, Internet style # # Note that it is presently the policy of IANA to assign a single well-known # port number for both TCP and UDP; hence, most entries here have two entries . . . tcpmux 1/udp # TCP port service multiplexer --More--(0%) ←The current number of pages accounts for the percentage of the entire file |
Here are examples of using less to read a file:
$ less /usr/share/dict/linux.words $ cat /usr/share/dict/linux.words | less ← This command achieves the same result as the previous example |
Please note that less provides more advanced features and functionality compared to more. If you're interested, you can press H or h while running less to access the help page and learn more about its operations and available commands.
Overall, less offers a more flexible and powerful way to navigate and read through large text files compared to more.