Linux文件与目录管理命令

《linux就该这么学》笔记 (6)
在linux系统的日常运维工作中,还需要掌握对文件的创建、修改、复制、剪切、更名、删除等操作。

1 touch

touch用于创建空白文件或者设置文件的时间。touch创建文件非常简单,有难度的操作主要体现在设置文件内容的修改时间上(mtime)、文件权限或属性的更改时间(ctime)与文件读取时间上。

touch [选项] [文件]

[root@linuxpronbe home]# ls -l test.txt 
-rw-r--r--. 1 root root 8 Jul 22 23:18 test.txt
[root@linuxpronbe home]# echo "hello world" >> test.txt 
[root@linuxpronbe home]# cat test.txt 
abcdefg
hello world
[root@linuxpronbe home]# touch -d "2012-05-04 15:00" test.txt 
[root@linuxpronbe home]# ls -l test.txt 
-rw-r--r--. 1 root root 20 May  4  2012 test.txt

2 mkdir

mkdir(make directories)命令用于创建空白的目录,目录就是windows里的文件夹,创建的一定是目录,即使有后缀。除了能创建单个空白目录外,mkdir还可以结合-p参数来递归创建出具有嵌套层关系的文件目录。

makir [参数] [目录]

[root@linuxpronbe Desktop]# mkdir test1
[root@linuxpronbe Desktop]# ls 
test1
[root@linuxpronbe Desktop]# mkdir -p test1/test2/test3 
[root@linuxpronbe Desktop]# cd test1/test2/test3/

3 cp

cp(copy files and directories)用于复制文件或目录。 在linux系统中, 复制操作具体分为3种情况:

  1. 如果目标文件是目录,则会把源文件复制到目录中
  2. 如果目标文件是普通文件,则询问是否覆盖
  3. 如果目标文件不存在, 则执行正常的复制操作
[root@linuxpronbe home]# touch test.txt 
[root@linuxpronbe home]# ls
redhat  test.txt
[root@linuxpronbe home]# cp test.txt newtest.txt 
[root@linuxpronbe home]# ls
newtest.txt  redhat  test.txt

4 mv

mv( move or rename files)命令用于剪切文件或将文件重命名,剪切操作与复制操作不同的是,剪切会把源文件删除。只保留剪切后的文件, 如果在同一个目录对一个文件进行剪切操作,那么就是重命名。

mv [参数] 源文件 [目标路径|目标文件名]

[root@linuxpronbe home]# ls 
test.txt
[root@linuxpronbe home]# mv test.txt newname.txt 
[root@linuxpronbe home]# ls
newname.txt 

5 rm

rm命令用于删除文件或目录, 在linux系统中删除文件时, 系统会默认向您询问是否要执行删除操作,如果不想总数看得到这种确认信息, 可以在rm命令后跟上 -f参数来强制删除。 如果想删除一个目录,需要加 -f才可以,否则提示目录无法删除。

rm [参数] 文件

[root@linuxpronbe home]# ls 
newname.txt  redhat 
[root@linuxpronbe home]# rm newname.txt  
rm: remove regular empty file ‘newname.txt’? y
[root@linuxpronbe home]# ls
redhat 

6 dd

dd命令用于按照指定大小和个数的数据块来复制或转移文件,是一个比较重要而且比较有特色的命令。 它能让用户指定大小和个数的数据块来复制文件的内容。当然如果愿意的话,还可以在复制过程中转换其中的数据。Linux系统中有一个名为 /dev/zero 的设备文件,这个文件不会占系统储存空间,但却可以提供无穷无尽的数据,因此可以使用它作为dd命令的输入文件, 来生成一个指定大小的文件。

dd [参数]

[root@linuxpronbe //]# dd if=/dev/zero of=560_file count=1 bs=560M
1+0 records in
1+0 records out
587202560 bytes (587 MB) copied, 9.69299 s, 60.6 MB/s

7 file

file用于查看文件的类型, 由于文本、目录、设备等所有这些一切都统称为文件,而我们又不能单凭后缀就直到具体的文件类型,这时就需要使用file命令来查看文件类型了。

[root@linuxpronbe home]#  mkdir test.txt 
[root@linuxpronbe home]# file test.txt
test.txt: directory
[root@linuxpronbe home]# touch test1.txt 
[root@linuxpronbe home]# file test1.txt 
test1.txt: empty
[root@linuxpronbe home]# vim test1.txt 
[root@linuxpronbe home]# file test1.txt 
test1.txt: ASCII text
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值