Linux中目录与文件

绝对路径与相对路径
  • 绝对路径:由根目录开头,如 /home/mrcode
  • 相对路径:不是由根目录开头的,如 ./mrcode
  • 以下的特殊目录需要着重了掌握
    • .:代表此层目录
    • ..:上一层目录
    • -:前一个工作目录
    • ~:目前用户身份坐在的家目录
    • ~account:表示 account 这个用户的家目录(account 是个账户名称)
目录相关指令
mkdir

新建文件夹,可以同时建立多个文件

[root@localhost demo]# mkdir test
[root@localhost demo]# ls
test
[root@localhost demo]# mkdir test1 test2
[root@localhost demo]# ls
test  test1  test2
cd

切换目录 cd … 返回上一级

[root@localhost demo]# cd ..
[root@localhost lianxi]# cd demo
[root@localhost demo]# 

pwd

查看当前目录

[root@localhost demo]# pwd

/lianxi/demo
ls

查看当前目录选项与参数:

  • a:全部的文件,连同隐藏文件(开头为 .)一起列出来(常用)
  • A:全部的文件,连同隐藏文件(不包括 . 和 … 这两个目录)
  • d:仅列出目录本身,而不是列出目录内的文件数据(常用)
  • f:直接列出结果,而不进行排序(ls 默认以文档名排序)
  • F:根据文件、目录等信息,给予附加数据结构
  • h:将文件容量以人类较易读的方式(例如 GB、KB)列出来
  • i:列出 inode 号码,inode 的意义后续讲解
  • l:长数据串输出,包含文件的属性与权限等数据(常用)
  • n:列出 UID 与 GID 而非使用者与群组的名称(UID 与 GID 会在账户管理中讲解)
  • r:将排序结果反向输出,例如原本文件名由小到大,反向则由大到小

#查看全部文件 连同隐藏文件
[root@localhost lianxi]# ls -a
.          create_file.sh  function          test.txt
..         create_user.sh  monitor_crond.sh  user_pwd.txt
buckup.sh  demo            sanchuang
#查看全部文件  不包括 . 和 .. 这两个目录
[root@localhost lianxi]# ls -A
buckup.sh       create_user.sh  function          sanchuang  user_pwd.txt
create_file.sh  demo            monitor_crond.sh  test.txt
#仅列出目录本身,而不是列出目录内的文件数据
[root@localhost lianxi]# ls -d
.
#直接列出结果,而不进行排序
[root@localhost lianxi]# ls -f
.               buckup.sh         sanchuang       user_pwd.txt
..              monitor_crond.sh  create_file.sh  function
create_user.sh  demo              test.txt
将文件容量以人类较易读的方式(例如 GB、KB)列出来
[root@localhost lianxi]# ls -h
buckup.sh       create_user.sh  function          sanchuang  user_pwd.txt
create_file.sh  demo            monitor_crond.sh  test.txt
#长数据串输出,包含文件的属性与权限等数据(常用)
[root@localhost lianxi]# ls -l
总用量 24
-rw-r--r--. 1 root root 181 2月  23 16:27 buckup.sh
-rw-r--r--. 1 root root 147 2月  19 15:12 create_file.sh
-rw-r--r--. 1 root root 207 2月  20 20:45 create_user.sh
drwxr-xr-x. 3 root root  50 2月  24 23:47 demo
drwxr-xr-x. 2 root root  21 2月  21 16:30 function
-rw-r--r--. 1 root root 180 2月  23 20:55 monitor_crond.sh
drwxr-xr-x. 3 root root  18 2月  18 19:15 sanchuang
-rw-r--r--. 1 root root   4 2月  20 19:52 test.txt
-rw-r--r--. 1 root root 590 2月  20 20:45 user_pwd.txt
#列出 UID 与 GID 而非使用者与群组的名称
[root@localhost lianxi]# ls -n
总用量 24
-rw-r--r--. 1 0 0 181 2月  23 16:27 buckup.sh
-rw-r--r--. 1 0 0 147 2月  19 15:12 create_file.sh
-rw-r--r--. 1 0 0 207 2月  20 20:45 create_user.sh
drwxr-xr-x. 3 0 0  50 2月  24 23:47 demo
drwxr-xr-x. 2 0 0  21 2月  21 16:30 function
-rw-r--r--. 1 0 0 180 2月  23 20:55 monitor_crond.sh
drwxr-xr-x. 3 0 0  18 2月  18 19:15 sanchuang
-rw-r--r--. 1 0 0   4 2月  20 19:52 test.txt
-rw-r--r--. 1 0 0 590 2月  20 20:45 user_pwd.txt
[root@localhost lianxi]# 
​
cp

复制文件或者目录

cp [-adfilprsu] 来源文件(source)目标文件(destination)
  • a:相当于 -dr --preserve=all 的一是一,至于 dr 请参考下列说明;(常用)
  • d:若来源文件为链接文件的属性(link file),则复制链接文件属性而非文件本身
  • f:强制(force)的意思,若目标文件已经存在且无法开启,则移除后再尝试一次
  • i:若目标文件已经存在时,在覆盖时会先询问动作的进行。(常用)
  • l:进行硬式链接(hard link)的链接档的建立,而非复制文件本身
  • p:连同文件的属性(权限、用户、时间)一起复制过去,而非使用默认属性;(备份文件常用)
  • r:递归持续复制,用于目录的复制行为。(常用)
#复制文件
[root@localhost china3]# cp lili.txt hunan
[root@localhost china3]# ls hunan
lili.txt
[root@localhost china3]# ls
boot  hunan  lili.txt  passwd

[root@localhost china3]# cp  /etc/passwd hunan
[root@localhost china3]# ls hunan
lili.txt  passwd

#复制文件并重命名
[root@localhost china3]# cp lili.txt hunan/ll.txt
[root@localhost china3]# ls hunan
lili.txt  ll.txt  passwd

#复制多个文件
[root@localhost china4]# cp dengchao*.txt deng

#复制文件夹
[root@localhost china4]# cp deng yueyang -r

#复制多个文件夹  中间用空格隔开
[root@localhost china4]# cp deng yueyang hunan -r

mv

移动或者重命名,当后面接的文件存在就是移动

可移动一个文件,也可以移动多个文件

  • f:强制,如果目标文件已经存在,不会询问,直接覆盖
  • i:若目标文件已经存在时,就会询问是否覆盖
  • u:若目标已经存在,且 source 比较新,才会功更新该文件
[root@study tmp]# cd /tmp/   
[root@study tmp]# cp ~/.bashrc bashrc
# 创建目录
[root@study tmp]# mkdir test
# 将刚刚拷贝的 bashrc 复制到目录中
[root@study tmp]# mv bashrc test/
# 目录更名
# 其实还有一个指令 rename,该指令专职进行多个文档名同时更名,并非针对单一文件更名
# 与 mv 不同,详细请 man rename
[root@study tmp]# mv test/ test2


rm

删除命令

  • rm -r 递归删除 文件夹中的子文件夹也会删除

  • rm -f 强制删除 不给予提醒

  • rm -rf * 删除文件夹中所有文件 但不包括隐藏文件

  • rm -rf .*删除文件夹中的隐藏文件

[root@localhost demo]# rm -rf *
[root@localhost demo]# ls
[root@localhost demo]# 
回收站实例

编写一个程序(脚本) 实现回收站功能

  1. 临时存放数据
  2. 记录了当时文件的路径 方便恢复 记录当时的路径到一个文件中 ,在恢复的时候查询使用

具体步骤

  1. 首先编写一个脚本mv_back.sh 在脚本新建一个回收站文件夹

/backup 让后通过mv与位置变量将输入的文件或者文件夹移动到/backup文件夹中

#!/bin/bash
#新建回收站目录
mkdir -p /backup

mv  $1 /backup
[root@localhost china3]# 

  1. 为脚本文件添加可执行权限

    [root@localhost china3]# chmod +x mv_back.sh
    
  2. 复制脚本文件到环境变量中

    [root@localhost china3]# cp mv_back.sh /usr/bin
    
  3. 修改rm的别名

    [root@localhost china3]# alias rm='mv_back.sh'
    
文件相关指令
cat

直接查阅一个文件的内容可以使用 cat

  • A:相当于 -vET 的整合选项,可列出一些特殊字符而不是空白
  • b:列出行号,仅针对非空白行做行号显示,空白行不标行号
  • E:将结尾的断行字符 $ 显示出来
  • n:打印出行号(包含空白行)
  • T:将 tab 按键以 ^I 显示出来
  • v:列出一些看不出来的特殊字符
#比较常用的就是如下两个
[root@localhost china3]# cat post_var.sh 
#!/bin/bash

echo "第一个位置变量是$1"
echo "第二个位置变量是$2"
echo "第三个位置变量是$3"
[root@localhost china3]# cat -n post_var.sh 
     1	#!/bin/bash
     2	
     3	echo "第一个位置变量是$1"
     4	echo "第二个位置变量是$2"
     5	echo "第三个位置变量是$3"

more与less

虽然more与less同cat一样可以显示文件内容,但是这两种命令都是可以翻页使用的

more

[root@localhost china3]# more /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
huangjiu:x:1000:1000::/home/huangjiu:/bin/bash
liqiang:x:1001:1001::/home/liqiang:/bin/bash
chenlin:x:7788:1002:sanchuang student:/home/yueyang:/sbin/nologin
liyili:x:7789:7789::/home/liyili:/bin/bash
user20:x:7790:7790::/home/user20:/bin/bash
user00:x:7791:7791::/home/user00:/bin/bash
user01:x:7792:7792::/home/user01:/bin/bash
user02:x:7793:7793::/home/user02:/bin/bash
user03:x:7794:7794::/home/user03:/bin/bash
--More--(52%)    #重点是这里   展示了当前显示了文件的多少内容

在 more 程序中,有几个按键可以按:

  • 空格键(space):向下翻一页
  • Enter:向下翻一行
  • /字符串:在显示的内容中,向下搜索「字符串」这个关键词
  • q:立即离开 more
  • b 或 ctrl+b:向前翻页,只针对文件有用,对管线(管道 |)无用

less同more大致相同,但是more在输出完之后会直接退出文件,但less不会

可以使用的按键和指令有

  • 空格键:向下翻一页
  • pagedown:向下翻一页
  • pageup:向上翻一页
  • /字符串:向下搜索字符串;注意这个斜杠也是需要输入的,不是在 「:」输入,:也和这个是一个功能
  • ?字符串:向上搜索字符串
  • n:重复前一个搜索(与 / 或 ?有关)
  • N:反向的重复前一个搜索
  • g:前进到这个资料的第一行
  • G:前进到这个资料的最后一行去(注意是大写)
  • q:离开 less 这个程序
head 取出文件前几行内容
head [-n number] 文件

-n:后面接数字,表示摘取几行

#默认取出的是十行内容
[root@localhost china3]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
#可以通过添加选项修改取出的行数
[root@localhost china3]# head -5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
tail 取出后面几行
tail [-nf number] 文件

-n :后面接数字,表示显示几行
-f :表示持续侦测后面所接的档名,要等到按下 ctrl+c 才会结束 tail 的侦测

两者可以组合使用

例如想要取文件的第5到10行

[root@localhost china3]# head /china3/test.txt |tail -5
这是第6行
这是第7行
这是第8行
这是第9行
这是第10行

想要取到第8行

[root@localhost china3]# head -8 /china3/test.txt |tail -1
这是第8行

|:管线/管道符,前面的指令所输出的信息,请透过管线交由后续的指令继续使用。后续会详细讲解

cat 和more、less的对比

当读取大文件,我们通常会使用more与less,这是因为cat读取文件的时候是一次性全部读取,会消耗比较多的内存与CPU,但是more与less是慢慢读取,不消耗很多的内存和cpu

touch

修改文件时间或新建文件

新建一个文件或多个文件

[root@localhost demo]# touch test.txt
[root@localhost demo]# ls
test.txt
[root@localhost demo]# touch test{1..10}.txt
[root@localhost demo]# ls
test10.txt  test2.txt  test4.txt  test6.txt  test8.txt  test.txt
test1.txt   test3.txt  test5.txt  test7.txt  test9.txt
[root@localhost demo]# 

du

统计目录及文件的空间占有情况

du 选项 目录和或文件名

  • a 统计时包括所有的文件

  • h 以更易读的字节单位(K,M)等显示信息

  • s 只统计每个参数所占用空间的总大小

[root@localhost demo1]# du -a
4	./test1.sh
4	./test.sh.gz
8	.
[root@localhost demo1]# du -h
8.0K	.
[root@localhost demo1]# du -h test1.sh 
4.0K	test1.sh
[root@localhost demo1]# du -s test1.sh 
4	test1.sh
local

locate 可以查找文件,并同时查看他们的文件权限

[mrcode@study kernel]$ locate crontab
/etc/anacrontab
/etc/crontab
/usr/bin/crontab
/usr/share/doc/man-pages-overrides-7.7.3/crontabs
/usr/share/doc/man-pages-overrides-7.7.3/crontabs/COPYING
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man1p/crontab.1p.gz
/usr/share/man/man4/crontabs.4.gz
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/vim/vim74/syntax/crontab.vim
指令与文件的搜集

which 查看命令的位置

which [-a] command

-a:将所有 PATH 目录中可以找到的指令均累出,而不止第一个被找到的指令名称

whereis 查看命令位置以及man手册的位置

whereis [-bmsu] 文件或目录名
[root@localhost china3]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@localhost china3]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@localhost china3]# 

他们本质上是从PATH环境变量中的目录来查找的,并且只能找出执行文件

另一个比较重要的命令就是find

find
find [path] [option] [action]

与时间有关的参数有 -atime、-ctime、-mtime,以 -mtime 说明:

  • mtime n:在 n 天前的「一天之内」被修改过内容的文件
  • mtime +n:列出在 n 天之前(不含 n 本身)被修改过内容的文件
  • mtime -n:列出在 n 天之内(含 n 天本身)被修改过内容的文件
  • newer file:file 为一个存在的文件,列出比 file 还要新的文件

实践练习

# 将过去系统上 24 小时内有更动过内容(mtime)的文件列出
find / -mtime 0
# 0 表示当前时间,也就是当前时间开始往前 24 小时,也就是 24 小时内被修改过的文件

# 3 天前,24 小时内,如下
find / -mtime 3

# 寻找 /etc 下的文件,如果文件日期比 /etc/passwd 新旧列出
find /etc -newer /etc/passwd

# 列出 4 天内被更动多的文件
find / -mtime -4

  • name filename:查找文件名为 filename 的文件

  • size [-+]SIZE:查找比 SIZE 还要大(+)或则小(-)的文件

    SIZE 支持的单位有:

    • c:byte
    • k:1024 byte

    所以要查找 比 50 KB 还要大的文件,指令为 find /home/ -size +50ks

  • type TYPE:查找文件类型为 TYPE 的。主要有

    • f:一般正规文件
    • b,c:装置文件
    • d:目录
    • l:连接
    • s:socket
    • p:FIFO
  • exec command:command 为其他指令,-exec 后面可再接额外的指令来处理搜索到的结果

  • perm mode:查找文件权限「刚好等于」mode 的文件,mode 为类似 chmod 的属性。

    例如:-rwsr-xr-x 的属性为 4755

  • perm -mode:查找文件权限「必须要全部包括 mode 的权限」的文件

    例如:查找 -rwxr–r-- ,即 0744 的文件,使用 -perm -0744

  • perm /mode:查找文件权限「包含任意 mode 的权限」的文件

    例如:-rwxr-xr-x,即 -perm /755 时,但一个属性属性为 -rw------ 也会被列出来, 因为他有 -rw 的属性存在

实践练习

# 找出文件名为 passwd 的文件
find / -name passwd

# 找出包含了 passwd 关键词的文件
find / -name "*passwd*"
#查找文件权限为7000的
find / -perm /7000
# 7000 就是 ---s--s--t
#将最近3小时内/lianxi 目录下文件大小大于10K的文件移动到/back目录下

find /lianxi -mmin +180 -type f -exec cp{} /find \;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值