25 Useful shell Scripts Common command sharing _linux shell

Source: Internet
Author: User
Tags ssh

1. List all directory usage and sort by size.

Copy Code code as follows:
Ls|xargs Du-h|sort-rn
#不递归下级目录使用du-sh

2. View the file exclusion with the # switch and blank line, suitable for viewing the configuration file.
Copy Code code as follows:
Egrep-v "^#|^$" filename
Sed '/#.*$/d; /^ *$/d '

3. Delete spaces and blank lines.
Copy Code code as follows:
Sed '/^$/d ' filename #删除空行
Sed ' s///g ' filename
Sed ' s/[[:space:]]//g ' filename

4. Delete the comment after #.
Copy Code code as follows:
Sed-i ' s/#.*$//g ' filename

5. Kick out the logged in user, use Who to view terminal.
Copy Code code as follows:
Pkill-kill-t pts/0

6. Delete empty files.
Copy Code code as follows:
Find/-type f-size 0-exec rm-rf {} \;

7. Find process pid and kill.
Copy Code code as follows:
Pgrep Nginx|xargs Kill
Pidof Nginx|xargs Kill

8. Get the current IP address, powerful awk, a command to fix.
Copy Code code as follows:
Ifconfig |awk-f "[]+| [:] "' nr==2 {print $} '

9. Text Mode view wtmp log
Copy Code code as follows:
Utmpdump/var/log/wtmp

10. List processes in memory size
Copy Code code as follows:
PS aux--sort=rss |sort-k 6-rn

11. Simple Web server lists the current directory files, Port 8000:
Copy Code code as follows:
Python-m Simplehttpserver

12. Modify the user password by pipeline Input method:
Copy Code code as follows:
echo "Password" |passwd–stdin root

12. Generate SSH certificate and copy to remote server:
Copy Code code as follows:
Ssh-keygen-y-F ~/.ssh/id_rsa && cat ~/.ssh/id_rsa.pub | SSH root@host "cat->> ~/.ssh/authorized_keys"

13.shell to create a new folder and enter, add the following BASHRC:
Copy Code code as follows:
MKCD () {
mkdir $
CD $
}

14. Fast backup files to another server via SSH:
Copy Code code as follows:
Tar zcvf-back/| SSH root@www.jb51.net tar xzf--c/root/back/

15. Download the whole station with wget:
Copy Code code as follows:
Wget-r-p-np-k http://www.jb51.net
#r递归 p Download All files np not load superior K convert relative link

16.Kill entire process tree:
Copy Code code as follows:
Pstree-ap 10277|grep-op ' [0-9]{4,6} ' |xargs kill-9

17. Generate Random characters:
Copy Code code as follows:
Cat/dev/urandom | Tr-dc ' a-za-z0-9 ' | Fold-w 32 | Head-n 1

18. Use awk to export the last column of data that is not empty:
Copy Code code as follows:
Awk-f "|" ' {if ($NF!= "") Print $NF} '

19. Find data that is larger than several numbers per row:
Copy Code code as follows:
Awk-f ' {if (nf>6) print $} '

20. Get HTML page text content:
Copy Code code as follows:
Lynx-dump www.jb51.net #包含页面的URL
W3m-no-cookie-dump www.jb51.net
Links-dump www.jb51.net #对中文内容支持不好

21. Port redirection:
Copy Code code as follows:
Socat Tcp4-listen:1234,reuseaddr,fork, tcp4:www.baidu.com:80

22. Insert before or after line:
Copy Code code as follows:
Sed ' p;s/^.*$/--------/' file
awk ' {print $0;print '-------'} ' file

23. Insert at the beginning or end of the line:
Copy Code code as follows:
Sed ' s/^/new/g ' file
Sed ' s/$/new/g ' file

24. Verbatim line-changing:
Copy Code code as follows:
Awk-f "" ' {for (i=1;i<=nf;i++) print $i} '

25. A large number of files in the directory deletion:
Copy Code code as follows:
ls | Xargs RM

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.