Linux: Chapter 5 after-school exercises and answers

Chapter 5 Linux Common Commands

Q1: What are the commonly used text content display commands? What is the difference?

  • The commands displayed in the text content are cat, more, less, head, tail
  • cat: Displays text files, and can also append the contents of several files to another file. If no file is specified, or file is "-", then standard input is read.
    • cat [options] [file]
    • [Example 5.1] Display the contents of the /etc/inittab file. [root@rhel ~]# cat /etc/inittab
    • [Example 5.2] Input the content of the textfile1 file into the textfile2 file after adding the line number. [root@rhel ~]# cat -n textfile1 > textfile2
    • [Example 5.3] Use the cat command to create mm.txt. [root@rhel ~]#cat >mm.txt<Hello >Linux >EOF //Enter the character EOF here, and it will automatically return to the shell prompt interface
  • more: display text files in pages, the most basic button is to press the space bar to display the content of the next page, and press the [b] key to return to display the content of the previous page.
    • more [options] [filename]
    • [Example 5.4] Display the contents of the /etc/services file in pages. [root@rhel ~]# more /etc/services
    • [Example 5.5] Display the contents of the testfile page by page. If there are more than two consecutive blank lines, it will be displayed as one blank line. [root@rhel ~]# more -s testfile
    • [Example 5.6] Display the contents of the testfile from the 20th line. [root@rhel ~]# more +20 testfile
    • [Example 5.7] Display the contents of the /etc/passwd file two lines at a time. [root@rhel ~]# more -2 /etc/passwd
  • less: Scroll back to display the text file
    • less [options] [filename]
    • [Example 5.8] Scroll back to display the contents of the /etc/services file. [root@rhel ~]# less /etc/services
  • head: Display the first few lines of the specified file, the default default setting is 10 lines
    • head [options] [files]
    • [Example 5.9] View the first 100 bytes of data content in the /etc/passwd file. [root@rhel ~]# head –c 100 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon :x:2:2:daemon:/sbin:/sbin/nol
    • [Example 5.10] View the data content of the first 3 lines of the /etc/passwd file. [root@rhel ~]# head -3 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon: x:2:2:daemon:/sbin:/sbin/nologin
  • tail: display the last few lines of the specified file, the default setting is 10 lines
    • tail [options] [filename]
    • [Example 5.11] View the data content of the 3 lines at the end of the /etc/passwd file. [root@rhel ~]# tail -3 /etc/passwd news:x:9:13:News server user:/etc/news:/bin/bash distcache:x:94:94:Distcache:/:/sbin/ nologin tcpdump:x:72:72::/:/sbin/nologin
    • [Example 5.12] View the data content of the 100 bytes at the end of the file /etc/passwd. [root@rhel ~]# tail -c 100 /etc/passwd er:/etc/news:/bin/bash distcache:x:94:94:Distcache:/:/sbin/nologin tcpdump:x:72:72: :/:/sbin/nologin

Q2: What are the commonly used text processing commands? What is the difference?

  • Text processing commands include sort, uniq, cut, comm, diff
  • sort: Sort the content in the text and output the result to standard output
    • sort [options] [file]
    • [Example 5.13] Sort the data of the file textfile1 and display it on the screen. [root@rhel ~]# sort textfile1
    • [Example 5.14] Read the content of textfile1, sort the file in reverse order and display it on the screen. [root@rhel ~]# sort -r textfile1
  • uniq: remove duplicate lines from a file
    • uniq [options] [file]
    • [Example 5.15] View the repeated data content in the file file3. [root@rhel ~]# cat file3 aaa aaa bbb [root@rhel ~]# uniq -d file3 aaa //The content of the repeated line data in the file3 file is aaa
    • [Example 5.16] View the non-repeated data content in the file file3. [root@rhel ~]# uniq -u file3 bbb //The content of the non-repeated row data in the file3 file is bbb
  • cut: Display selected bytes, characters or fields from each line of the file
    • cut [options] [file]
    • [Example 5.17] Display the user login name and full user name fields in the file /etc/passwd, which are the first and fifth fields, separated by colons. [root@rhel ~]# cut -f 1,5 -d: /etc/passwd root:root bin:bin daemon:daemon adm:adm lp:lp  …
  • comm: Compare two sorted files line by line
    • comm [options] [file1] [file2]

  • diff: Compares two text files line by line, listing their differences. Can not be sorted in advance
    • diff [options] [file1] [file2]
    • [Example 5.20] Compare file1 and file2 and list their differences. [root@rhel ~]# cat file1 a aa [root@rhel ~]# cat file2 a bb //View the contents of files file1 and file2 [root@rhel ~]# diff file1 file2 2c2 < aa --- > bb //You can see that the difference between file1 and file2 is the aa and bb in the second line

Q3: What command can be used to display the kernel version of the current computer?

  • uname -r

Q4: What command can be used to clear computer screen information?

  • clear

Q5: What command can be used to sort the file contents in reverse order?

  • sort -r [filename]

Replenish:

File and command lookup

  • Commands include grep, find, locate
  • grep: Find strings that match the conditions in the file
    • grep [options] [search pattern] [filename]
    • [Example 5.21] Search for the matching character "test file" in the file kkk. [root@rhel ~]# grep 'test file' kkk
    • [Example 5.22] Display the data content of all rows containing "test" in all files starting with d. [root@rhel ~]# grep 'test' d*
    • [Example 5.23] Find the content of the line starting with b in the /root/aa file. [root@rhel ~]#grep ^b /root/aa
    • [Example 5.24] Find the content of the line that does not start with b in the /root/aa file. [root@rhel ~]# grep -v ^b /root/aa
    • [Example 5.25] Find the content of the line ending with le in the /root/kkk file. [root@rhel ~]# grep le$ /root/kkk
    • [Example 5.26] Find the sshd process information. [root@rhel ~]# ps –ef|grep sshd
  • find: List qualified files in the file system
    • find [path] [options]
    • [Example 5.27] Find the boot menu configuration file grub.cfg in the /boot directory. [root@rhel ~]# find /boot -name grub.cfg
    • [Example 5.28] Find all files with ".conf" extension in the / directory. [root@rhel ~]# find / -name '*.conf'
    • [Example 5.29] List all files changed in the last 20 days under the current directory and its subdirectories. [root@rhel ~]# find . -ctime -20
  • locate: find the file in the database
    • locate [options] [template style]
    • [Example 5.34] Find the httpd.conf file. [root@rhel ~]# locate httpd.conf
    • [Example 5.35] It shows that several httpd.conf files are found. [root@rhel~]# locate -c httpd.conf

system information display

  • The commands are uname, hostname, free, du
  • uname: Display information about the computer and operating system
    • uname [options]
    • [Example 5.36] Display the kernel release number of the operating system. [root@rhel ~]# uname -r 3.10.0-327.el7.x86_64
    • [Example 5.37] Display the computer hardware architecture name. [root@rhel ~]# uname -m X86_64
    • [Example 5.38] Display all information of the operating system. [root@rhel ~]# uname -a Linux rhel 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
  • hostname: Display or modify the computer hostname
    • hostname [options] [hostname|-F <file>] set hostname
    • hostname [options] display formatted hostname
  • free: view memory information
    • free [options]
    • [Example 5.41] View the physical memory and swap partition usage of the system. [root@rhel ~]# free
    • [Example 5.42] View the physical memory and swap partition usage of the system in MB. [root@rhel ~]# free -m
    • [Example 5.43] Display the system's physical memory plus the total capacity of the swap partition. [root@rhel ~]# free -t
  • du: Display the disk usage of a directory or file
    • du [options] [file|dir]
    • [Example 5.44] Display the disk usage of the file /etc/inittab. [root@rhel ~]# du /etc/inittab
    • [Example 5.45] Display the disk usage of the /root directory. [root@rhel ~]# du –s /root
    • [Example 5.46] Display the disk usage of the /root directory in MB. [root@rhel ~]# du –sh /root

date and time

  • Commands include cal, date, hwclock
  • cal: display calendar information
    • cal [options] [[[day]month]year]
    • [Example 5.47] Display the calendar of this month. [root@rhel ~]# cal
    • 【Example 5.48】 Display the year 2001 calendar. [root@rhel ~]# cal 2001
    • [Example 5.49] Display the calendar of September 2007 AD. [root@rhel ~] # cal 9 2007
    • [Example 5.50] Display the calendar of this month with Monday as the first day of each week. [root@rhel ~]# cal -m
    • [Example 5.46] Display this year's calendar with the number of days since January 1. [root@rhel ~]# cal -jy
  • date: display and set system date and time
    • date [option] [display time format] (starts with +, followed by format)
    • [Example 5.52] Display the date and time on the current computer. [root@rhel ~]# date
    • [Example 5.53] Set the computer date and time to February 2, 2028 at 19:14. [root@rhel ~]# date 0202191428
    • [Example 5.54] Display the computer date and time in the specified format. [root@rhel ~]# date +'%r%a%d%h%y'
    • [Example 5.55] Set the computer time to 9:16 am. [root@rhel ~]# date -s 09:16:00
    • [Example 5.56] Set the computer time to April 14, 2024. [root@rhel ~]# date -s 240414

  • hwclock: view and set the hardware clock
    • hwclock [options]
    • [Example 5.57] Check the hardware time. [root@rhel ~]#hwclock
    • [Example 5.51] Update hardware time with system time. [root@rhel ~]# hwclock -w
    • [Example 5.52] Update system time with hardware time. [root@rhel ~]# hwclock -s

information exchange

  • These commands are echo, mesg, wall, write
  • echo: display text on the display
    • echo [options] [string]
    • [Example 5.60] Write a piece of information to the standard output. [root@rhel ~]# echo Hello Linux Hello Linux
    • [Example 5.61] Add the text "Hello Linux" to the new file notes. [root@rhel ~]# echo Hello Linux > notes [root@rhel ~]# cat notes Hello Linux //Check the file notes, you can see the content in the file is hello Linux
    • [Example 5.62] Display the value of the $HOME variable. [root@rhel ~]# echo $HOME /root
  • mesg: Allow or deny writing messages
    • mesg [options]
    • [Example 5.63] Display the current message permission settings. [root@rhel ~]# mesg is y //Allow all hosts on the local network to send messages to their own host
    • [Example 5.64] Only the root user is allowed to send messages to his own host. [root@rhel ~]# mesg n [root@rhel ~]# mesg is n
  • wall: send information to all logged-in users
    • wall [message]
    • [Example 5.66] Send the message "Please turn off the computer after get off work." to all users. [root@rhel ~]# wall 'Please turn off the computer after get off work' [root@rhel ~]# Broadcast message from root@rhel (pts/1) (Sun Jun 3 05:38:14 2012): Please turn off the computer after get off work
  • write: send a message to the user
    • write [user] [terminal name]
    • [Example 5.67] Send information from the tty2 terminal to the root user on the tty3 terminal. [root@rhel ~]# write root tty3 hello //Input the message to be sent on the tty2 terminal. After inputting, if you want to exit the sending state, press the key combination [Ctrl+c][root@rhel ~]# Message from root@rhel on tty2 at 05:39 ... hello EOF //On the terminal tty3, the user root will receive the above information. To finish, press [Ctrl+c] key combination

other commands

  • clear: Clear computer screen information
  • uptime: Displays the time the system has been running

Guess you like

Origin blog.csdn.net/qq_46119575/article/details/131335592