Linux文件和目录基本操作

目录

文件目录操作

语法

1.mkdir(创建文件夹)

2. ls -

3. pwd

4. cd(切换目录)

5. rmdir(删除空的目录)rmdir [-p] 目录名称

6. touch(创建新文件)

7. cp(复制文件和目录)

8. rm (移除文件或文件夹)

9.mv (移动文件与目录,或修改名称)

文件目录操作


打开Terminal,依次输入下列命令
root@cg:~/Desktop# cd ~/base
root@cg:~/base# mkdir dirandfile
root@cg:~/base# cd dirandfile
root@cg:~/base/dirandfile#

语法

1.mkdir(创建文件夹)

mkdir[-mp] 目录名称

选项和参数:

  • -m :配置文件的权限

  • -p :递归地创建目标目录及全部的上层目录

2. ls -

ls [-aAdfFhilnrRSt] 目录名称
ls [--color={never,auto,always}] 目录名称
ls [--full-time] 目录名称

选项和参数:

  • -a :全部的文件,包括隐藏文件

  • -d :仅列出目录

  • -l :列出详细的文件属性

示例:

root@cg:~/base/dirandfile# mkdir test 这里创建了一个名为test的文件夹
root@cg:~/base/dirandfile# mkdir test1/test2/test3/test4
mkdir: cannot create directory 'test1/test2/test3/test4': No such file or directory
root@cg:~/base/dirandfile# mkdir -p test1/test2/test3/test4
root@cg:~/base/dirandfile#

有了-p选项之后,可以递归地创建出所需要目录的上层目录

root@cg:~/base/dirandfile# ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb 16 14:42 test
drwxr-xr-x 3 root root 4096 Feb 16 14:43 test1
root@cg:~/base/dirandfile# mkdir -m 711 test2
root@cg:~/base/dirandfile# ls -l
total 12
drwxr-xr-x 2 root root 4096 Feb 16 14:42 test
drwxr-xr-x 3 root root 4096 Feb 16 14:43 test1
drwx--x--x 2 root root 4096 Feb 16 14:44 test2
root@cg:~/base/dirandfile#

如果没有设置-m选项,那么该目录将被设置为默认权限drwxr-xr-x

在上例中该目录通过-m 711设置为了drwxr-sr-x权限

3. pwd

是 Print Working Directory 的缩写,也就是显示目前所在目录的命令。

示例:
显示出当前目录的绝对路径:

root@cg:~/base/dirandfile# pwd
/headless/base/dirandfile
root@cg:~/base/dirandfile#

4. cd(切换目录)

cd是Change Directory的缩写,这是用来变换工作目录的命令。
cd [相对路径或绝对路径]

示例:

使用绝对路径切换到 test2 目录

root@cg:~/base/dirandfile# cd /headless/base/dirandfile/test2

..   表示回到当前目录的上一级目录

cd ..

使用相对路径切换到 test2 目录

~表示自己的家目录,cd ~表示回到家目录,亦即是 /headless 这个目录

root@cg:~/base/dirandfile# cd /headless/base/dirandfile/test2
root@cg:~/base/dirandfile/test2# cd ..
root@cg:~/base/dirandfile# cd ./test2
root@cg:~/base/dirandfile/test2# cd ~
root@cg:~# pwd
/headless

5. rmdir(删除空的目录)
rmdir [-p] 目录名称

选项与参数:

-p :连同上一级『空的』目录也一起删除  

非空目录无法删除

root@cg:~/base/dirandfile# rmdir test1
rmdir: failed to remove 'test1': Directory not empty

递归地删除test1/test2/test3/test4

root@cg:~/base/dirandfile# rmdir -p test1/test2/test3/test4
root@cg:~/base/dirandfile# ls -l
total 4
drwx--x--x 2 root root 4096 Feb 16 14:44 test2
root@cg:~/base/dirandfile#

6. touch(创建新文件)

一创建新的空文件、二修改已存在文件的时间戳
touch [-acdfmrt] 文件名称

选项与参数:

  • -a 或--time=atime或--time=access或--time=use 只更改存取时间。

  • -c 或--no-create 不建立任何文档。

  • -d 使用指定的日期时间,而非现在的时间。

  • -f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。

  • -m 或--time=mtime或--time=modify 只更改变动时间。

  • -r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。

  • -t 使用指定的日期时间,而非现在的时间。

1. 创建新文件

root@cg:~/base/dirandfile# touch file1.txt
root@cg:~/base/dirandfile# ls -l
total 4
-rw-r--r-- 1 root root    0 Feb 16 15:00 file1.txt
drwx--x--x 2 root root 4096 Feb 16 14:44 test2

2. 修改文件的时间戳

root@cg:~/base/dirandfile# touch -t 202002161300 file1.txt
root@cg:~/base/dirandfile# ls -l
total 4
-rw-r--r-- 1 root root    0 Feb 16 13:00 file1.txt
-rw-r--r-- 1 root root    0 Feb 16 15:04 file2.txt
drwx--x--x 2 root root 4096 Feb 16 14:44 test2
root@cg:~/base/dirandfile#

7. cp(复制文件和目录)

cp 文件名加后缀名  文件夹名(将文件复制到文件夹)

选项与参数:

  • -a:相当于同时使用-pdr

  • -d:若源文件为链接文件,则复制链接文件属性而非文件本身

  • -f:强制复制文件或目录,无论目标文件或目录是否已经存在

  • -i:若目标文件已存在,覆盖时会先询问

  • -l:复制为硬链接

  • -p:连同文件的属性一起复制过去

  • -r:递归地复制各层目录

  • -s:复制为软链接

  • -u:若目标文件比源文件旧才升级目标文件

root@cg:~/base/dirandfile# cp file1.txt test2
root@cg:~/base/dirandfile# cp -i file1.txt test2
cp: overwrite 'test2/file1.txt'? n
root@cg:~/base/dirandfile#

!!! cd跟cp很容易输错,如果执行结果不对,就看看是不是字母输错了!!!

8. rm (移除文件或文件夹)

rm [-fir] 文件或目录

选项与参数:

  • -f :强制执行删除操作,不发出警告;

  • -i :删除前会请求确认

  • -r :递归删除,用于删除多层目录

root@cg:~/base/dirandfile# rm -i file1.txt 
rm: remove regular empty file 'file1.txt'? y
root@cg:~/base/dirandfile# ls -l
total 4
-rw-r--r-- 1 root root    0 Feb 16 15:04 file2.txt
drwx--x--x 2 root root 4096 Feb 16 15:06 test2
root@cg:~/base/dirandfile#

9.mv (移动文件与目录,或修改名称)

mv [-fiu] 源 目标
mv [options] 源1 源2 源3 .... 目标

选项与参数:

  • -f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;

  • -i :若目标文件已存在,覆盖时会先询问

  • -u :若目标文件已经存在,且 source 比较新,才会升级 (update) 

root@cg:~/base/dirandfile# mv file2.txt test
root@cg:~/base/dirandfile# ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb 16 15:09 test
drwx--x--x 2 root root 4096 Feb 16 15:06 test2
root@cg:~/base/dirandfile# mv test test1
root@cg:~/base/dirandfile# ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb 16 15:09 test1
drwx--x--x 2 root root 4096 Feb 16 15:06 test2
root@cg:~/base/dirandfile# mv test2 test1
root@cg:~/base/dirandfile# ls -l
total 4
drwxr-xr-x 3 root root 4096 Feb 16 15:10 test1
root@cg:~/base/dirandfile# ls -l test1
total 4
-rw-r--r-- 1 root root    0 Feb 16 15:04 file2.txt
drwx--x--x 2 root root 4096 Feb 16 15:06 test2
root@cg:~/base/dirandfile#

总:mkdir(创建文件夹)、ls (列出目录)、pwd(显示当前所在的目录)、cd(切换目录)、rmdir(删除空的目录)、touch(创建新文件)、cp(复制文件和目录)、rm (移除文件或目录)、mv (移动文件与目录,或修改名称)

自己试一试吧:创建一个txt文件,和一个与文件同名的文件夹,再将文件复制到该文件夹下面。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值