In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use special characters under shell in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First, quickly switch between directories.
In the text mode of the Linux operating system, switching between directories requires commands. Obviously, this is not as convenient as using mouse clicks in a graphical interface. However, some special characters are still available in shell. With the help of these characters, the system administrator can easily switch between different directories.
If the system administrator wants to return to the user's home directory from any directory, is there any faster way than to go back the same way? In fact, a very convenient way to get back to the user's home directory is through a special character ~. Typically, when an administrator creates a user, a folder with that user's name is created in the / home directory of the system. The directory where this folder is located is the user's home directory. When users do not know which directory they are in and need to quickly return to their home directory, they can use this command to achieve: cd ~. In other words, the special symbol ~ represents the user's home directory in the operating system. Because different users have different home directories, you can use this home directory to quickly return to your own home directory.
In the process of directory switching, there are two more important special characters, namely. With; with. Symbols. Among them "." Represents the current directory. This symbol is very important and needs to be used in many places. For example, when defining the PATH environment variable, you need to add this to the path. Number, which represents the current directory. In addition, if the system administrator wants to run a script file in the current directory, such as setup.sh. You can use. / setup.sh instead of an absolute path. Among them, this one. The symbol represents the current directory. It can also be used in the cd command. Number, such as cd. / setup, means to go to the next subdirectory of the current directory, setpup. So this special symbol.. (period in English) is very useful when changing directories. Another special character is ".." (double-dot in English) represents a higher-level directory in the system. If the administrator uses the cd command to define a directory and then wants to go back to the previous directory, he can use cd.. Command to implement. Note that there is a space between the cd command and the period.
If the system administrator is proficient in the application of these special characters, especially the ~ symbol that represents the user's home directory, it can help the administrator to quickly switch to the directory he wants. For example, in the Linux client, the folder of the user's mailbox is often saved under the user's home directory. For example, the user may use thunderbird's mail client. There is a disadvantage of using this client, that is, when the Linux system shuts down illegally, the meeting generates a lock file in the mail directory. The client cannot be opened. It's just that the system administrator needs to change to the mail folder under the user's home directory and delete the lock file. The relatively simple method at this time is to directly use the ~ symbol to navigate to the user's home directory, and then go to the mail directory, find the file and delete it. This level-by-level entry is much faster.
Save the execution result to a file.
In daily operations, users may need to save the execution results of some commands to a file. If you need to save the running results of some programs to the daily file, and so on. The author now takes a simple example as an example. The Ps command is used to display the current system running program command. For example, on the command line, enter the ps command to display all the programs currently running on the system (note that programs and processes are two different experiences). It displays the process number of the application, terminal information, run time, program name, and so on. Now what if the administrator wants to save this information to a specific file?
At this time, the system administrator can use a special character >, such as ps > ps.txt, after the command to save the execution result of the command ps to the text file ps.txt. If the target file does not exist, the system creates the file and saves the information. If the target file exists, the system overwrites the previous file. Therefore, special attention should be paid when using the special symbol > to create a file. Because the target file exists, the system will not give the user any prompt, so delete the target file and create a new file with the same name. For this reason, carelessness may result in the loss of files. But it also has a twin brother, the > symbol. This special symbol is similar to the > symbol, which also saves the execution results to a file. But there is a big difference between this symbol and the > symbol. If the target file already exists, the execution effect of the two symbols is different. If the > symbol is used, the system will automatically overwrite it. If the > > symbol is used, the existing object file will not be overwritten in the system, but only the relevant records will be appended to the target file. If the target file does not exist, the target file is still created automatically.
Because the > > symbol does not automatically overwrite the target file, it just appends the record to the target file. There are other special uses for this symbol, such as the ability to merge different files. For example, there are even files listjpg.txt and listgif.txt. In these two files, the image names of jpg and gif are mainly saved. Now the user wants to merge the two files, or append the records in listjpg.txt to the listgif.txt file. What should be done at this time? You can do this with the command cat listjpg.txt > > listgif.txt. This command means that the contents of the file listjpg.txt are read out using the cat command and then appended to the listgif.txt file. After this command is executed, the two files are merged. But the original documents still exist. If it is not necessary, you need to delete it manually.
Because the functions of the two special symbols are similar, but the implementation details are different. For this reason, the system administrator needs to grasp the differences and choose the appropriate way to deal with them in the actual work. For example, in some installers, you need to save the information during the installation process in a log file. However, during the next reinstallation, the system administrator may prefer to delete the original log first. Because it will increase the amount of reading. At this point, it is reasonable to use the special symbol > because it automatically deletes the log file.
Third, run the program in the background.
Sometimes, the system administrator may want some programs to run in the background of the system. If the Linux operating system is managed as a server, it is a common way to run some long-running programs in the background. For example, administrators now use while loops to implement a function. Every 5 minutes to determine whether an application system has an error log file. If any, ask Ma Shan to send this error log file to the system administrator. After writing this script, the way to * is to put the script to run in the background. Because this program needs to run for a long time, it may interfere with other programs if it is run in the foreground. And it is also easy to be turned off by mistake. Therefore, it is more reasonable to put it in the background. So how do you prevent scripts from running in the background in the Linux operating system? Another special character & is also needed at this time. For example, the name of this script is errorlog.sh. Then the administrator only needs to run the command errorlog.sh &, and the system will run the program in the background.
Fourth, explain the escape characters of special characters.
In Microsoft operating system, some characters cannot be used as file names. Such as * the meter character symbol, which is used as a wildcard, cannot appear in a file name or directory name. However, there is no such limitation in the Linux operating system. For example, test*.txt is a legal name in the Linux operating system. Isn't the * symbol used as a wildcard in the Linux operating system? In fact, in the Linux operating system, this * symbol also has a special meaning, it is also a code wildcard. If you use the ls text* command, you can list the names of all files or directories that have begun with text. So why can this * meter symbol be used as a file name in the Linux operating system? This is all the escape character\ in its role.
If users now want to add a * meter character to the file name, they can use the command vi text\ * .txt to do so. Adding a transfer character\ before the wildcard * meter character tells the system to treat the * character as a normal character. In fact, the important role of this escape character is reflected in the deletion of the file. For example, there are several files in a directory, chap*.txt,chap1.txt,chap2.txt. Chap* represents the catalogue of a book, while others represent the chapters of the book. If the system administrator wants to delete the directory file now and uses the rm chap*.txt command, what are the consequences? At this point, all files starting with chap will be deleted. In other words, not only the catalog file, but also the content file of the chapter has been deleted. Because at this time, the operating system treats the * symbol as a wildcard. If you want to delete the directory file in, the correct command should write: rm chap\ * .txt. This command tells the system that the * symbol is a normal symbol, not a wildcard. At this point, the directory file will be deleted without affecting other files.
Although in the Linux operating system, escaping characters allows users to use some special characters to name files. However, for the convenience of management in the future, the author still suggests that we should not use special symbols to name. Otherwise, irreparable losses will occur if you are not careful. As in the case above, if the * wildcard is used as the name of the file, it is easy to delete all the relevant files when deleting.
Thank you for reading! This is the end of this article on "how to use special characters under shell in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.