天天看点

shell 快捷组合命令及常用命令

​!$ ​

​!$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串。如:你可能会这样:

$mkdir mydir
$mv mydir yourdir
$cd yourdir
可以改成:
$mkdir mydir
$mv !$ yourdir
$cd !$      

直接可以利用​

​!$ 代表上次执行的命令的最后一个字符串​

loongson@LOonux:~$ touch 123
loongson@LOonux:~$ vim 123
loongson@LOonux:~$ touch 123
loongson@LOonux:~$ vim !$
vim 123
loongson@LOonux:~$      

类似的有:

!最近一次命令首个单词 -----  执行最近一次含该单词的命令

loongson@LOonux:~$ ls -al tar.sh 
-rw-r--r-- 1 root root 628 12月 22 16:44 tar.sh
loongson@LOonux:~$ !ls
ls -al tar.sh 
-rw-r--r-- 1 root root 628 12月 22 16:44 tar.sh
loongson@LOonux:~$      

sudo !!----以root的身份执行上一条命令

loongson@LOonux:~$ apt-get install java
E: 无法打开锁文件 /var/lib/dpkg/lock - open (13: 权限不够)
E: 无法对状态列表目录加锁(/var/lib/dpkg/),请查看您是否正以 root 用户运行?
loongson@LOonux:~$ sudo !!
sudo apt-get install java
[sudo] password for loongson:      
# cd -  -----两个非递归目录间的切换(前提:两个目录间的切换是一步完成的)

loongson@LOonux:~$ cd testdir/
loongson@LOonux:~/testdir$ cd /home/loongson/ctcs2/
loongson@LOonux:~/ctcs2$ cd -
/home/loongson/testdir
loongson@LOonux:~/testdir$ cd -
/home/loongson/ctcs2
loongson@LOonux:~/ctcs2$      

cd ~ 是回到自己的Home目录,cd ~user,是进入某个用户的Home目录

date -d@秒   -------- 转换秒为具体时间

loongson@LOonux:~$ date +%s
1359009384
loongson@LOonux:~$ date -d@1359009384
2013年 01月 24日 星期四 14:36:24 CST      

​man ascii​

​​

显示ascii码表

​netstat –tlnp​

​​

列出本机进程监听的端口号​​

​ ​

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

在远程机器上运行一段脚本。这条命令最大的好处就是不用把脚本拷到远程机器上。

yee@Loong:~$ cat sshtest.py 
#!/usr/bin/python
# -*- coding: utf-8 -*-
ls -al > re.txt
yee@Loong:~$ ssh [email protected] sh < sshtest.py 
The authenticity of host '172.16.2.63 (172.16.2.63)' can't be established.
RSA key fingerprint is f2:78:cf:25:bc:68:43:4e:60:89:ca:2e:c9:0e:86:44.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.2.63' (RSA) to the list of known hosts.
[email protected]'s password: 
yee@Loong:~$      

远程机器:已运行脚本并输出记录文件如下

loongson@LOonux:~$ ls -lt
总用量 2799804
-rw-r--r--  1 loongson loongson       5077  1月 25 09:45 re.txt
-rw-r--r--  1 loongson loongson          2  1月 24 14:49 123      

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

比较一个远程文件和一个本地文件

loongson@LOonux:~$ cat re.txt 
总用量 2799932
drwxr-xr-x  2 loongson loongson       4096  1月 17 15:24 下载
drwxr-xr-x  2 loongson loongson       4096 12月 18 15:52 音乐
drwxr-xr-x  2 loongson loongson       4096  1月  8 08:43 桌面      
yee@Loong:~$ cat re.txt 
总计 1153428
drwxr-xr-x  2 yee  yee        4096 2009-02-10 公共的
drwxr-xr-x  2 yee  yee        4096 2009-02-10 模板
drwxr-xr-x  4 yee  yee        4096 2009-11-09 视频
drwxr-xr-x  2 yee  yee        4096 2010-07-06 图片
lrwxrwxrwx  1 yee  yee           7 08-12 20:28 桌面 -> Desktop
yee@Loong:~$ ssh [email protected] cat /home/loongson/re.txt |diff re.txt -
[email protected]'s password: 
1,6c1,4
< 总计 1153428
< drwxr-xr-x  2 yee  yee        4096 2009-02-10 公共的
< drwxr-xr-x  2 yee  yee        4096 2009-02-10 模板
< drwxr-xr-x  4 yee  yee        4096 2009-11-09 视频
< drwxr-xr-x  2 yee  yee        4096 2010-07-06 图片
< lrwxrwxrwx  1 yee  yee           7 08-12 20:28 桌面 -> Desktop
---
> 总用量 2799932
> drwxr-xr-x  2 loongson loongson       4096  1月 17 15:24 下载
> drwxr-xr-x  2 loongson loongson       4096 12月 18 15:52 音乐
> drwxr-xr-x  2 loongson loongson       4096  1月  8 08:43 桌面
yee@Loong:~$      

实时某个目录下查看最新改动过的文件

watch -d -n 1 'df; ls -FlAt /path'

在使用这条命令时你需要替换其中的 /path 部分,watch 是实时监控工具,-d 参数会高亮显示变化的区域,-n 1 参数表示刷新间隔为 1 秒。

df; ls -FlAt /path 运行了两条命令,df 是输出磁盘使用情况,ls -FlAt 则列出 /path 下面的所有文件。

ls -FlAt 的参数详解:

-F 在文件后面加一个文件符号表示文件类型,共有 */=>@| 这几种类型,* 表示可执行文件,/ 表示目录,= 表示接口( sockets) ,> 表示门, @ 表示符号链接, | 表示管道。

-l 以列表方式显示

-A 显示 . 和 ..

-t 根据时间排序文件

显示当前目录中所有子目录的大小

      du -h --max-depth=1

loongson@LOonux:~$ du -h --max-depth=1 
4.0K    ./.gnome2_private
8.0K    ./.ssh
48K    ./.pulse
4.0K    ./视频
4.0K    ./公共的
101M    ./桌面
800K    ./.gstreamer-0.10
4.0K    ./文档
16K    ./.adobe      

net rpc shutdown -I ipAddressOfWindowsPC -U username%password

远程关闭一台Windows的机器,但不一定能执行成功,与win上面的设置有关;

关于net的一些参数:

net time    to view or set time information
  net lookup    to lookup host name or ip address
  net user    to manage users
  net group    to manage groups
  net sam    to edit the local user database directly
  net lookup    to look up various things
  net groupmap    to manage group mappings
  net join    to join a domain
  net cache    to operate on cache tdb file
  net getlocalsid [NAME]  to get the SID for local machine name
  net setlocalsid SID  to set the local machine SID
  net getdomainsid the machine SID and the domain SID on the local server
  net setdomainsid SID  to set the domain SID on member servers
  net changesecretpw  to change the machine password in the local secrets database only
                      this requires the -f flag as a safety barrier
  net status    Show server status
  net usersidlist  to get a list of all users with their SIDs
  net usershare    to add, delete and list locally user-modifiable shares
  net conf    to view and edit samba's registry based configuration

  net ads <command>  to run ADS commands
  net rap <command>  to run RAP (pre-RPC) commands
  net rpc <command>  to run RPC commands

Type "net help <option>" to get more information on that option
Valid targets: choose one (none defaults to localhost)
  -S or --server=<server>    server name
  -I or --ipaddress=<ipaddr>  address of target server
  -w or --workgroup=<wg>    target workgroup or domain

Valid miscellaneous options are:
  -p or --port=<port>    connection port on target
  -W or --myworkgroup=<wg>  client workgroup
  -d or --debuglevel=<level>  debug level (0-10)
  -n or --myname=<name>    client name
  -U or --user=<name>    user name
  -s or --configfile=<path>  pathname of smb.conf file
  -l or --long      Display full information
  -V or --version      Print samba version information
  -P or --machine-pass    Authenticate as machine account
  -e or --encrypt    Encrypt SMB transport (UNIX extended servers only)      

convert input.png -gravity NorthWest -background transparent -extent 尺寸 output.png

改一下图片的大小尺寸,相对参数比较多,有点麻烦;

lsof –i

实时查看本机网络服务的活动状态。

root@LOonux:/home/loongson# lsof -i

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

avahi-dae 1433 avahi 12u IPv4 4020 0t0 UDP *:mdns

avahi-dae 1433 avahi 13u IPv6 4021 0t0 UDP *:mdns

avahi-dae 1433 avahi 14u IPv4 4022 0t0 UDP *:53714

avahi-dae 1433 avahi 15u IPv6 4023 0t0 UDP *:44682

ntpd 1482 ntp 16u IPv4 3814 0t0 UDP *:ntp

ntpd 1482 ntp 17u IPv6 3815 0t0 UDP *:ntp

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

vim一个远程文件

python -m SimpleHTTPServer

一句话实现一个HTTP服务,把当前目录设为HTTP服务目录,可以通过http://IP:8000访问 这也许是这个星球上最简单的HTTP服务器的实现了

但是命令执行中断,服务也就中断;很实用,对与远程有选择的下载文件是个不错的选择;

yee@Loong:~$ python -m SimpleHTTPServer

Serving HTTP on 0.0.0.0 port 8000 ...

172.16.7.94 - - [25/Jan/2013 11:35:33] "GET / HTTP/1.1" 200 -

172.16.7.94 - - [25/Jan/2013 11:35:39] code 404, message File not found

172.16.7.94 - - [25/Jan/2013 11:35:44] "GET /favicon.ico HTTP/1.1" 404 -

172.16.7.94 - - [25/Jan/2013 11:36:00] "GET /test.py HTTP/1.1" 200 -

172.16.7.94 - - [25/Jan/2013 11:36:33] "GET /shell/ HTTP/1.1" 200 -

如果该文件夹下恰好存在 Index.html 文件,则在浏览器中输入IP进入时会优先进入该index文件。

yee@Loong:~$ wget --random-wait -r -p -e robots=off -U mozilla http://www.baidu.com

100%[===============================================>] 208,993 140K/s in 1.5s 2013-01-25 10:58:36 (140 KB/s) - `www.baidu.com/cache/sethelp/img/green_browser1.jpg' 已保存 [208993/208993]--2013-01-25 10:58:36-- http://www.baidu.com/cache/sethelp/img/green_browser2.jpg正在连接
 www.baidu.com|115.239.210.27|:80... 已连接。已发出 HTTP 请求,正在等待回应... 200 OK长度:158563 (155K) [image/jpeg]保存到‘www.baidu.com/cache/sethelp/img/green_browser2.jpg’100%[===============================================>] 158,563 121K/s in 1.3scurl ifconfig.me当你的机器在内网的时候,可以通过这个命令查看外网的IPloongson@LOonux:~$ curl ifconfig.me
222.92.8.142