Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Something you may not know about Shell

  sonic0002        2012-11-24 07:19:47       5,020        0    

Shell is also called command line interface, it is the interface between *nix users and computer. Shell is a program which provides service to access OS kernel services.

This article is to introduce some not so well known but useful and interesting knowledge about shell.

Wiki

  • shell was born almost at the same time as Unix. The first UNIX shell was written by Ken Thompson in 1971 and its name is Thompson sh. It's older than Linux kernel.
  • The default Shell in most *nix and MacOS is bash, bash was created by Brian Fox in 1987, the full name is Bourne Again shell(bash).
  • There are other shells except bash, there are Bourne shell(sh), Korn shell(ksh), C shell(csh). Actually there are over 50 different shells. For details , please refer : http://www.freebsd.org/ports/shells.html

Some powerful commands

We strongly recommend that you open a shell prompt console to try these commands.

!$

!$ is a special environment variable, it represents the last string of last command. For example:

$mkdir mydir
$mv mydir yourdir

$cd yourdir

You can modify it as:

$mkdir mydir
$mv !$ yourdir
$cd !$

sudo !!

Execute the previous command with root role.

For example:

In Ubuntu, it needs root role when using apt-get to install packages, we may often forget to add sudo before apt-get. So everytime we may need to add sudo and then type this command again, actually we can use sudo !! to complete this easily.(Edited by Hao Chen : In shell, sometimes you may type a long command, you can use !XXX to repeat last command, for example, you might type vi /where/the/file/is, next time you can use !vi to repeat the vi command.

cd -

Back to the previous directory you accessed. For example: The current working directory is /home/a, then you use cd ../b to go to /home/b, now you can use cd - to switch between /home/a and /home/b forth and back.

'ALT+.' or '.'

Hot key alt+. or esc+. can bring back the options of last command

^old^new

Replace some characters in previous command

For example :

echo "wanderful", actually you may want to output echo "wonderful". Then you only need ^a^o. It's very useful when you want to modify a very long command just because some spell errors. (Edited by Hao Chen :You can also use !!:gs/old/new)

du -s * | sort -n | tail

List the biggest 10 files in current directory

:w !sudo tee %

Save a file which can only be writable with root permission in vi.

date -d@1234567890

Covert timestamp to date

> file.txt

Create an empty file

mtr coolshell.cn

mtr is better than traceroute

Add an empty space before a command, this command will be in the history list.

echo “ls -l” | at midnight

execute a command at some time

curl -u user:pass -d status=”Tweeting from the shell” http://twitter.com/statuses/update.xml

Update Twitter in command line

curl -u username –silent “https://mail.google.com/mail/feed/atom” | perl -ne ‘print “\t” if //; print “$2\n” if /<(title|name)>(.*)<\/\1>/;’

Check your unread mails in Gmail

ps aux | sort -nk +4 | tail

List top 10 processes which consume most memory

man ascii

Show the ASCII chart

ctrl-x e

Launch default editor quickly (It is set by $EDITOR)

netstat –tlnp

List the port number which local host processes listen to

tail -f /path/to/file.log | sed '/^Finished: SUCCESS$/ q'

Quit tail when "Finished: SUCCESS" appears in the file.log file, this command is used to check and filter some records in log in real time.

ssh user@server bash < /path/to/local/script.sh

Execute a script in a remote machine. The best thing about this command is there is no need to copy the script to the remote machine.

ssh user@host cat /path/to/remotefile | diff /path/to/localfile -

Compare a local file with a remote file

net rpc shutdown -I ipAddressOfWindowsPC -U username%password

Shutdown a remote windows machine

wget --random-wait -r -p -e robots=off -U mozilla http://www.example.com

Download the whole example.com website

curl ifconfig.me

When you are in an intranet, you can use this command to check IP on internet.

convert input.png -gravity NorthWest -background transparent -extent 720×200  output.png

Resize a picture

lsof –i

Check network service status

vim scp://username@host//path/to/somefile

vim a remote file

python -m SimpleHTTPServer

One command to realize HTTP service, set the current directory as HTTP service directory. You can use http://localhost:8000 to access it. This may be 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

This command will list the top 10 most frequently used commands

You can also refer to another artciel for more useful Linux commands: Concise bash programming skills.

Author : Source : http://coolshell.cn/articles/8619.html

LINUX  COMMAND  SHELL  UNIX 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.