查找
find .
可以查看当前目录所有嵌套的文件
find .|grep .txt
使用管道符
find . -type f
只想看到文件
find . -type d
只想看到目录
find . -type f -exec ls -l '{}' ';'
其中'{}'
代表找到的文件
还可以使用ack
,来自beyondgrep.com
,具体使用见happycasts.net/episodes/26
脚本编程
指定解析器
如脚本第一行#!/usr/bin/env bash
即指定bash为解析器,当然还可以将bash换成python、ruby等
语句即命令,命令即语句
terminal:
$echo "hello"
hello
script:
#!/usr/bin/env python
echo "hello"
脚本语句对空格非常敏感
位置参数
$
即是位置参数
如$peter.sh a.txt b.txt
,则引用第一个参数用$0
,第二个用$1
,第三个用$2
,以此类推
脚本中的语句不在当前shell执行
$ ls
bin Desktop happygrep mydir peter.sh
$ ls mydir/
$ vim peter.sh#!/usr/bin/env bashcd /home/peter/mydir
touch a.txt
$ ./peter.sh
$ ls mydir/
a.txt
当前工作目录没有改变,如果将第八行改成source peter.sh,当前目录就会变成脚本所在目录
循环控制
$ ls mydir/
a b c
$ vim rename.sh
!/usr/bin/env bash
cd $1 # 对应执行脚本时传入的第一个参数
for file in a b c
do
mv $file $file.txt
done
$ chmod +x rename.sh
$ ./rename.sh mydir/
$ ls mydir/
a.txt b.txt c.txt
远程执行
在本地执行一个脚本,此脚本中的命令作用在服务器上
如在本地写一个脚本mkfile.ssh:
ssh -t peter@happycasts.net 'touch a.txt'
执行脚本:
./mkfile.ssh
对服务器来说就相当于执行了此命令touch a.txt
进程
查看当前所有进程
ps aux|grep【要查找的内容】
ctrl+shift+c
复制内容
ctrl+shift+v
粘贴内容
例:vim
启动vim,ps aux|grep vim
查看进程号,如为4646,则要结束这个进程用kill 4646
后台执行
【程序名】 &
程序便在后台执行
使用ctrl+z
结束程序
结束进程
kill【选项】
选项如9
强制杀死进程
Linux同时有7个工作台在运行,当正在使用的出现卡死情况时,ctrl+out+f1
切换工作台,在这里可以杀死那个导致卡死的进程,然后ctrl+out+f7
回到原先工作台
软件安装
安装方式Ⅰ
使用解压缩命令来解压,然后将程序放到echo $PATH
的任意路径下,或者使用软链接,如ln -s ~/.sublime3/sublime_text ~/bin/subl
,其中的subl
是快捷命令名
安装方式Ⅱ
解压缩后进入软件目录下,然后做一下三步:
-
./configure
-
make
-
sudo make install
deb包安装【只适用于ubuntu系统】
程序本身 配置文件 安装位置 依赖关系
happycasts.net/episodes/14会讲解如何将源码打包成deb包
在deb所在的目录下sudo dpkg -i 【包名】.deb
dpkg -l
列出系统上所有的deb包
从apt-get仓库安装
sudo apt-get install git
安装git
sudo apt-get remove git
删除git
sudo apt-get purge git
删除git,连配置文件都一起删除
安装的时候也有tab补齐功能,敲两下tab键,它也能自动处理依赖问题
网络操作
只要ssh
跑得通,那么就能用rsync
rsync -r mydir happycasts.net:【服务器上的路径】
代表将本地的目录mydir上传到服务上
反过来,rsync -r happycasts.net:【服务器上的路径】mydir.
代表将服务器上的目录下载到本地,最后的.
代表当前目录
rsync -r mydir/ happycasts.net:mydir/
可以同步客户端和服务器的对应目录,注意两个/
都不能省,而且一般使用-a
参数替代-r
,-av
可以打印出一些有用信息
-a
只能同步在客户端新创建的文件,而有时候本地还会删除一些文件,需要服务器也做相应删除除了,则应rsync -av --delete mydir/ happycasts.net:mydir/
,但这样也是有风险的,一般在上面命令后再加一个--dry-run
,会开启验证
用户和文件权限
三种权限
读:r
;写:w
;执行:x
三类人
所有者:owner
;用户组:group
;其他人:world
实际考虑的就是每类人是否拥有rwx三种权限
chmod--change file mode
ls -l a.txt
# -rw-rw-r-- 1 peter peter 0 Aug 30 11:57 a.txt
# a.txt对world没有写和执行权限
chmod rw-rw-rw- a.txt # wrong!
chmod 110110110 a.txt # wrong!
chmod 666 a.txt # right! 6对110就是二进制转八进制
实际上还可以通过chown
命令来改变权限所对应的用户,具体看billie66的书《Linux命令行》第十章--权限