Linux个人学习笔记2 Linux系统基本操作

1.命令格式

command [-options] [paramter]
#命令 [-选项] [参数]
#例如
ls -l /vedios

2.路径操作

(1)ls

  • 效果
    作用:以平铺的方式列出当前目录中所有文件以及文件夹
[failur@localhost ~]$ ls
#Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
  • 语法
ls [-a -l -h] [/...]

用户登录时,进入的目录不是根目录,而是:/home/用户名 (工作目录)

  • 参数及选项
    · -a (all)列出全部文件(包含隐藏文件)

文件前有’.'的文件为隐藏文件

[failur@localhost ~]$ ls
#Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[failur@localhost ~]$ ls -a
#.   .bash_history  .bash_profile  .cache   .dbus    Documents  .esd_auth      .local    Music     .pki    Templates
#..  .bash_logout   .bashrc        .config  Desktop  Downloads  .ICEauthority  .mozilla  Pictures  Public  Videos

· -l (list)以列表形式按行展示所有信息

[failur@localhost ~]$ ls -l
#总用量 0
#drwxr-xr-x. 2 failur failur  6 2月   3 20:33 Desktop
#drwxr-xr-x. 2 failur failur  6 2月   3 20:33 Documents
#drwxr-xr-x. 2 failur failur 34 2月   4 01:32 Downloads
#drwxr-xr-x. 2 failur failur  6 2月   3 20:33 Music
#drwxr-xr-x. 3 failur failur 24 2月   4 01:32 Pictures
#drwxr-xr-x. 2 failur failur  6 2月   3 20:33 Public
#drwxr-xr-x. 2 failur failur  6 2月   3 20:33 Templates
#drwxr-xr-x. 2 failur failur  6 2月   3 20:33 Videos
    权限        用户组   用户   大小     创建时间  文件名

· 选项可以混合使用

[failur@localhost ~]$ ls -a -l
#总用量 36
#drwx------. 16 failur failur 4096 2月   4 01:31 .
#drwxr-xr-x.  4 root   root     35 2月   3 21:26 ..
#-rw-------.  1 failur failur  378 2月   4 10:30 .bash_history
#-rw-r--r--.  1 failur failur   18 10月 30 2018 .bash_logout
#-rw-r--r--.  1 failur failur  193 10月 30 2018 .bash_profile
#-rw-r--r--.  1 failur failur  231 10月 30 2018 .bashrc
#drwx------. 18 failur failur 4096 2月   4 01:32 .cache
#drwxr-xr-x. 17 failur failur 4096 2月   4 01:32 .config
#...

· -h 以易于阅读的方式展示文件大小(k,m,g)

  • 必须与-l连用,不然没有作用
[failur@localhost Downloads]$ ls -l
#总用量 1324
#文件以byte显示,没有单位
#-rwxrw-rw-. 1 failur failur 1355284 9月   8 21:33 springLeader_2_1.png

[failur@localhost Downloads]$ ls -h
#不与-l连用,没有效果
#springLeader_2_1.png

[failur@localhost Downloads]$ ls -l -h
#总用量 1.3M
#可以看到以M显示
#-rwxrw-rw-. 1 failur failur 1.3M 9月   8 21:33 springLeader_2_1.png

(2)cd

  • 效果
    cd (change directory)

更改当前工作目录

  • 语法
    cd [linux路径]

cd命令没有参数,单独执行自动回到HOME目录

cd / #前往根目录
cd   #回到HOME目录

(3)pwd

  • 用法
    无参数 无选项 返回工作路径

(print worker directory)

[failur@localhost Downloads]$ pwd
# /home/failur/Downloads

(4)路径

  • 绝对路径与相对路径
    起点从根目录开始 为绝对路径
cd /home/failur/Downloads

起点从当前文件夹(/home)进入子文件夹为相对路径

cd failur/Downloads
  • 特殊路径符
    · . 当前目录

· …上一级目录

../.. 上上一级目录

· ~ home目录

3.文件操作

(1)mkdir

mkdir [-p] [linux路径][…]

参数必填 相对/绝对路径

-p参数表示自动创建父目录

支持多参数

[failur@localhost Downloads]$ mkdir test0/test1/test2
#不加参数的情况:
#mkdir: 无法创建目录"test0/test1/test2": 没有那个文件或目录
[failur@localhost Downloads]$ mkdir -p test0/test1/test2
#创建成功

例:多参数

[failur@localhost test]$ mkdir t1 t2 t3 t4
[failur@localhost test]$ ls
#t1  t2  t3  t4

(2)touch

touch [路径] 创建文件

[failur@localhost test1]$ touch tt.txt
[failur@localhost test1]$ ls
#tt.txt

(3)cat

cat [路径] 读取文件所有内容

[failur@localhost test1]$ cat hw.txt
#Hello world!

(4)more

more[路径] 按页读取,用于大容量文件的读取

空格键翻页

q键退出

(5)cp

cp [r] [路径1] [路径2]

copy 复制文件/文件夹,将路径1内容复制到路径2处

-r 复制文件夹 表示递归

[failur@localhost test]$ ls
#t.t
[failur@localhost test]$ cp t.t t1.t #复制t.t到当前目录的t1.t 自动创建
[failur@localhost test]$ ls
#t1.t  t.t
[failur@localhost Downloads]$ cp test test1
#不用-r无法复制文件夹
#cp: 略过目录"test"
[failur@localhost Downloads]$ cp -r test test1
[failur@localhost Downloads]$ ls
#springLeader_2_1.png  test  test

(6)mv

mv [路径1] [路径2]

move 将路径1的文件/文件夹转移到路径2处

在原目录上操作具有文件重命名的效果

[failur@localhost Downloads]$ ls
#springLeader_2_1.png  test  test1
[failur@localhost Downloads]$ mv test test2 
#将原来的test尝试放到test2中,但test2不存在,故自动创建新的test2
[failur@localhost Downloads]$ ls
#springLeader_2_1.png  test1  test2

(7)rm

rm [-r -f] [参数1] [参数2] […]

remove 移除参数路径的文件/文件夹

-r用于迭代删除文件夹内的所有内容

-f force用于强制删除 (没有提示,直接删除,仅管理员)

  • 参数数量可以无限多,删除每个参数路径的内容

Linux下只有管理员删除时会提示注意信息,普通用户则不会

  • 提权指令:
    su root
    注意退出 exit
  • rm支持通配符!
    rm *test 会删除 结尾为test的文件

rm test 会删除 包含test的文件

rm test* 会删除 开头为test的文件

例1,-r的使用

[failur@localhost ~]$ ls
#Desktop  Documents  Downloads  Music  Pictures  Public  Templates  test1  Videos
[failur@localhost ~]$ rm test1
#rm: 无法删除"test1": 是一个目录
[failur@localhost ~]$ rm -r test1 #使用参数-r即可删除
[failur@localhost ~]$ ls
#Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

例2,多个参数批量删除

#springLeader_2_1.png  test2  test3
[failur@localhost Downloads]$ rm -r test2 test3
[failur@localhost Downloads]$ ls
#springLeader_2_1.png

例3,通配符

#t1  t2  t3  t4
[failur@localhost test]$ rm -r t*
[failur@localhost test]$ ls
#无返回

4.查找相关

(1)which

which [参数]

which用于查找可执行文件的位置

Linux中 如 cd ls命令 实际是一个个可执行文件

可以使用which来查找它们存放的位置

例,查找常用指令

[failur@localhost ~]$ which cd
#/usr/bin/cd
[failur@localhost ~]$ which ls
#alias ls='ls --color=auto'
#        /usr/bin/ls
[failur@localhost ~]$ which which
#alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
#        /usr/bin/alias
#        /usr/bin/which
[failur@localhost ~]$ 

(2)find

  1. find [路径] -name ‘*’
  2. find [路径] - size +/- [k/M/G]
    find支持按名称搜索以及按照大小搜索

按照名称搜索支持通配符,定向检索文件名

按照大小搜索时注意 k为小写,M,G为大写

可以检索大小小于或者大于某个大小的文件

例1,按照名称搜索

[root@localhost failur]# find . -name 'test'
# ./Downloads/test

例2,按照大小搜索

[root@localhost failur]# find . -size  -100k
#.
#./.mozilla
#./.mozilla/extensions
#./.mozilla/plugins
#...

例3,多条件查询

[failur@localhost ~]$ find . -size +98k -size -190k -name *goog*
#./.cache/mozilla/firefox/x6i8fyrf.default/safebrowsing/google4/goog-phish-proto.pset
#./.cache/mozilla/firefox/x6i8fyrf.default/safebrowsing/google4/goog-malware-proto.pset
#./.cache/mozilla/firefox/x6i8fyrf.default/safebrowsing/google4/goog-unwanted-proto.pset
#./.cache/mozilla/firefox/x6i8fyrf.default/safebrowsing/google4/goog-badbinurl-proto.pset

5.文件内容

(1)grep

grep [-n] ‘*’ [路径]

文件:

hello world
wowo
what a beautiful day~
wowowo
wowowowowow
lololololololo
hohohohohohohoho
~
~ 

[failur@localhost test]$ grep -n 'wo' ./t.txt
#1:hello world
#2:wowo
#4:wowowo
#5:wowowowowow

(2)wc

wc [-c -m -l -w ] [/…]

-c 统计比特数

-m 统计字符数量

-l 统计行数

-w 统计单词数量

[failur@localhost test]$ wc t.txt
#不加参数默认 行数 单词数 比特数 文件名
# 8 11 91 t.txt
[failur@localhost test]$ wc -c t.txt
#91 t.txt 
[failur@localhost test]$ wc -m t.txt
#91 t.txt
[failur@localhost test]$ wc -l t.txt
#8 t.txt
[failur@localhost test]$ wc -w t.txt
#11 t.txt

(3)管道符

| 表示将|左测的输出作为右边指令的输入

例1,cat获取文件内容传参给wc

[failur@localhost test]$ cat t.t | wc
#      8      12     112

例2, 管道符嵌套

[failur@localhost test]$ cat t.t | grep "wo" | wc
#cat获取文件内容 grep提取其中含有“wo”的内容,wc进行统计
#      4       5      36

(4)echo

echo [参数]

打印参数的内容

注意 双引号中包含感叹号有奇怪的错误 用单引号则不会出现 原因未知

可以用\,但输出中仍有\存在

例1,双引号导致错误

[failur@localhost test]$ echo "hello world!"
#-bash: !": event not found
[failur@localhost test]$ echo "hello world\!"
#hello world\!
[failur@localhost test]$ echo 'hello world!'
#hello world!

使用反引号`会将‘’中的内容以代码执行

例2,反引号的使用

[failur@localhost test]$ echo pwd
#pwd
[failur@localhost test]$ echo `pwd`
#/home/failur/test

(5)tail

tail [-f -num] [/…]

tail用于获取文件尾部的更改

-f选项用于持续追踪更改

-num用于指定tail获取文件尾部的行数 默认为10

例 获取尾部五行

[failur@localhost test]$ cat t.t
#bin
#boot
#dev
#etc
#home
#lib
#lib64
#media
#mnt
#opt
#proc
#root
#run
#sbin
#srv
#sys
#tmp
#usr
#var
[failur@localhost test]$ tail -5 t.t
#srv
#sys
#tmp
#usr
#var

(6)重定向符

‘>’ ,‘>>’,用于向文件中写入信息

表示将前面的输出覆盖写入后面的文件中,覆盖原文件

表示在后面的文件中追加写入,保留原文件

[failur@localhost test]$ cat t.t
#hello world!

#覆盖写入
[failur@localhost test]$ echo 'wow' > t.t
[failur@localhost test]$ cat t.t
#wow

#追加写入
[failur@localhost test]$ echo 'wow' >> t.t
[failur@localhost test]$ cat t.t
#wow
#wow

6.vim编辑器

(1)工作模式

  • 命令模式(Command mode)
    用于快捷操作文本

‘i’ 进入输入模式

':'进入底线命令模式

  • 输入模式(Insert mode)
    用于编辑文件

'esc’进入命令模式

  • 底线命令模式(Last line mode)
    用于操作文件修改与退出

'esc’进入命令模式

(2)快捷键

  • 命令模式(Command mode)
    dd 删除一行

数字n+dd 删除n行

yy 复制一行

p 粘贴

$ 光标移动至行尾

/ 查找

n向下一个查找

N向上一个查找

u 撤销上一次操作

ctrl+r 退回上一次撤销

  • 底线命令模式(Last line mode)
    w 保存

q!强制退出(不保存)

wq 保存退出

wq!强制保存退出

其他快捷键

Linux vi/vim | 菜鸟教程 (runoob.com)

  • 69
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值