格式:指令 -[参数] [目录]
查看当前工作路径
改变 shell 工作目录
列出当前目录下的文件和目录
ls -[参数] [目录]
ls的参数均是短命令,可以使用-后添加多个参数
可选参数:
-l: 以长格式显示
[root@localhost test]# ls
demo1 demo2 demo3 demo4
[root@localhost test]# ls -l
总用量 0
d-------wx. 2 redhat root 6 11月 3 11:37 demo1
drwxrwxrwx. 6 redhat redhat 58 11月 8 10:46 demo2
drwxrwxrwx. 2 redhat redhat 6 11月 3 11:37 demo3
drwxrwxrwx. 5 redhat redhat 45 11月 8 10:54 demo4
### 如果目录下文件过多,此时我们只想查看某个文件的时间戳,大小等的信息,可以使用以下命令。
[redhat@localhost test]$ ls a.txt -l
-rw-r--r--. 1 root root 3781 12月 1 12:12 a.txt
-a:显示所有子目录和文件的信息,包括隐藏文件(开头为.的文件)
[root@localhost test]# ls
demo1 demo2 demo3 demo4
[root@localhost test]# ls -a
. .. .a.txt demo1 demo2 demo3 demo4
// 使用参数 -a 后可以看到当前目录 . 和上级目录 .. 以及隐藏文件 .a.txt
-A:同a,但不显示当前目录 “.” 和上级目录"…"信息
[root@localhost test]# ls -a
. .. .a.txt demo1 demo2 demo3 demo4
[root@localhost test]# ls -A
.a.txt demo1 demo2 demo3 demo4
-d:显示目录本身属性
[root@localhost test]# ls -d
.
//-d 参数可以结合 -l一起使用,查看当前目录的相关属性
[root@localhost test]# ls -dl
drwxrwxrwx. 7 redhat redhat 72 12月 1 12:00 .
-h:以更易读的方式显示文件信息(K,M)
[root@localhost test]# ls -l
总用量 4
-rw-r--r--. 1 root root 3781 12月 1 12:12 a.txt
d-------wx. 2 redhat root 6 11月 3 11:37 demo1
drwxrwxrwx. 6 redhat redhat 58 11月 8 10:46 demo2
drwxrwxrwx. 2 redhat redhat 6 11月 3 11:37 demo3
drwxrwxrwx. 5 redhat redhat 45 11月 8 10:54 demo4
[root@localhost test]# ls -hl
总用量 4.0K
-rw-r--r--. 1 root root 3.7K 12月 1 12:12 a.txt # 可以看到这里是3.7k
d-------wx. 2 redhat root 6 11月 3 11:37 demo1
drwxrwxrwx. 6 redhat redhat 58 11月 8 10:46 demo2
drwxrwxrwx. 2 redhat redhat 6 11月 3 11:37 demo3
drwxrwxrwx. 5 redhat redhat 45 11月 8 10:54 demo4
创建新的目录
[redhat@localhost test]$ mkdir -m 777 -p demo5/a1/a2/a3
[redhat@localhost test]$ ls -l
drwxr-xr-x. 3 redhat redhat 16 12月 6 10:33 demo5
[redhat@localhost demo5]$ ls -l
drwxr-xr-x. 3 redhat redhat 16 12月 6 10:33 a1
[redhat@localhost a1]$ ls -l
drwxr-xr-x. 3 redhat redhat 16 12月 6 10:33 a2
[redhat@localhost a1]$ cd a2/
drwxrwxrwx. 2 redhat redhat 6 12月 6 10:33 a3
# 只会修改最后一个的权限,前面的权限依旧为默认的
删除文件夹
[redhat@localhost test]$ rmdir -p demo5/a1/a2/a3/
[redhat@localhost test]$ ls
a.txt demo1 demo2 demo3 demo4
统计目录及文件占用情况
[redhat@localhost test]$ sudo du -hs
4.0K . #当前文件夹
创建一个文件;如果存在修改时间戳。
[redhat@localhost test]$ touch a1.txt
[redhat@localhost test]$ ll
-rw-r--r--. 1 redhat redhat 0 12月 6 10:59 a1.txt
[redhat@localhost test]$ touch a1.txt
[redhat@localhost test]$ ll
-rw-r--r--. 1 redhat redhat 2554 12月 6 11:00 a1.txt
查看指定文件的类型
[redhat@localhost test]$ file a.txt
a.txt: UTF-8 Unicode text
[redhat@localhost test]$ file .a
.a: directory
cp [可选参数] 源文件/目录 目的文件/目录
移动文件/目录。还可用来重命名
[redhat@localhost test]$ ls
a1.txt a.txt demo1 demo2 demo3 demo4
[redhat@localhost test]$ mv a.txt ./demo4 # 移动
[redhat@localhost test]$ ls
a1.txt demo1 demo2 demo3 demo4
[redhat@localhost test]$ ls demo4
a.txt demo4-1 demo4-2 demo4-3
[redhat@localhost test]$
删除文件夹后目录的
用于查找命令的,并且显示所在位置
[redhat@localhost test]$ which ll
alias ll='ls -l --color=auto'/usr/bin/ls
[redhat@localhost test]$ which ls
alias ls='ls --color=auto'/usr/bin/ls
查找文件或目录
find [查找范围] [条件]
创建链接(Link)
格式: ln [-s 不选该参数即为硬链接] 源文件 链接文件或目标目录
修改源文件后链接文件都会修改,硬链接是使用同一个数据,但是是两个一样的inode。
查看文件内容
同上面相反,反着显示,从最后一行显示,是按行算的!!
都是以全屏的形式进行分页查看
格式:head/tail [-n] xx.txt
在文件中查找字符串
grep [参数] string a.txt
tar [参数] 归档名称 源文件
以上5个只能5选一使用
常用组合命令
tar -zxf aaa.tar.gz # 解压aaa.tar.gz
tar -zcf demo.tar.gz demo4 # 创建压缩包demo.tar.gz 使用demo4的内容创建
tar -tf demo.tar.gz # 在未解压的情况下查看压缩包
tar -xvf file.tar // 解压 tar包
tar -zxvf file.tar.gz // 解压 tar.gz
tar -jxvf file.tar.bz2 // 解压 tar.bz2
tar -Zxvf file.tar.Z // 解压 tar.Z
tar详解
更改文件权限
用户,用户组。一个用户可以属于多个用户组。
属主:所属的用户,文档所有者,这是一个账户,这是一个人
属组:所属的用户组,这是一个组
使用 ll 或 ls-al 命令来查看文件相关信息
drwxrwxrwx. 2 redhat redhat 6 11月 3 11:37 demo1
drwxrwxrwx. 2 redhat redhat 6 11月 3 11:37 demo2
drwxrwxrwx. 2 redhat redhat 6 11月 3 11:37 demo3
drwxrwxrwx. 2 redhat redhat 6 11月 3 11:37 demo4
第一个表示属性,是文件还是目录。
后面9个字符,3个一组,分别表示rwx
chgrp [-R] 属组名 文件名
-R 表示递归更改,就是把文件夹里的文件全改了
chgrp root demo1 //把demo1所属的组改成root
chown [–R] 属主名 文件名
chown [-R] 属主名:属组名 文件名
chown root demo1 //把文件demo1所属的主改成root
chown root:root demo2 //把文件demo2的用户组和所有者都改成root
Linux 文件的基本权限就有九个,分别是 owner/group/others(拥有者/组/其他) 三种身份各有自己的 read/write/execute 权限。
chmod [-R] -777 demo1
用户身份就三个,user,group,other, + 代表添加权限,-代表删除权限,=表示设置权限
chmod [-R] u=rwx,g-w,o+r demo1
查看所有环境变量
[redhat@localhost test]$ env
SHELL=/bin/bash
HISTCONTROL=ignoredups
HISTSIZE=1000
HOSTNAME=localhost
有时环境变量过多,可使用grep来筛选
[redhat@localhost test]$ env|grep HOME
HOME=/home/redhat
查看环境变量的内容
echo $环境变量名称
[redhat@localhost test]$ echo $HOME
/home/redhat
设置环境变量
export 变量名=‘值’
在退出shell后就会失效,并非永久
[redhat@localhost test]$ export h="hello"
[redhat@localhost test]$ echo $h
hello
用于显示所有本地定义的shell变量
用于清除环境变量。如果未指定值,该变量被置为NULL
history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...]-c 删除所有条目从而清空历史列表。-d 偏移量 从指定位置删除历史列表。负偏移量将从历史条目末尾开始计数-a 将当前会话的历史行追加到历史文件中-n 从历史文件中读取所有未被读取的行并且将它们附加到历史列表-r 读取历史文件并将内容追加到历史列表中-w 将当前历史写入到历史文件中-p 对每一个 ARG 参数展开历史并显示结果,而不存储到历史列表中-s 以单条记录追加 ARG 到历史列表中
下一篇:Docker基本使用