liunx的基础命令-02

Linux(Ubuntu)基础命令02

1.echo 输出

root@doublechina:~# echo "hello world"
hello world

2. 重定向

  • > 覆盖
  • >> 尾部追加
  • cat 查看
cat file # 一次查看所有的文件
cat file1  file2 # 一次查看两个命令
# >覆盖内容
doublechina@doublechina:~$ echo hello > 3.txt
#cat查看文本内容
doublechina@doublechina:~$ cat 3.txt
hello

# >>尾部追加内容
doublechina@doublechina:~$ echo python java  >> 3.txt
doublechina@doublechina:~$ cat 3.txt
hello
python java

#cat 读取内容,写入其他文件
doublechina@doublechina:~$ cat  .bash_logout  >> 3.txt
doublechina@doublechina:~$ ls
3.txt  aa  bb  cc  ddcc  ee
doublechina@doublechina:~$ cat 3.txt

doublechina@doublechina:~$ echo hello >>1.txt
doublechina@doublechina:~$ ls
1.txt  3.txt  aa  bb  cc  ddcc  ee
#cat查看多个文件
doublechina@doublechina:~$ cat 1.txt 3.txt

3.more | less

more filename # 分页查看

  • f向下翻页
  • b向上翻页
  • enter 逐行阅读
  • 翻到末页结束

less filename # 分页查看

  • f 向下
  • b 向上
  • 循环翻页,不会自动结束
  • q 退出
#more分页查看
doublechina@doublechina:~$ more 1.txt
if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi
hello
python java
# ~/.bash_logout: executed by bash(1) when login shell exits.

# when leaving the console clear the screen to increase privacy

--More--(9%)  
#按回车键逐行阅读 

#less 查看分页
doublechina@doublechina:~$ less 1.txt
....
fi
hello
python java
# ~/.bash_logout: executed by bash(1) when login shell exits.
# when leaving the console clear the screen to increase privacy
1.txt
# f,b翻页,q退出

4. which / whereis 查找命令

which command # 查看 二进制文件

doublechina@doublechina:~$ which ls
/bin/ls

whereis 可执行文件 # 二进制文件 、man手册

doublechina@doublechina:~$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz

帮助文档:
1.man手册 ,帮助文档 man ls
2.ls --help

doublechina@doublechina:~$ man ls
#翻页查看

doublechina@doublechina:~$ ls --help
#显示所有的

5.find 查找文件

find 路径 参数

doublechina@doublechina:~$ ls
1.txt  3.txt  ee
#find查出所有的文件,包含隐藏的文件
doublechina@doublechina:~$ find
.
./.viminfo
./.bash_history
./3.txt
./.profile
./.cache
./.cache/motd.legal-displayed
./.bash_logout
./1.txt
./.bashrc
./ee
./ee/ff
./.sudo_as_admin_successful

常用参数
-name # 按照名字

doublechina@doublechina:~$ find /home/doublechina -name 3.txt
/home/doublechina/3.txt
#用*必须带上引号,要不查找不到
doublechina@doublechina:~$ find /home/doublechina -name '*.txt'
/home/doublechina/3.txt
/home/doublechina/1.txt

#全局搜索某个文件
doublechina@doublechina:~$ sudo  find / -name  "3.txt"
[sudo] password for doublechina: 
/home/doublechina/3.txt

-size # 按照大小
find ./ -size +100k -size -10M # 在当前目录下找大于100k 小于 10的文件

#找出家目录中大于 3k的文件
doublechina@doublechina:~$ find ~ -size +3k
/home/doublechina
/home/doublechina/.cache
/home/doublechina/1.txt
/home/doublechina/.bashrc
/home/doublechina/ee
/home/doublechina/ee/ff

#找出家目录中大于 3k小于5k的文件
doublechina@doublechina:~$ find ~ -size +3k -size  -5k
/home/doublechina
/home/doublechina/.cache
/home/doublechina/1.txt
/home/doublechina/.bashrc
/home/doublechina/ee
/home/doublechina/ee/ff

6. grep 文本搜索(筛选内容)

grep 'content' filename

  • 常用参数
  • -v 显示不包含匹配文本的所有‘行’ (求反)
  • -n 显示匹配行及行号
  • -i 忽略大小写
  • 内容参数
  • ^hello 行首 搜索以hello开头的行
  • fi$ 行尾 索以wh结束的行
#显示带java字符的哪行
doublechina@doublechina:~$ grep "java" 3.txt
python java
# -n显示行号
doublechina@doublechina:~$ grep -n  "java" 3.txt
2:python java
#-i 不区分大小写
doublechina@doublechina:~$ grep -i  "Java" 3.txt
python java
#-v取反
doublechina@doublechina:~$ grep -v  "java" 3.txt
hello
# ~/.bash_logout: executed by bash(1) when login shell exits.

# when leaving the console clear the screen to increase privacy

if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi
# ^脱字符,以什么开头
doublechina@doublechina:~$ grep -n  "^hello" 3.txt
1:hello
# $以什么结尾的
doublechina@doublechina:~$ grep -n  "fi$" 3.txt
9:fi

7. | 管道

一个命令的输出,可以通过管道符,做为另一个命令的输入

命令输出 | 命令处理

ls --help | less # 将输出,放入翻页模式中
ls --help | grep -n 'txt' #将输出,放入‘筛选’模式中
ls --help | grep -n 'txt' >> 3.txt #将输出,放入‘筛选’模式中,然后将重定向到3.txt

 ls --help 的输出内容,输出给less使用
doublechina@doublechina:~$ ls --help | less

doublechina@doublechina:~$ ls -l | grep -n  "txt"
2:-rw-rw-r-- 1 doublechina doublechina 4052 Dec  5 16:27 1.txt
3:-rw-rw-r-- 1 doublechina doublechina  238 Dec  5 13:13 3.txt

#将输出内容,放入‘筛选’模式中,然后将重定向到3.txt
doublechina@doublechina:~$ ls -l | grep -n  "txt" >> 3.txt
doublechina@doublechina:~$ ls
1.txt  3.txt  ee
doublechina@doublechina:~$ cat 3.txt
hello
python java
# ~/.bash_logout: executed by bash(1) when login shell exits.

# when leaving the console clear the screen to increase privacy

if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi
2:-rw-rw-r-- 1 doublechina doublechina 4052 Dec  5 16:27 1.txt
3:-rw-rw-r-- 1 doublechina doublechina  238 Dec  5 13:13 3.txt

8. ln 创建链接文件

ln  file  hardlink # 硬链接
ln -s  file softlink  # 软链接

软链接: 相当于 window上的快捷方式 源文件删除则软链接失效

硬链接: 硬链接只能连接普通的文件 不能连接目录

注意 如果软链接文件和源文件不在同一个目录 源文件要使用绝对路径 不能使用相对路径

软链接:  相当于 `window`上的快捷方式,源文件删除则软链接失效

doublechina@doublechina:~$ ln -s 3.txt soft.txt
doublechina@doublechina:~$ ls
1.txt  3.txt  ee  softlink.txt  soft.txt
doublechina@doublechina:~$ echo "cccc" >> soft.txt
doublechina@doublechina:~$ cat 3.txt
hello
python java
# ~/.bash_logout: executed by bash(1) when login shell exits.
# when leaving the console clear the screen to increase privacy
if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi
2:-rw-rw-r-- 1 doublechina doublechina 4052 Dec  5 16:27 1.txt
3:-rw-rw-r-- 1 doublechina doublechina  238 Dec  5 13:13 3.txt
cccc
doublec

#硬链接,改了硬链接的内容,源文件内容也会改变
#删除源文件,硬链接还是存在
doublechina@doublechina:~$ touch 2.tx
doublechina@doublechina:~$ ls
1.txt  2.tx  3.txt  ee  softlink.txt
doublechina@doublechina:~$ ln 2.tx hard.txt
doublechina@doublechina:~$ ls
1.txt  2.tx  3.txt  ee  hard.txt  softlink.txt
doublechina@doublechina:~$ echo aaa >> hard.txt
doublechina@doublechina:~$ cat hard.txt
aaa
doublechina@doublechina:~$ cat  2.tx
aaa
doublechina@doublechina:~$ rm 2.tx
doublechina@doublechina:~$ ls
1.txt  3.txt  ee  hard.txt  softlink.txt
doublechina@doublechina:~$ cat hard.txt
aaa


9. alias 创建别名

  • alias # 查看所有别名 alias c3='cat 3.txt'
  • unalias # 删除别名
doublechina@doublechina:~$ alias c1="cat 1.txt"
doublechina@doublechina:~$ c1
hello
hello
....
doublechina@doublechina:~$ unalias c1
doublechina@doublechina:~$ c1
c1: command not found


注意 这种定义别名的方式 只在当前登录有效
如果要永久定义生效,可以通过修改~/.bashrc文件
这个修改要下次登录才能生效,想要立即生效 可以输入source ~/.bashrc

10. vim 编辑器

工作模式:命令模式、输入模式、末行模式

模式之间切换

  • 当打开一个文件时处于命令模式
  • 在命令模式下,按 i 进入输入模式
/alias 
  • 在命令模式下,按 '/' 定位搜索关键字

  • 在输入模式,按ESC回到命令模式。

  • 在命令模式下,按shift+;,末行出现:冒号,则进入末行模式

#进入命令模式
doublechina@doublechina:~$ vim 1
#进入输入模式
输入一个i或者a 或者 o
#退出输入模式 ,按下ESC,进入命令模式

命令模式下,按`shift+; `,末行出现`:`冒号,则进入末行模式
进入末行模式,输入wq回车退出

进入与退出

vim filename 进入

当打开一个文件时处于命令模式

在末行模式下输入q退出文件

  • ! 强制 w 保存 q 退出
  • wq 保存退出
  • q! 不保存退出(强制退出)
  • wq! 强制保存退出
  • 搜索内容 :在命令模式下 / 内容。 例子:/kkkk

注意

  • 在vim下千万不要按Ctrl + s 。 不然就会卡死
  • 如果按了Ctrl + s,按Ctrl + q 可以退出

11.在Ubuntu14.04上安装Pip

步骤一: 在终端上使用以下命令,来保证你系统上所有的包都是最新的。
sudo apt-get update

doublechina@doublechina:~$ sudo apt-get update
[sudo] password for doublechina: 
Hit:1 http://cn.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://cn.archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:3 http://cn.archive.ubuntu.com/ubuntu xenial-backports InRelease
Get:4 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Fetched 102 kB in 3s (26.3 kB/s)  
Reading package lists... Done

sudo apt-get upgrade

doublechina@doublechina:~$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree      
Reading state information... Done
Calculating upgrade... Done
....
Do you want to continue? [Y/n] y  #填写y就好了
Get:1 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main i386 dpkg i386 1.18.4ubuntu1.3 [2,112 kB]
.....
Processing triggers for resolvconf (1.78ubuntu5) ...
Processing triggers for ca-certificates (20170717~16.04.1) ...
Updating certificates in /etc/ssl/certs...
17 added, 42 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.

步骤二: 安装Pip

安装python3-pip和你所需要的包:

sudo apt-get install python3-pip

doublechina@doublechina:~$ sudo apt-get install python3-pip
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following additional packages will be installed:
....
Do you want to continue? [Y/n] y
....
Setting up python3-dev (3.5.1-3) ...
Setting up python3-pip (8.1.1-2ubuntu0.4) ...
Setting up python3-setuptools (20.7.0-1) ...
Setting up python3-wheel (0.29.0-1) ...
Processing triggers for libc-bin (2.23-0ubuntu9) ...
安装完成

检查你所安装Pip的版本:
pip3 -V

doublechina@doublechina:~$ pip3 -V
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,825评论 4 377
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,887评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,425评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,801评论 0 224
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,252评论 3 299
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,089评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,216评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 31,005评论 0 215
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,747评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,883评论 2 255
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,354评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,694评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,406评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,222评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,242评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,017评论 2 281

推荐阅读更多精彩内容