linux命令学习笔记

http://blog.csdn.net/tangwei2014/article/details/46714589

copy:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>cp file1 file2
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>cp -r dir1 dir2</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

move:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>mv file ..
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>mv file dir/</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

rename:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>mv file1 file2
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>mv dir1 dir2 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># dir2如果存在,则为移动操作</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

remove:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>rm file
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>rm -r dir</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

创建文件:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>touch a.txt
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>>a.txt</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

创建目录:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>mkdir dir</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

查看文件:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#一般</span>
$ cat <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#查看文件类型:</span>
$ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> <span class="hljs-operator" style="box-sizing: border-box;">a</span>.txt
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#创建和查看隐藏的文件/文件夹:</span>
$ touch .<span class="hljs-operator" style="box-sizing: border-box;">a</span>.txt  或者 mkdir .dir
$ ls -<span class="hljs-operator" style="box-sizing: border-box;">a</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#分页查看文件:</span>
$ less <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#ctrl ++:缩小  ctrl --:放大  j:向下滚屏 k:向上滚屏  /:字符查找 n:查找下一处 gg:文件头 G:文件尾 q:退出</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

*通配符

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>rm *.html <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#删除所有的html文件</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>echo a* <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 看所有以a开头的文件</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

打包和压缩:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#zip文件</span>
$ zip -r name<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zip</span> dir
$ unzip name<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zip</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#.tar.gz文件</span>
$ tar zxvf name<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</span>
$ tar zcvf name<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</span> dir
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#.tar.bz2文件</span>
$ tar jxvf name<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bz</span>2
$ tar jcvf name<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bz</span>2 dir</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

重定向: 
三个重要文件: 
linux下所有的东西都是文件 
文件描述符: 0 标准输入文件 1 标准输出文件 3 标准错误输出文件 
重定向的方式: 
输出重定向符:> 每次重定向之前都会把文件清空 >> 追加内容到一个文件中 
输出错误重定向符: 2> 
输入重定向符:< 
管道线:linux命令行的基石 
管道线命令符:| 把符号前面的结果传给符号后面的命令作为其输入

用户和权限: 
默认只有在自己的主目录/home/usrname/下有写权限 其他的地方没有写权限(ctrl + a可以直接到命令的行首) 
三种基本权限: 
读:r 写:w 执行:x 
多用户模式: owner group world 
文件模式:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>ls -l a.txt <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#对文件查看权限信息</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>ls -ld dir <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#对目录查看权限信息</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

对一个目录有写权限,意味着可以对里面的文件进行创建删除重命名等操作,但是对一个文件有用写权限并不拥有这些操作权限 
对一个目录有执行权限,意味着可以cd进这个目录,目录在创建时 一般都有执行权限 
chmod命令: 
对三种用户模式下每一种文件权限都可以用1代表有改权限,0代表没有该权限

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>chmod <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">111110110</span> a.txt <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># owner有rwx group和world只有rw权限</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

进程: 
获取进程号: 
PID就表示进程号 
linux下粗略的可以认为,用户每次执行一个新的程序,系统都会开启一个新的进程 
ps命令:作用是报告系统上当前的进程状况 
ps aux |less 查看所有的进程的进程号 $ ps aux |grep vim 查找vim的进程号 
Alt + Tab可以在各个打开的程序直接切换 
后台执行: 
在后面加&符号,可以让程序在后台执行 
如果已经打开程序了,补救的办法就是先用ctrl+z让程序暂停,回到命令行,然后输入bg命令,这样和加&一样。然后敲fg又回到前台,crtl + c关闭 
kill:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>kill pid号   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 程序在正常运行的时候有用,相当于加了 -2</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>kill -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span> pid  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 当程序跑飞了,强制终止</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

如果程序运行卡死了命令行界面,怎么办: 
linux同时运行着7个工作台的,敲crtl + alt + F1到第一个工作台在这里输入命令杀死那个进程,然后敲crtl + alt + F7回到原来的工作台

查找: 
locate:在系统全局范围内定位文件 
locate不是在整个File system中定位文件,而是从一个database中定位文件,这也是locate执行很快的原因。 
还有一个updatedb命令,负责将File system中的文件更新到database中,但是updatedb一般是每天执行一次。导致locate 不可能找到新创建的那些文件。 
补救办法:$ sudo updatedb 然后再 locate 
find:在目录范围内定位文件

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>find dir <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#列出目录下的所有文件</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>find dir  -type f <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 只看目录下的所有文件</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>find dir  -type d <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 只看目录下的所有目录</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>find dir -type f -exec xxx <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'{}'</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">';'</span>  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># xxx可以是各种系统命令,对找到的文件进行操作</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>find dir -type f -exec grep hello <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'{}'</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">';'</span>  -print <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 在找到的所有文件中查找hello字符串,并且打印所在的文件的文件名</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

grep:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>grep hello <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#查找hello字符串</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>grep -n hello <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#并且显示出行号</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>grep -ni hello <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#显示出行号,并忽略大小写</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

ack grep:专门为程序员在代码中查找的命令 
将一个二进制文件移动到~/bin下,在整个系统中就能使用该命令了: $ rm xxx ~/bin 
如果一个命令太长,可以到~/bin目录下 $ ln -s xxx y 就可以给xxx取一个别名y 以后用y就相当与用xxx

网络操作: 
远程操作: 
自己电脑上的程序要放到服务器上,可以花10$每个月买一台VPS服务器,在服务器上装上ubuntu server系统。 
ssh:可以认为是互联网上两台机器互联的一种协议,需要服务器端开22端口,就如http协议需要开80端口。除了开22端口,还需要服务器端和客户端装相应的ssh软件。 
ubuntu desktop版和ubuntu server版这两个都默认是装好的。 
买服务器的时候,会给一个ip,可以给这个ip绑定一个域名。 
在desktop上可以这样连接服务器:$ ssh 服务器上的用户名@域名或者ip 输入密码以后就可以在服务器上操作了。 
ctrl+d可以退出服务器。 
每次输入密码比较烦,可以在本地上输入$ ssh-keygen 然后在.ssh下可以得到公钥(id_rsa.pub)和私钥(id_rsa)。仍然在.ssh下运行$ ssh-copy-id 服务器上的用户名@域名或者ip。以后就不要在输入密码了。 
tmux是一个很好的远程操作防止断网和误操作中断通信,数据没有保存的软件。 
数据传输: 
rsync: 本地和远端同步数据用的。如果ssh能用,rsync就能用。 
上传使用:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>rsync -r 本地目录 服务器上的用户名@域名或者<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">ip:</span>服务器上路径</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

就可以把本地的目录上传到服务器路径下。注意本地目录后面不能有/ 
下载只需要交换上面两个参数的位置即可 
如何同步本地增加的文件:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>rsync -av mydir/ 服务器上的用户名@域名或者<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">ip:</span>服务器上路径/mydir/</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

这里v参数是为了显示执行过程 a参数是为了保留更加完整的文件信息 
如何同步本地既有增加又有删除的文件:

<code class="hljs coffeescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$  rsync -av --<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">delete</span> mydir/ 服务器上的用户名@域名或者<span class="hljs-attribute" style="box-sizing: border-box; color: rgb(0, 136, 0);">ip</span>:服务器上路径<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/mydir/</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

为了防止误删除一些文件,可以先执行:

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">$</span>  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">rsync</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">av</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">delete</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">mydir/</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">服务器上的用户名@域名或者ip:服务器上路径/mydir/</span>  <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">dry</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">run</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

先只报告信息,再执行真正的同步操作。

软件安装: 
手动安装: 
下载编译好后的代码解压缩: 
得到的目录放在一个不碍事的地方,最好放在一个隐藏目录下

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$PATH</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#可以查看环境变量PATH,它的值是一系列由:隔开的目录。</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

只需要把可执行文件放在PATH下的任何一个目录中,就可以成为一个系统可执行的命令了。 
也可以不移动可执行程序本身,而是创建一个链接:$ ln -s 本来的可执行程序目标位置以及名字 
自己下载源码编译安装: 
解压缩并切换到源码目录

<code class="hljs go has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">./configure
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">make</span>
sudo <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">make</span> install</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

deb包:debian package 
手动安装的问题:安装依赖 会给安装过程带来不便;安装的软件多的时候会忘记都在哪些位置安装了文件,卸载软件就成了一场噩梦 
deb包 = 程序本身+配置文件+安装位置+依赖关系 
HAPPYCASTS第十四期讲解如何将一个程序从源码打成一个deb包 
安装一个程序的deb包:$ sudo dpkg -i deb包

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>dpkg -l |grep chrome <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#可以查看所有安装过的chrome deb包</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>dpkg -<span class="hljs-constant" style="box-sizing: border-box;">L</span> 包名 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#看包安装了哪些文件</span>
<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>dpkg -<span class="hljs-constant" style="box-sizing: border-box;">S</span> 文件名 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#看一个文件来自那个包</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

从apt-get仓库安装: 
apt-get中底层包裹了dpkg 
开源软件一般是以源码包的形式放在github或者自由软件基金会上的,ubuntu公司会定期将这些源码包打造成deb包,并在ubuntu自己服务器上搭建一个软件仓库apt repo,再把deb包上传到软件仓库中。 
在ubuntu下安装软件非常方便,$ apt-get install 软件名 就可以安装好了 
卸载软件:$ sudo apt-get remove 软件名 
连配置文件一起删除:$ sudo apt-get purge 软件名 
不知道精确的包名的时候,可以用: $ apt-cache search 貌似的软件名|less 
apt-get 也会自动帮安装上依赖文件

脚本编程技巧: 
指定解析器: 
一般用bash作为解析器,所以在第一行要加上: #!/usr/bin/env bash 指明用bash解析下面的语句 
但是不仅bash可以解析,python ruby等等都可以解析脚本 #!/usr/bin/env python 指明用python解析 
语句即命令: 
在命令行中的命令,放在脚本中就称为一个语句 
命令在脚本中可以直接罗列,也可以包裹到一个函数中,通过呼叫函数名来执行命令: 
say_hello(){ echo "hello" } 然后调用say_hello调用echo “hello”命令 
命令对空格敏感,所以脚本语句对空格异常敏感 
位置参数: 
脚本运行的时候也可以输入参数,在脚本中接受参数用$符号 $#表示参数的个数 $0 脚本文件名\ $1–\$n 用户提供的参数 
脚本不在当前的shell执行: 
脚本不是在当前的shell里执行的,而是新开了一个shell并在里面执行脚本的。 
非要在当前shell中执行脚本,用source xx.sh 
循环控制:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$1</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> file <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> `ls`
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span>
   ****命令
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">done</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

脚本中导引好可以取出命令的输出 pwd 
远程执行: 
脚本中写:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>ssh -t 远程用户名<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ip</span>或者域名  <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'要执行的脚本或者命令'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

确认执行:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> -n <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Want to continue? (Y/n)"</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">read</span> AAA
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${AAA:-y}</span>"</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"y"</span>];<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span>
     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#调用函数</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> Nothing <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">done</span>, bye.
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span></code>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值