红联Linux门户
Linux帮助

Linux的基本使用-基本命令

发布时间:2008-03-22 21:28:42来源:红联作者:Totoobo
1、显示当前工作目录中的内容
要想知道当前的工作目录中有些什么文件或目录,在Windows的“命令提示符”中要使用“dir”命令,那么在Linux中就要使用“ls”命令,如下所示:

[root@Linux ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog

这样屏幕上就立刻显示当前目录下的所有文件及自目录。注意你可能发现“Desktop”显示的颜色是蓝颜色,这里提示大家一下:

白色的文件:一般的文件
蓝色的文件:目录
绿色的文件:可执行文件

这种方法可以最直观的方法来识别文件类型。

直接使用“ls”可能看到的信息有限,为了知道关于文档的更多资料,我们可以增加一个参数“-l”,这样可以看到更多关于文件的信息:

[root@Linux ~]# ls -l
总计 56
-rw------- 1 root root 865 03-15 04:12 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 03-14 20:34 Desktop
-rw-r--r-- 1 root root 27964 03-15 04:12 install.log
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog

这样就可以知道文档的权限、拥有者、大小、日期等相关的信息,关于这些信息可能大家一时看不懂,在以后的学习中会讲这些内容的。

“ls -l”有一个简写是“ll”,如下:

[root@Linux ~]# ll
总计 56
-rw------- 1 root root 865 03-15 04:12 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 03-14 20:34 Desktop
-rw-r--r-- 1 root root 27964 03-15 04:12 install.log
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog

所显示的和使用“ls -l”一模一样。

2、显示隐藏的文件
系统中一些文件,和Windows中的系统文件一样,这些文件一般是不显示出来的,那么我们可以使用“-a”参数来显示隐藏的文件。如下所示:

[root@Linux ~]# ls -a
. .chewing .gconfd install.log .redhat
.. .cshrc .gnome install.log.syslog .scim
anaconda-ks.cfg Desktop .gnome2 .lesshst .tcshrc
.bash_history .dmrc .gnome2_private .metacity .thumbnails
.bash_logout .eggcups .gstreamer-0.10 .mozilla .Trash
.bash_profile .esd_auth .gtkrc-1.2-gnome2 .nautilus
.bashrc .gconf .ICEauthority .recently-used.xbel

看看这样显示的文件就比较多了。可以发现,隐藏文件是以“.”开头的文件。这些文件一般都有特殊的作用,如果显示出来,有可能被用户不小心删除。

我们可以结合上面的两个参数来显示目录下的文件,如下所示:

[root@Linux ~]# ls -al
总计 212
drwxr-x--- 18 root root 4096 03-19 05:30 .
drwxr-xr-x 24 root root 4096 03-19 12:21 ..
-rw------- 1 root root 865 03-15 04:12 anaconda-ks.cfg
-rw------- 1 root root 2372 03-19 05:52 .bash_history
-rw-r--r-- 1 root root 24 2006-07-13 .bash_logout
-rw-r--r-- 1 root root 191 2006-07-13 .bash_profile
-rw-r--r-- 1 root root 176 2006-07-13 .bashrc
drwx------ 2 root root 4096 03-14 20:32 .chewing
-rw-r--r-- 1 root root 100 2006-07-13 .cshrc
drwxr-xr-x 2 root root 4096 03-14 20:34 Desktop
-rw------- 1 root root 26 03-14 20:31 .dmrc
drwxr-x--- 2 root root 4096 03-14 20:32 .eggcups
-rw------- 1 root root 16 03-19 05:30 .esd_auth
drwx------ 4 root root 4096 03-19 05:06 .gconf
drwx------ 2 root root 4096 03-19 05:30 .gconfd
drwxr-xr-x 3 root root 4096 03-14 20:32 .gnome
drwx------ 6 root root 4096 03-19 05:30 .gnome2
drwx------ 2 root root 4096 03-14 20:32 .gnome2_private
drwxr-xr-x 2 root root 4096 03-14 20:32 .gstreamer-0.10
-rw-r--r-- 1 root root 81 03-14 20:32 .gtkrc-1.2-gnome2
-rw------- 1 root root 358 03-19 05:30 .ICEauthority
-rw-r--r-- 1 root root 27964 03-15 04:12 install.log
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog
-rw------- 1 root root 35 03-19 05:26 .lesshst
drwx------ 3 root root 4096 03-14 20:32 .metacity
drwx------ 3 root root 4096 03-14 20:36 .mozilla
drwxr-xr-x 3 root root 4096 03-19 05:30 .nautilus
-rw-r--r-- 1 root root 1528 03-19 05:30 .recently-used.xbel
drwxr-xr-x 3 root root 4096 03-14 20:32 .redhat
drwx------ 4 root root 4096 03-14 21:06 .scim
-rw-r--r-- 1 root root 129 2006-07-13 .tcshrc
drwx------ 3 root root 4096 03-14 20:33 .thumbnails
drwx------ 2 root root 4096 03-14 20:32 .Trash

这样显示是不是觉得更好呢?

3、显示其他目录中的文件
上面都是显示当前工作目录中的文件,如果要显示其他目录中的文件,那么我们只要在“ls”后面加入所要显示的目录的路径。如下所示:

[root@Linux ~]# ls /
bin dev home lost+found misc net proc sbin srv tftpboot usr
boot etc lib media mnt opt root selinux sys tmp var

这样显示的就是“/”目录下的文件。当然我们也可以加入一下参数,如下所示:

[root@Linux ~]# ls -l /
总计 148
drwxr-xr-x 2 root root 4096 03-16 17:05 bin
drwxr-xr-x 4 root root 1024 03-15 03:58 boot
drwxr-xr-x 11 root root 3920 03-19 12:21 dev
drwxr-xr-x 96 root root 12288 03-19 12:21 etc
drwxr-xr-x 4 root root 1024 03-14 20:29 home
drwxr-xr-x 14 root root 4096 03-16 17:04 lib
drwx------ 2 root root 16384 03-15 03:45 lost+found
drwxr-xr-x 2 root root 4096 03-15 04:26 media
drwxr-xr-x 2 root root 0 03-19 12:21 misc
drwxr-xr-x 2 root root 4096 2006-10-11 mnt
drwxr-xr-x 2 root root 0 03-19 12:21 net
drwxr-xr-x 2 root root 4096 2006-10-11 opt
dr-xr-xr-x 83 root root 0 03-19 12:20 proc
drwxr-x--- 18 root root 4096 03-19 05:30 root
drwxr-xr-x 2 root root 12288 03-16 17:05 sbin
drwxr-xr-x 2 root root 4096 03-15 03:50 selinux
drwxr-xr-x 2 root root 4096 2006-10-11 srv
drwxr-xr-x 11 root root 0 03-19 12:20 sys
drwxr-xr-x 2 root root 4096 2006-07-13 tftpboot
drwxrwxrwt 6 root root 4096 03-19 12:21 tmp
drwxr-xr-x 14 root root 4096 03-15 03:54 usr
drwxr-xr-x 25 root root 4096 03-15 04:04 var

加入“-l”参数后,显示的信息还是多多了。

4、建立目录
当我们工作的需要,建立一个目录的时候,我们可以使用“mkdir”命令来建立一个目录,如下所示:

[root@Linux ~]# mkdir myfile
[root@Linux ~]# ll
总计 60
-rw------- 1 root root 865 03-15 04:12 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 03-14 20:34 Desktop
-rw-r--r-- 1 root root 27964 03-15 04:12 install.log
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog
drwxr-xr-x 2 root root 4096 03-19 12:36 myfile

我们先使用“mkdir myfile”建立了一个名字叫“myfile”的目录,然后我们使用命令验证一下,发现我们已经建立了这个文件夹,可以根据颜色来判断是一个文件夹。

5、删除目录
如果这个目录不需要了,我们可以使用“rmdir”命令来删除一个目录,用法如下:

[root@Linux ~]# ll
总计 60
-rw------- 1 root root 865 03-15 04:12 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 03-14 20:34 Desktop
-rw-r--r-- 1 root root 27964 03-15 04:12 install.log
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog
drwxr-xr-x 2 root root 4096 03-19 12:36 myfile
[root@Linux ~]# rmdir myfile
[root@Linux ~]# ll
总计 56
-rw------- 1 root root 865 03-15 04:12 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 03-14 20:34 Desktop
-rw-r--r-- 1 root root 27964 03-15 04:12 install.log
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog

我们先看看当前目录下有一个myfile的目录,我们不需要这个目录了,我们可以使用“rmdir myfile”命令来删除这个目录,然后使用ll命令验证一下。

注意的是,使用这个命令来删除一个目录的时候,那么这个目录下必须是没有任何文件和文件夹的,我们看看如下的操作:

[root@Linux ~]# mkdir myfile
[root@Linux ~]# cd myfile cd命令是切换目录,这里就是进入myfile
[root@Linux myfile]# mkdir one
[root@Linux myfile]# cd .. ..表示当前目录的上一层目录
[root@Linux ~]# rmdir myfile
rmdir: myfile: 目录非空

我们在myfile目录下建立了一个one的目录,当我们删除myfile目录时,提示目录为空,所以必须将这个目录下的所有文件和目录全部删除之后,才能删除这个目录。

6、切换目录
上面的例子中提到了一个“cd”命令,一般为了工作的需要,我们可能需要到不同的目录中编辑不同的文件,这个时候我们就要在不同的目录中切换了。我们使用“cd”命令可以在不同的目录中切换,如下的操作:

[root@Linux ~]# mkdir one
[root@Linux ~]# mkdir two
[root@Linux ~]# cd one
[root@Linux one]#

我们建立了两个目录“one”和“two”,我们可以使用“cd one”,进入one目录,进入这个目录之后,命令提示符有原来的“[root@Linux ~]”变成了“[root@Linux one]”,提示符中的“one”表示我们在one这个目录中工作了。

我们看看这个目录下有什么文件,为了看看有没有隐藏文件,我们使用“ls -al”命令来看看这个目录。如下操作:

[root@Linux one]# ls -al
总计 12
drwxr-xr-x 2 root root 4096 03-19 12:43 .
drwxr-x--- 20 root root 4096 03-19 12:43 ..

我们发现这个目录下面有两个奇怪的家伙,一个是“.”,一个是“..”。其实这又要讲到路径的知识,路径的表示方法有两种:

A :绝对路径:就是表示出目标文件或子目录的目录树结构。我们这个例子中的目录“one”,的绝对路径表示方法为“/root/one”,如果one目录下还有一个目录xu,那么目录xu的绝对路径就是:“/root/one/xu”。

B :相对路径:就是相对于当前工作目录的路径来表示位置。相对路径中有两个符号,就是“..”,“.”。“..”表示当前当前工作目录的上一层目录。如当前我们处在one目录中,我们作如下操作:

[root@Linux one]# cd ..
[root@Linux ~]#

我们就或发现提示“[root@Linux one]”变为“[root@Linux ~]”。发现现在已经返回到目录one的上一层目录。

“.”表示当前的工作目录。如果当前我们处在one目录中,那么“.”就表示当前的工作目录one。

7、复制文件
工作的时候,有是需要保存文件的一个副本,便于以后还原。我们可以使用“cp”命令,操作如下:

[root@Linux ~]# ll
总计 64
-rw------- 1 root root 865 03-15 04:12 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 03-14 20:34 Desktop
-rw-r--r-- 1 root root 27964 03-15 04:12 install.log
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog
drwxr-xr-x 2 root root 4096 03-19 12:43 one
drwxr-xr-x 2 root root 4096 03-19 12:43 two
[root@Linux ~]# cp install.log ./one
[root@Linux ~]# cd one
[root@Linux one]# ll
总计 28
-rw-r--r-- 1 root root 27964 03-19 12:55 install.log

先看看当前目录下有哪些文件,然后我们需要将install.log文件复制一份到one目录中,我们可以使用“cp install.log ./one”就可以了,注意了,这里使用了上面的相对路径中的“.”,表示当前工作目录下的one目录。

然后进one目录,查看一下是否有intsall.log这个文件,可以看到我们已经复制了一份lnstall.log文件。

我们在来试一试,我们将目录one复制到two中,我们看看如下的操作:

[root@Linux ~]# ll
总计 64
-rw------- 1 root root 865 03-15 04:12 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 03-14 20:34 Desktop
-rw-r--r-- 1 root root 27964 03-15 04:12 install.log
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog
drwxr-xr-x 2 root root 4096 03-19 12:55 one
drwxr-xr-x 2 root root 4096 03-19 12:43 two
[root@Linux ~]# cp one two
cp: 略过目录 “one”
[root@Linux ~]# cd two
[root@Linux two]# ll
总计 0

发现没有two目录下没有one文件夹,这个时候我们需要使用参数“-r”,看如下操作:

[root@Linux ~]# ll
总计 64
-rw------- 1 root root 865 03-15 04:12 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 03-14 20:34 Desktop
-rw-r--r-- 1 root root 27964 03-15 04:12 install.log
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog
drwxr-xr-x 2 root root 4096 03-19 12:55 one
drwxr-xr-x 2 root root 4096 03-19 12:43 two
[root@Linux ~]# cp -r one two
[root@Linux ~]# cd two
[root@Linux two]# ll
总计 4
drwxr-xr-x 2 root root 4096 03-19 13:00 one

看看,在cp命令后面加入参数“-r”之后,就可以用来复制目录了,我们可以进入two目录下的子目录one,会发现原来one目录中的文件也一起被复制了。

最后来看看复制文件并重新命名的新文件,实例如下:

[root@Linux one]# cp install.log install.log.bak
[root@Linux one]# ll
总计 56
-rw-r--r-- 1 root root 27964 03-19 12:55 install.log
-rw-r--r-- 1 root root 27964 03-20 13:10 install.log.bak

这里就是复制了一个文件,并将复制的文件更名,我们可以看到目录下有两个文件了。

8、移动文件
我们可以使用mv命令将一个文件移动到另外一个目录中,如下操作:

[root@Linux ~]# ll
总计 64
-rw------- 1 root root 865 03-15 04:12 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 03-14 20:34 Desktop
-rw-r--r-- 1 root root 27964 03-15 04:12 install.log
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog
drwxr-xr-x 2 root root 4096 03-20 13:10 one
drwxr-xr-x 3 root root 4096 03-19 13:00 two
[root@Linux ~]# mv install.log.syslog ./one
[root@Linux ~]# cd one
[root@Linux one]# ll
总计 68
-rw-r--r-- 1 root root 27964 03-19 12:55 install.log
-rw-r--r-- 1 root root 27964 03-20 13:10 install.log.bak
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog

我们将root用户目录下的install.log.syslog,移动到目录one下。除此之外,我们还可以为文件更名,如下操作:

[root@Linux one]# ll
总计 68
-rw-r--r-- 1 root root 27964 03-19 12:55 install.log
-rw-r--r-- 1 root root 27964 03-20 13:10 install.log.bak
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog
[root@Linux one]# mv install.log install.sys
[root@Linux one]# ll
总计 68
-rw-r--r-- 1 root root 27964 03-20 13:10 install.log.bak
-rw-r--r-- 1 root root 5069 03-15 04:11 install.log.syslog
-rw-r--r-- 1 root root 27964 03-19 12:55 install.sys

我们将install.log这个文件名称变为了install.sys。

9、创建文件
这个和前面的创建目录是有区别的,创建文件是指创建一个一般的普通文件,并且这个文件为空,我们可以使用touch命令来建立一般文件,如下操作:

[root@Linux two]# touch 111.txt
[root@Linux two]# touch 112.txt
[root@Linux two]# touch 113.txt
[root@Linux two]# ll
总计 0
-rw-r--r-- 1 root root 0 03-20 13:26 111.txt
-rw-r--r-- 1 root root 0 03-20 13:26 112.txt
-rw-r--r-- 1 root root 0 03-20 13:26 113.txt

这样我们就创建了三个文件。

10、删除文件
使用rm命令来删除文件,和前面的rmdir有点区别,不过rm也可以实现删除目录,我们来看看实际的操作:

[root@Linux two]# ll
总计 0
-rw-r--r-- 1 root root 0 03-20 13:26 111.txt
-rw-r--r-- 1 root root 0 03-20 13:26 112.txt
-rw-r--r-- 1 root root 0 03-20 13:26 113.txt
[root@Linux two]# mkdir one
[root@Linux two]# ll
总计 4
-rw-r--r-- 1 root root 0 03-20 13:26 111.txt
-rw-r--r-- 1 root root 0 03-20 13:26 112.txt
-rw-r--r-- 1 root root 0 03-20 13:26 113.txt
drwxr-xr-x 2 root root 4096 03-20 13:27 one
[root@Linux two]# rm 111.txt
rm:是否删除 一般空文件 “111.txt”? y
[root@Linux two]# ll
总计 4
-rw-r--r-- 1 root root 0 03-20 13:26 112.txt
-rw-r--r-- 1 root root 0 03-20 13:26 113.txt
drwxr-xr-x 2 root root 4096 03-20 13:27 one

我们先查看目录下的文件,然后删除这个目录下的文件111.txt,删除的时候,提示我们是否要删除文件,我们输入“y”,然后删除文件,删除之后,我们查看这个文件夹下的文件,发现少了111.txt文件。

如果我们不需要系统提示我们就要增加一个参数“-f”,这样只要输入命令之后,系统直接删除文件。看如下操作:

[root@Linux two]# rm -f 112.txt
[root@Linux two]# ll
总计 4
-rw-r--r-- 1 root root 0 03-20 13:26 113.txt
drwxr-xr-x 2 root root 4096 03-20 13:27 one

这样增加参数后,系统没有提示用户,直接删除了文件。

我们在来看看如何删除这个文件下的目录,增加参数“-r”就可以了,看如下操作:

[root@Linux two]# ll
总计 4
-rw-r--r-- 1 root root 0 03-20 13:26 113.txt
drwxr-xr-x 2 root root 4096 03-20 13:27 one
[root@Linux two]# rm -rf one
[root@Linux two]# ll
总计 0
-rw-r--r-- 1 root root 0 03-20 13:26 113.txt

这样,这个目录下的one目录就被删除了。

11、通配符
以上所有的操作我们好像都只能操作一个文件,当我们要同时对多个文件进行操作的时候,我们就可以使用通配符,这些符号不是作为普通的文字来使用,而是来代替文键名称的字符串。我们来看看Linux下的几种通配符,和他们的含义:

A:* :任何字符和字符串。
B:? :一个任意字符
C:[abc...] :[]内的任意一个字符。[abc]表示a、b、c任一个字符;有时候也表示范围,如[a-x],表示a到x的任一个字符;[1-9]表示1到9的任一数字。
D:[!abc...]:和上面的相反,表示除[]内的字符外的任意一个字符。

我们来分别看看这些符号的实际操作中的实例:

[root@Linux two]# ll
总计 0
-rw-r--r-- 1 root root 0 03-20 20:08 111.txt
-rw-r--r-- 1 root root 0 03-20 20:08 112.txt
-rw-r--r-- 1 root root 0 03-20 20:08 113.txt
[root@Linux two]# rm 1*.txt
rm:是否删除 一般空文件 “111.txt”? y
rm:是否删除 一般空文件 “112.txt”? y
rm:是否删除 一般空文件 “113.txt”? y
[root@Linux two]# ll
总计 0

这样只要是以“1”开头的文件全部删除。我们在来看下面的操作:

[root@Linux two]# ll
总计 0
-rw-r--r-- 1 root root 0 03-20 20:09 111.txt
-rw-r--r-- 1 root root 0 03-20 20:09 112.txt
-rw-r--r-- 1 root root 0 03-20 20:09 113.txt
-rw-r--r-- 1 root root 0 03-20 20:10 121.txt
[root@Linux two]# rm -f 11?.txt
[root@Linux two]# ll
总计 0
-rw-r--r-- 1 root root 0 03-20 20:10 121.txt

这样就删除了以“11”开头的文件,我们还是以上面的例子在看:

[root@Linux two]# ll
总计 0
-rw-r--r-- 1 root root 0 03-20 20:11 111.txt
-rw-r--r-- 1 root root 0 03-20 20:11 112.txt
-rw-r--r-- 1 root root 0 03-20 20:11 113.txt
-rw-r--r-- 1 root root 0 03-20 20:10 121.txt
[root@Linux two]# rm -f 11[1-3].txt
[root@Linux two]# ll
总计 0
-rw-r--r-- 1 root root 0 03-20 20:10 121.txt

这样就删除了以“11”开头,后面是以1、2、3结尾的文件。最后我们来看:

[root@Linux two]# ll
总计 0
-rw-r--r-- 1 root root 0 03-20 20:13 111.txt
-rw-r--r-- 1 root root 0 03-20 20:13 112.txt
-rw-r--r-- 1 root root 0 03-20 20:13 113.txt
-rw-r--r-- 1 root root 0 03-20 20:10 121.txt
[root@Linux two]# rm -f 1[!1]*.txt
[root@Linux two]# ll
总计 0
-rw-r--r-- 1 root root 0 03-20 20:13 111.txt
-rw-r--r-- 1 root root 0 03-20 20:13 112.txt
-rw-r--r-- 1 root root 0 03-20 20:13 113.txt

这样就删除了第二个字符不是为“1”的任何文件。

有了通配符,我们就可以针对多个文件一起进行操作。

10、pwd
如果切换目录次数太多,可能不知道自己在那个目录,那么就可以使用pwd命令,显示当前所在的目录。是print working directory的缩写。看如下操作:

[root@Linux ~]# pwd
/root
[root@Linux ~]# cd one
[root@Linux one]# pwd
/root/one
[root@Linux one]# cd ../two
[root@Linux two]# pwd
/root/two

这样就很清楚自己在那个目录下工作了。
文章评论

共有 1 条评论

  1. sonicss 于 2008-03-22 22:31:17发表:

    新手上路,又学习了一遍,谢谢。