各种命令的含义(cd、ls等)

Linux cd命令用于切换当前工作目录至 dirName(目录参数)

其中 dirName 表示法可为绝对路径或相对路径。若目录名称省略,则变换至使用者的 home 目录 (也就是刚 login 时所在的目录)

另外,"~" 也表示为 home 目录 的意思,"." 则是表示目前所在的目录,".." 则表示目前目录位置的上一层目录。

一、Linux cd命令语法

cd [dirName]

dirName:要切换的目标目录。

二、Linux cd命令实例

跳入test目录:

[root@/root/linuxdaxue.com]#cd testDir/

[root@/root/linuxdaxue.com/testDir]#ls

file1  file2  file3

跳至上层目录

[root@/root/linuxdaxue.com/testDir]#cd ..

[root@/root/linuxdaxue.com]#ls

testDir

跳至上上层目录

[root@/root/linuxdaxue.com/testDir]#cd ../../

[root@/root]#ls

跳入用户主目录

[root@/root/linuxdaxue.com/testDir]#ls

file1  file2  file3

[root@/root/linuxdaxue.com/testDir]#cd ~/

[root@/root]#pwd

/root

使用绝对路径

[root@/root]#cd /root/linuxdaxue.com/testDir

[root@/root/linuxdaxue.com/testDir]#ls

file1  file2  file3

使用环境变量

[root@/root]#cd $TEST_PATH

[root@/root/linuxdaxue.com/testDir]#ls

file1  file2  file3

跳入上次使用目录

[root@/root]#pwd

/root

[root@/root]#cd -

/root/linuxdaxue.com/testDir

[root@/root/linuxdaxue.com/testDir]#

cd命令比较简单,主要用来切换工作目录,希望大家能熟练掌握。



linux命令总结之ls命令

ls命令是linux下最常用的命令之一,lsdos下的dir命令是一样的都是用来列出目录下的文件,下面我们就来一起看看ls的用法

英文全名:List即列表的意思,当我们学习某种东西的时候要做到知其所以然,当你知道了这个东西大概是个什么了以后你的思维就会联想到很多的东西学习的就会很快。

命令格式:

1

ls [选项] [目录名]

  

1. ls -a 列出文件下所有的文件,包括以“.“开头的隐藏文件(Linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录)。
2. ls -l
列出文件的详细信息,如创建者,创建时间,文件的读写权限列表等等。
3. ls -F
在每一个文件的末尾加上一个字符说明该文件的类型。"@"表示符号链接、"|"表示FIFOS"/"表示目录、"="表示套接字。
4. ls -s
在每个文件的后面打印出文件的大小。  size(大小)
5. ls -t
按时间进行文件的排序  Time(时间)
6. ls -A
列出除了"."".."以外的文件。
7. ls -R
将目录下所有的子目录的文件都列出来,相当于我们编程中的“递归”实现
8. ls -L
列出文件的链接名。Link(链接)
9. ls -S
以文件的大小进行排序

  

以上是一些关于ls命令的一些用法,当然还有好多没有列出来,大家可以man一下。有助于学习英文
ls
可以结合管道符”|“来进行一下复杂的操作。比如: ls | less用于实现文件列表的分页,ls计算当前目录下的文件数量:

1

ls -|*|grep "^-"| wc -|

  

最后说一下linux下文件的一些文件颜色的含义(默认,颜色在CRT客户端可以修改)
绿色---->代表可执行文件,(绿色代表通行证的意思)呵呵
红色---->代表压缩文件
深蓝色---->代表目录
浅蓝色----->代表链接文件
灰色---->代表其它的一些文件

常用范例:

例一:列出/home/文件夹下的所有文件和目录的详细资料

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

[root@Gin scripts]# ls -lR

.:

total 20

drwxr-xr-x 2 root root 4096 Jan 26 15:57 a1

drwxr-xr-x 2 root root 4096 Jan 26 15:57 a2

drwxr-xr-x 2 root root 4096 Jan 26 15:57 a3

drwxr-xr-x 2 root root 4096 Jan 26 15:57 a4

drwxr-xr-x 2 root root 4096 Jan 26 15:57 a5

-rw-r--r-- 1 root root    0 Jan 26 15:58 p.py

 

./a1:

total 0

 

./a2:

total 0

 

./a3:

total 0

 

./a4:

total 0

 

./a5:

total 0

例二:列出当前目录中所有以“t”开头的目录的详细内容,可以使用如下命令:

1

2

3

4

5

6

7

8

9

[root@Gin scripts]# ls -l t*

t1:

total 0

 

t2:

total 0

 

t3:

total 0

例三:只列出文件下的子目录

1

2

3

[root@Gin gin]# ls -F ./|grep /$

scripts/

tools/

例四:列出目前工作目录下所有名称是a 开头的文件,愈新的排愈后面,可以使用如下命令:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

[root@Gin scripts]# ll -tr a*

a1:

total 0

 

a3:

total 0

 

a2:

total 0

 

a4:

total 0

 

a5:

total 0

例五:列出目前工作目录下所有档案及目录;目录于名称后加"/", 可执行档于名称后加"*"

1

2

[root@Gin scripts]# ls -AF

a1/  a2/  a3/  a4/  a5/  p.py  s.sh*  t1/  t2/  t3/

例六:计算当前目录下的文件数和目录数

1

2

ls -l * |grep "^-"|wc -l ---文件个数 

ls -l * |grep "^d"|wc -l    ---目录个数

例七: ls中列出文件的绝对路径

1

2

3

4

5

6

7

8

9

10

11

[root@Gin scripts]# ls|sed "s#^#`pwd`#"

/gin/scriptsa1

/gin/scriptsa2

/gin/scriptsa3

/gin/scriptsa4

/gin/scriptsa5

/gin/scriptsp.py

/gin/scriptss.sh

/gin/scriptst1

/gin/scriptst2

/gin/scriptst3

例八:列出当前目录下的所有文件(包括隐藏文件)的绝对路径, 对目录不做递归

1

2

3

4

5

6

7

8

9

10

11

12

[root@Gin scripts]# find $PWD -maxdepth 1|xargs ls -ld

drwxr-xr-x 10 root root 4096 Jan 26 16:04 /gin/scripts

drwxr-xr-x  2 root root 4096 Jan 26 15:57 /gin/scripts/a1

drwxr-xr-x  2 root root 4096 Jan 26 15:57 /gin/scripts/a2

drwxr-xr-x  2 root root 4096 Jan 26 15:57 /gin/scripts/a3

drwxr-xr-x  2 root root 4096 Jan 26 15:57 /gin/scripts/a4

drwxr-xr-x  2 root root 4096 Jan 26 15:57 /gin/scripts/a5

-rw-r--r--  1 root root    0 Jan 26 15:58 /gin/scripts/p.py

-rwxr-xr-x  1 root root    0 Jan 26 16:04 /gin/scripts/s.sh

drwxr-xr-x  2 root root 4096 Jan 26 15:59 /gin/scripts/t1

drwxr-xr-x  2 root root 4096 Jan 26 15:59 /gin/scripts/t2

drwxr-xr-x  2 root root 4096 Jan 26 15:59 /gin/scripts/t3

例九:指定文件时间输出格式

1

2

3

4

5

6

7

8

9

10

11

12

[root@Gin scripts]# ls -tl --time-style=full-iso

total 32

-rwxr-xr-x 1 root root    0 2017-01-26 16:04:10.148346687 +0800 s.sh

drwxr-xr-x 2 root root 4096 2017-01-26 15:59:20.159327457 +0800 t2

drwxr-xr-x 2 root root 4096 2017-01-26 15:59:20.159327457 +0800 t3

drwxr-xr-x 2 root root 4096 2017-01-26 15:59:20.158327471 +0800 t1

-rw-r--r-- 1 root root    0 2017-01-26 15:58:03.089327419 +0800 p.py

drwxr-xr-x 2 root root 4096 2017-01-26 15:57:53.530327465 +0800 a5

drwxr-xr-x 2 root root 4096 2017-01-26 15:57:53.522327531 +0800 a4

drwxr-xr-x 2 root root 4096 2017-01-26 15:57:53.509327419 +0800 a2

drwxr-xr-x 2 root root 4096 2017-01-26 15:57:53.509327419 +0800 a3

drwxr-xr-x 2 root root 4096 2017-01-26 15:57:53.502327284 +0800 a1