Linux基础知识--目录操作

目录操作命令:

pwd、cd、ls、mkdir、du等

pwd:查看当前工作目录

[root@localhost ~]# pwd
/root

cd:切换工作目录

cd [目录位置]

[root@localhost ~]# cd ..
[root@localhost /]# pwd
/
[root@localhost /]# cd root
[root@localhost ~]# pwd
/root

补充:绝对路径和相对路径

[root@localhost /]# pwd
/
[root@localhost /]# cd root
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /home
[root@localhost home]# pwd
/home

可以看到从根目录进入到root目录不用加反斜杠,进入到root目录下,再进入home目录要加反斜杠。原因在于root目录和home目录都在根目录下,从根目录进入root目录,root就在当前目录下,所以不用加反斜杠,这叫做相对路径;但是在root目录下进入home目录,home目录不在root目录下,所以要用绝对路径进入,需要加反斜杠。要从root目录下进入home目录,原理上来讲是要先回到根目录,然后在进入到home目录。加了反斜杠就表示从根目录开始。

cd命令后面什么都不加,回到用户的根目录

cd ..回到上一层目录

ls:显示目录的内容,显示这个目录下有什么内容

ls -l 以长格式显示

ls -a显示所有子目录和文件信息,包括隐藏文件

ls -d显示目录本身属性

ls -A类似于a但是不显示“.”和“..”目录的信息

ls -h以更易读懂的字节单位(K、M)等显示信息

ls -R递归显示内容

ls --color:以颜色区分不同类型文件

[root@localhost home]# ls -l
total 16
drwx------ 4 oracle oinstall 4096 Jan 25 05:36 oracle
drwxr-xr-x 2 root   root     4096 Jan 25 03:59 rpm
[root@localhost home]# ls -a
.  ..  oracle  rpm
[root@localhost home]# ls -al
total 32
drwxr-xr-x  4 root   root     4096 Jan 25 04:27 .
drwxr-xr-x 25 root   root     4096 Jan 26 18:57 ..
drwx------  4 oracle oinstall 4096 Jan 25 05:36 oracle
drwxr-xr-x  2 root   root     4096 Jan 25 03:59 rpm
[root@localhost home]# ls -d /home
/home
[root@localhost home]# ls -lh
total 16K
drwx------ 4 oracle oinstall 4.0K Jan 25 05:36 oracle
drwxr-xr-x 2 root   root     4.0K Jan 25 03:59 rpm

通配符和占位符

[root@localhost /]# cd root
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog
[root@localhost ~]# ls install*
install.log  install.log.syslog
[root@localhost ~]# ls install.???
install.log

使用*通配符是指查找什么什么开头的所有文件

使用?占位符,三个占位符表示后面有三个字符

创建目录,mkdir

mkdir -p 创建嵌套目录

[root@localhost home]# mkdir test
[root@localhost home]# ls
oracle  rpm  test
[root@localhost home]# mkdir -p test1/subtest1
[root@localhost home]# ls
oracle  rpm  test  test1
[root@localhost home]# ls -R test1
test1:
subtest1

test1/subtest1:

可以看到home目录下有test1和test,test1下有subtest1目录

du:统计目录及文件占用空间的情况

格式:du [选项]... [目录或文件名]

  -a 统计所有文件,不仅仅包括目录,不要与-s结合

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

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

[root@localhost home]# du -a /root
8       /root/.bashrc
8       /root/.cshrc
4       /root/.viminfo
8       /root/.bash_history
40      /root/install.log
8       /root/.bash_logout
8       /root/anaconda-ks.cfg
8       /root/install.log.syslog
4       /root/.lesshst
8       /root/.tcshrc
4       /root/.bash_profile
116     /root
[root@localhost home]# du -s
723492  .
[root@localhost home]# du -sh /root
116K    /root
[root@localhost home]# du -ah /root
8.0K    /root/.bashrc
8.0K    /root/.cshrc
4.0K    /root/.viminfo
8.0K    /root/.bash_history
40K     /root/install.log
8.0K    /root/.bash_logout
8.0K    /root/anaconda-ks.cfg
8.0K    /root/install.log.syslog
4.0K    /root/.lesshst
8.0K    /root/.tcshrc
4.0K    /root/.bash_profile
116K    /root

分别统计了home目录的大小、以字节单位表示root目录大小、以字节单位显示root目录下所有文件所占空间

猜你喜欢

转载自www.cnblogs.com/bigbigtong/p/10323252.html