Linux文件目录管理命令

本文详述了Linux系统中进行目录管理和文件管理的基本操作,包括切换目录、创建目录、查看文件、统计文件大小、创建文件、复制、移动、删除文件及目录,以及统计文件行数、查看文件内容等实用技巧,旨在帮助用户熟练掌握Linux系统中的文件和目录管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、目录管理

1)切换目录

cd [目录名称]

[root@tan student]# cd /etc/sysconfig/
[root@tan sysconfig]# pwd
/etc/sysconfig
[root@tan sysconfig]# cd
[root@tan ~]# pwd
/root
[root@tan ~]# cd /home/
[root@tan home]# pwd
/home
[root@tan home]# cd /etc/sysconfig/
[root@tan sysconfig]# pwd
/etc/sysconfig

[root@tan student]# cd /home/
[root@tan home]# ls
student
[root@tan home]# cd student/
[root@tan student]# pwd
/home/student

[root@tan student]# cd …
[root@tan home]# pwd
/home
[root@tan home]# cd …
[root@tan /]# pwd
/

2) 创建目录

mkdir [option] <目录名称>

[root@tan network-scripts]# mkdir /tmp/bj

[root@tan ~]# mkdir sh

[root@tan ~]# mkdir /linux

选项:
-p 层级创建目录

[root@tan ~]# mkdir -p /uplooking/linux

[root@tan ~]# mkdir -p /caiwu/2017-{1…12} 花括号展开创建

2、文件管理

1)查看文件

ls [option] [目录名称]

[root@tan ~]# ls
anaconda-ks.cfg sh

[root@tan ~]# ls /tmp/

选项:
-l 以详细信息的方式显示

[root@tan ~]# ls -l
总用量 4
-rw-------. 1 root root 1227 2月 26 01:08 anaconda-ks.cfg

  • 代表文件
    d 代表目录
    l 代表软连接文件(link)
    b 代表设备文件(block)硬盘、硬盘分区、U盘、光盘
    c 代表字符设备文件(character)键盘、鼠标、显示器

[root@tan ~]# ls -l /etc/passwd
-rw-r–r--. 1 root root 846 2月 26 01:07 /etc/passwd

-h

[root@tan ~]# ls -lh /etc/passwd
-rw-r–r--. 1 root root 846 2月 26 01:07 /etc/passwd

-d   显示目录自身的信息

[root@tan ~]# ls -ldh /etc/
drwxr-xr-x. 74 root root 8.0K 2月 26 01:38 /etc/

-a 显示所有隐藏文件(以.开头的文件)

[root@tan ~]# ls -a

-t   按文件修改时间降序排列

[root@tan ~]# ls -lt

-S   按文件大小降序排列

[root@tan ~]# ls -lhS
总用量 4.0K
-rw-------. 1 root root 1.2K 2月 26 01:08 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 2月 26 02:23 sh

2)统计文件目录大小

[root@tan ~]# du -sh /etc/passwd
4.0K /etc/passwd

[root@tan ~]# du -sh /etc/
31M /etc/

[root@tan ~]# du -ah /etc/

3)创建文件

touch <文件名>

[root@tan ~]# touch /tmp/1.txt

[root@tan ~]# touch /tmp/{1…100}.mp3 花括号展开创建

[root@tan ~]# touch /tmp/{1,3,9}.jpg 花括号展开创建

[root@tan ~]# touch /tmp/{a,b,c}.txt 花括号展开创建

[root@tan ~]# mkdir -p /caiwu/2017-{1…12} 花括号展开创建

date 显示时间
[root@tan ~]# date
2020年 02月 26日 星期三 04:45:10 CST

[root@tan ~]# date +%F 格式
2020-02-26

[root@tan ~]# date +%T 时分秒
04:48:20

[root@tan ~]# date +%H 小时
04

[root@tan ~]# date +%M 分钟
48

[root@tan ~]# date +%m 月份
02

[root@tan ~]# date +%F-%T 年月日时分秒
2020-02-26-04:52:37

[root@tan ~]# date
2020年 02月 26日 星期三 05:09:18 CST
[root@tan ~]# date +%H
05
[root@tan ~]# date +%M
09
[root@tan ~]# date +%S
37
[root@tan ~]# date +%H-%M-%S 将时分秒连起来的方式
05-10-04

命令引用:

方法1:$(command)

[root@tan ~]# touch /tmp/$(date +%F-%T).log

方法2:反引号

[root@tan ~]# touch /tmp/date +%F-%T.log

#man [命令] 查看命令的使用
按Q退出

4)复制文件目录

cp [option] 源文件 目录文件

[root@tan ~]# cp /linux/1.txt /windows/

[root@tan ~]# cp /linux/2.txt /windows/2.jpg 复制并重命名格式
[root@tan ~]# ls /windows/
1.txt 2.jpg

选项:

-r 复制目录时

[root@tan ~]# cp -r /linux /windows/
[root@tan ~]# ls /windows/
1.txt 2.jpg linux

-f force强制覆盖 (谨慎不要使用)

[root@tan ~]# cp -fn /linux/1.txt /windows/

5)移动文件目录

mv 源文件 目录文件

重目录移动改名:
[root@tan ~]# mv /linux/3.txt /linux/33.txt

6)删除文件目录 (谨慎使用rm,可用mv 移到自己的垃圾目录去)

rm [option] 文件名称

[root@tan ~]# rm /linux/33.txt
rm:是否删除普通空文件 “/linux/33.txt”?yes

选项:
-r 删除目录时

[root@tan ~]# rm -r /windows/

-f 强制删除目录 (谨慎不要使用)

[root@tan ~]# rm -rf /linux/

  •     以下所有东西    (不要用!)
    

[root@tan ~]# rm -rf /tmp/*

7) 统计文件的行数

wc 统计一个文件的有多少行数,多少单词,多少字符。

[root@tan ~]# wc /etc/passwd
19 27 846 /etc/passwd
行数 单词 字符

选项:
-l 统计一个文件的行数

[root@tan ~]# wc -l /etc/passwd
19 /etc/passwd

8)查看文件内容 ---- cat/more/less/head/tail 只能查看文本文件

cat 查看一些较少的文件内容

[root@tan ~]# cat /etc/fstab

[root@tan ~]# cat -n /etc/fstab (-n 加行号)

查看系统版本信息
[root@tan ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

==more 或 less == (查看一些较多的文件内容)分页显示文件内容
翻页按回车或空格键,按Q退出
[root@tan ~]# more /usr/share/dict/words

less 按上可以翻回去。
[root@tan ~]# ;less /usr/share/dict/words

head 或 tail (查看前几行和后几行内容)

[root@tan ~]# head -n 3 /etc/passwd -n >>>指定查看前几行内容
[root@tan ~]# head /etc/passwd >>>默认查看前十行内容

[root@tan ~]# tail -n 3 /etc/passwd -n >>>指定查看后几行内容
[root@tan ~]# tail /etc/passwd >>>默认查看后十行内容

9)# file 查看文件的类型

[root@tan ~]# file /etc/passwd
/etc/passwd: ASCII text 意思是文本文件

10)# which 查看系统命令所在路径

[root@tan ~]# which ls
alias ls=‘ls --color=auto’
/usr/bin/ls

管道符的使用: | 连接多个命令来实现

[root@tan ~]# head -n 5 /etc/passwd

使用管道符查看前5行的第5行 (结合使用)
[root@tan ~]# head -n 5 /etc/passwd | tail -n 1
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin

[root@tan ~]# ls -lhS /etc/ | head -n 5 显示文件最大的前五行

[root@tan ~]# ls -lt /etc/ | head -n 11 显示文件最近修改的时间的前十行

11)查找文件目录

find 路径 查找方式

按文件名称查找

[root@tan ~]# find /etc/ -name “*.conf”

按文件名称查找并统计多少行

[root@tan ~]# find /etc/ -name “*.conf” | wc -l
90

按文件的大小查找#-size
比如查 /etc下大小1M的文件: ( M G k)
[root@tan ~]# find /etc/ -size +1M
[root@tan ~]# find /etc/ -size -1M
[root@tan ~]# find /etc/ -size +500k

按文件修改时间查找#-mtime
[root@tan ~]# find / -mtime +7 >>>7天前修改的
[root@tan ~]# find / -mtime -7 >>>7天内修改的

按文件类型查找#-type

  • 代表文件
    d 代表目录
    l 代表软连接文件(link)
    b 代表设备文件(block)硬盘、硬盘分区、U盘、光盘
    c 代表字符设备文件(character)键盘、鼠标、显示器

[root@tan ~]# find /dev/ -type b
[root@tan ~]# find /dev/ -type l

复合条件查找 -a and并且

[root@tan ~]# find / -mtime -7 -a -size +100k

查找文件并执行后面的操作:

查找文件并执行所查到的文件全删除:
[root@tan ~]# find /bj/ -name “*.txt” -exec rm -rf {} ;

查找文件并执行所查到的文件拷贝到某文件里去:
[root@tan ~]# find /bj/ -name “.txt" -exec cp {} /tmp ;
再次删除
[root@tan ~]# find /tmp/ -name "
.txt” -exec rm -rf {} ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值