NO17 The second test: Return to the last directory and ls -lrt to see files in reverse order--delete logs 7 days ago--view log updates--record line numbers

Question 2: How to go back to the last directory without the cd /ildboy command:

If the current directory is:

[root@localhost oldboy]# pwd
/oldboy
now needs to enter the /tmp directory to operate, the command to execute is as follows:
[root@localhost oldboy]# cd /tmp
[root@localhost tmp]# pwd
/tmp
After the operation is completed I want to quickly return to the directory I entered last time, that is, the /oldboy directory. How to do it? You cannot use the cd /oldboy command.

Solution:
[root@localhost tmp]# cd -  (go back to last directory)
/oldboy

The principle of this question:
[root@localhost oldboy]# env|grep -i oldpwd (the system has a variable to automatically follow the record)
OLDPWD=/tmp 
[root@localhost oldboy]# cd - 
/tmp
[root@localhost tmp]# env| grep -i oldpwd
OLDPWD=/oldboy

 

About the cd command:
[root@localhost tmp]# cd . #current directory
[root@localhost tmp]# cd .. #superior directory
[root@localhost /]# cd ~ #home directory
[root@localhost ~]# cd - #last directory
/

 


Question 3: There are many files in a directory (multiple screens when viewing with ls). I want to view the most recently updated files as quickly as possible. How to view them?

Answer:
[root@localhost ~]# ls -lrt /etc


ls command:
-r reverse order, reverse order
-t order by modification time

 


Question 4: I won't talk about it for now

 

Question 5: It is known that the access logs of the apache service are recorded in the local directory /app/logs of the server on a daily basis. Due to the shortage of disk space, the access logs are only required to be kept for 7 days. How can I solve this problem?
Please give a solution or configuration or processing command.
(Hint: You can start from the apache service configuration, or you can start from the generated log)

Answer:
[root@localhost logs]# find /app/logs/ -type f -mtime +7 (check the file first)
Method 1:
[root@localhost logs]# find /app/logs/ -type f -mtime +7 |xargs rm -f
Method 2:
[root@localhost logs]# find /app/logs/ -type f -mtime +7 -exec rm -f {} \;
Method 3:
[root@localhost logs]# rm -f 'find /app/logs/ -type f -mtime +7'


·find finds time-related parameters:
-atime n #n is a number, meaning the files that have been accessed [within one day] before n days.
-ctime n #n is a number, meaning the files that have been changed in the state [within one day] before n days.
-mtime n #n is a number, meaning the files that have been modified [within one day] before n days.
-newer file #file is an existing file, which means that as long as the file is newer than the file, it will be listed

 


Question 6: When debugging system services, I want to view the update of the system log /var/log/messages in real time. How to do it?

Answer:
Method 1:
[root@localhost logs]# tail -f /var/log/messages
[root@localhost logs]# tailf /var/log/messages     (tailf and tail -f have the same effect)

[root@localhost logs]# tail -F /var/log/messages ( -F and -f are more than multiple retry functions, that is, the file does not exist, and will continue to try )

 


Question 7: How to print the line number and content of the configuration file nginx.conf?

Answer: nginx and apache are different web service software, they are of the same kind, just like men and women are human beings.

Create an environment:
[root@localhost /]# echo stu{01..20} |xargs -n 1 >nginx.conf
[root@localhost /]# cat nginx.conf
stu01
stu02
stu03
stu04
stu05
stu06
stu07
stu08
stu09
stu10
stu11
stu12
stu13
stu14
stu15
stu16
stu17
stu18
stu19
stu20

Method 1: Remember
[root@localhost /]# cat -n nginx.conf (the most common, remember!)


Method 2:
[root@localhost /]# nl nginx.conf (number lines, professional display line numbers are not commonly used) Blank lines do not record line numbers.
 

Method 3:
[root@localhost /]# grep -n . nginx.conf   (Display the line number for the filtered content, if you want to display the line number for all files, you have to filter all the content. "." represents any single character. This will not Record the line number on the blank line )
[root@localhost /]# grep -n ".*" nginx.conf  (add * will record the line number on the blank line)


Method 4: Remember
[root@localhost /]# vim nginx.conf (remember!)
Then enter set nu instead of line number, just set nonu

Method 5: Remember
[root@localhost /]# awk '{print NR,$0}' nginx.conf  (NR means line number, $0 means the entire line content)


Method 6: Remember
[root@localhost /]# sed = nginx.conf| sed 'N;s/\n/ /'


Method 7:
[root@localhost /]# less -N nginx.conf

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325298458&siteId=291194637