Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the common advanced parts of Linux Shell

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand the advanced parts of commonly used Linux Shell". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the advanced parts of commonly used Linux Shell".

How to calculate the number of files and directories in the current directory

# ls-l * | grep "^ -" | wc-l-to count files # ls-l * | grep "^ d" | wc-l-to count dir

How to list only subdirectories?

Ls-F | grep / $

Or

Alias sub = "ls-F | grep / $" (linux)

Ls-l | grep "^ d"

Or

Ls-lL | grep "^ d" (Solaris)

How to fetch specific line contents from a file

If you only want to see the first five lines of the file, you can use the head command

Such as:

Head-5 / etc/passwd

If you want to see the last 10 lines of the file, you can use the tail command

Such as:

Tail-10 / etc/passwd

Do you know how to look at the middle paragraph of the file? You can use the sed command

Such as:

Sed-n'5 10p'/ etc/passwd

This way you can only look at lines 5 through 10 of the file.

How to find a file with a specific string

For example, find that the current directory contains "the string you want find..." File for string:

$find. -type f-exec grep "the string you want find..." {};-print

How to list directory trees

The following short shell program can list directory trees, taking full advantage of sed's powerful pattern matching capabilities.

The directory tree is in the following form:

. `--shellp`-- updates `--wu-ftpd-2.4 |`-- doc | | `--examples |`-- src | | `--config | |`-- makefiles | `--support | |`-- makefiles | | `--man |`-- util script is as follows: #! / bin/sh # dtree: Usage: dtree [any directory] dir=$ {1VM.} (cd $dir) Pwd) find $dir-type d-print | sort-f | sed-e "s, ^ $1" s, "- e" / ^ $/ d "- e" s, [^ /] * / ([^ /] *) $, `- 1, "- e" s, [^ /] * /, |, g "

How to extract specific column contents from a file

We often encounter certain fields of a file that need to take out sub-fields. For example, / etc/password separates the fields by ":". This can be done through the cut command. For example, if we want to save the system account name to a specific file, we can:

Cut-d:-f 1 / etc/passwd >; / tmp/users

-d is used to define the delimiter, which defaults to the tab key, and-f indicates which field needs to be obtained.

Of course, you can also get specific characters in each line of the file through cut, such as:

Cut-c3-5 / etc/passwd

This is the third to fifth characters of each line in the output / etc/passwd file.

The-c and-f parameters can be followed by the following sub-parameters:

The Nth character or field

N-from the first character or field to the end of the file

Nmurm from the Nth to the M character or field

-M from the first to the nth character or field

Realizing batch encryption in vim

There is still no space in the password, no matter, as long as it can be encrypted, use it like this first.

#! / bin/bash # Encrypt file with vim if (test $#-lt 2) then echo Usage: decrypt password filename else vim-e-s-c ": set key=$1"-c': wq' $2 echo "$2 encrypted." Fi [weeder@SMTH weeder] $for file in *. Txt; do encrypt test $file; done test2.txt encrypted. Test4.txt encrypted. Test9.txt encrypted. Kick.txt encrypted. Echo "$2 encrypted." Fi [weeder@SMTH weeder] $for file in *. Txt; do encrypt test $file; done test2.txt encrypted. Test4.txt encrypted. Test9.txt encrypted. Kick.txt encrypted. Too_old.txt encrypted. Too_old_again.txt encrypted. Bg5.txt encrypted. [weeder@SMTH weeder] $

The meaning of specific shell variables such as $@

In the actual writing of shell scripts, there are some special variables that are useful:

$# number of parameters passed to the script

$* displays all parameters passed to the script in a single string. Unlike location variables, this option can have more than 9 parameters

The ID number of the current process that the $script is running

$! The process ID number of the last process running in the background

$@ is the same as $#, but use it in quotation marks and return each parameter in quotation marks

$- displays the current options used by shell, with the same function as the set command

$? Displays the exit status of the last command. 0 indicates that there is no error, and any other value indicates an error.

How to direct the execution result of the program to the screen and file at the same time

Program_name | tee logfile

In this way, the display during the execution of the program is recorded to logfile and displayed to standard output (screen).

How to use sendmail to send messages to all users of the system

First create an alias in the aliases file:

Alluser:: include:/etc/mail/allusers

And execute newaliases to make it effective, and then list all users in / etc/mail/allusers, you can use the following command:

Awk-F:'$3 >; 100 {print $1}'/ etc/passwd >; / etc/mail/allusers

How to find the related library files of a command

When making your own distribution, you often need to determine which library files are needed for a command to ensure that the specified command can be run reliably in a separate system.

This can be achieved through the ldd command in the Linux environment and executed in the console:

Ldd / bin/ls

You can get a list of related library files for the / bin/ls command.

How to use the host command to get more information

Host can be used to query domain names, but it can get more information. Host-t mx linux.com can query the MX record of the Linux.com, as well as the name of the Host that processes the Mail. Host-l linux.com returns all domain names registered under linux.com. Host-a linux.com displays all domain name information for this host.

How to stop multiple processes in a terminal

The following is the script:

Echo "system current user"

Echo "-" who | awk'{print $2} 'echo "-"

Echo "enter the terminal number to kill the terminal:"

Read $TTY kill-9 ${K} = `ps-t $TTY | grep [0-9] | awk'{print $1} '`Thank you for reading. The above is the content of "how to understand the advanced parts of common Linux Shell". After the study of this article, I believe you have a deeper understanding of how to understand the advanced parts of commonly used Linux Shell, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report