[Turn] Share some shell usage and scripts that you may not know

Share some shell usage and scripts that you may not know, simple & powerful!

Before reading the following sections, the reader is strongly recommended to open a shell experiment, these are not common in shell textbooks :)

!$
!$ is a special environment variable, which represents the last string of the previous command. For example: you may do this:
$mkdir mydir
$mv mydir yourdir
$cd yourdir
can be changed to:
$mkdir mydir
$mv !$ yourdir
$cd !$
sudo !!
Execute the previous command as root.
Scenario example: For example, when installing packages with apt-get in Ubuntu, root is required, and we often forget to add sudo before apt-get. Every time you have to add sudo and then retype this line of commands, you can easily use sudo!!
(Chen Hao Note: Under the shell, sometimes you will enter a long command, you can use !xxx to repeat the most recent command, for example, if you have entered it before, vi /where/the/file/is, next time You can use !vi to retrieve the last most recent vi command.)
cd -
Go back to the last directory.
Scenario example: the current directory is /home/a, use cd ../b to switch to /home/b. At this time, you can easily switch back and forth between /home/a and /home/b by repeatedly executing the cd - command.
(Chen Hao's note: cd ~ is to go back to your own home directory, cd ~user is to enter a user's home directory)
'ALT+.' or '<ESC> .'
Hot-build alt+. or esc+. can repeat the last command line parameters.
^old^new
replaces part of the string in the previous command.
Scenario: echo "wanderful", actually want to output echo "wonderful". Just ^a^o and it's a great help for misspellings of very long commands. (Chen Hao Note: You can also use !!:gs/old/new)
du -s * | sort -n | tail
lists the 10 largest files in the current directory.
:w !sudo tee %
save a file in vi that only root can write
date -d@1234567890
time cut-off time
> file.txt
to create an empty file, which is shorter than touch.
mtr coolshell.cn
mtr command is better than traceroute.
Add a space before the command line, the command will not enter the history.
echo "ls -l" | at midnight
to run a certain command at a certain time.
curl -u user:pass -d status=”Tweeting from the shell” http://twitter.com/statuses/update.xml
update twitter by command line.
curl -u username –silent “https://mail.google.com/mail/feed/atom” | perl -ne 'print “\t” if /<name>/; print “$2\n” if /<( title|name)>(.*)<\/\1>/;'
Check your gmail unread mail
ps aux | sort -nk +4 | tail
list the top ten most memory consuming processes
man ascii
show ascii code surface.
Scenario: Do ​​you still need to google when you forget the ascii code table? Especially when the Chinese network is so "smooth", it is even more troublesome to apply the rules in GWF one more time, just use the local man ascii.
ctrl-x e
quickly launches your default editor (set by the variable $EDITOR).
netstat -tlnp
lists the port numbers that the native process is listening on. (Chen Hao's note: netstat -anop can display the process listening on this port number)
tail -f /path/to/file.log | sed '/^Finished: SUCCESS$/ q'
When Finished appears in file.log: Exit tail when SUCCESS occurs. This command is used to monitor and filter the log in real time to see if a certain record appears.
ssh user@server bash < /path/to/local/script.sh
runs a script on the remote machine. The biggest advantage of this command is that there is no need to copy the script to the remote machine.
ssh user@host cat /path/to/remotefile | diff /path/to/localfile -
compares a remote file with a local file
net rpc shutdown -I ipAddressOfWindowsPC -U username%password
remotely shuts down a Windows machine
screen -d - m -S some_name ping my_router
runs a non-terminating program in the background, and you can check its status at any time. The -d -m parameter starts the "detached" mode, and -S specifies the identity of a session. An identified session can be "mounted" again with the -R command. See screen usage man screen for more details.
wget --random-wait -r -p -e robots=off -U mozilla http://www.example.com
downloads the entire www.example.com site. (Note: Don't go too far, most websites have anti-crawling functions:))
curl ifconfig.me
When your machine is on the internal network, you can use this command to check the IP of the external network.
convert input.png -gravity NorthWest -background transparent -extent 720×200 output.png
Change the size of the picture
lsof -i
Check the activity status of the local network service in real time.
vim scp://username@host//path/to/somefile
vim a remote file
python -m SimpleHTTPServer
implements an HTTP service in one sentence, setting the current directory as the HTTP service directory, which can be accessed through http://localhost:8000 This may be the implementation of the simplest HTTP server on the planet.
history | awk '{CMD[$2]++;count++;} END { for (a in CMD )print CMD[a] " " CMD[a]/count*100 "% " a }' | grep -v ". /" | column -c3 -s " " -t | sort -nr | nl | head -n10
(Chen Hao Note: It's a bit complicated, history|awk '{print $2}'|awk 'BEGIN {FS=”|” } {print $1}'|sort|uniq -c|sort -rn|head -10)
This line of script prints your ten most frequently used commands, which can even give you an insight into what kind of programmer you are.
tr -c “[:digit:]” ” ” < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR=”1;32″ grep –color “[^ ]”
Want to see Marix’s screen effect ? (Not very similar, but cool too!)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326913570&siteId=291194637