楼主: changyongid

一天一个Linux命令

[复制链接]
 楼主| 发表于 2009-9-10 15:50:26 | 显示全部楼层
来试一下。。做几个例子就会用了。。
 楼主| 发表于 2009-9-10 15:57:30 | 显示全部楼层
[changyongid@localhost ~]$ pwd
/home/changyongid
现在在我们用户的主目录下

建立一个名为a的目录
[changyongid@localhost ~]$ mkdir a
然后ls -d 一下,就可以看到a这个目录了。ls -d 是列出当前路径下的目录。

选项 -v
[changyongid@localhost ~]$ mkdir -v a
mkdir: 已创建目录 “a”
上面的第二行是终端里提示出来的。。这下可以看到使用 -v 选项的作用了吧。。

选项 -p
这是个很有用的选项。记得在某篇文章里说高手都喜欢用带 -p 的。具体它是什么作用呢?我们使用一下:
注意:现在我已经删除了刚才建的那个a目录“rm -rf ./a”
那么,现在我们来建一个目录b,它在a目录里面。。。于是很自然的可以想到:
[changyongid@localhost ~]$ mkdir a/b
mkdir: 无法创建目录 “a/b”: 没有那个文件或目录
看到了吧,没有这个目录,即我们没有a目录,那么也就不能创建a里面的b目录喽。。
ok,-p选项的用武之地来了。
[changyongid@localhost ~]$ mkdir -p a/b
[changyongid@localhost ~]$ tree a
a
`-- b

用tree命令来查看a目录里的结构。可以看到,它里面有个b目录了。方便了很多了吧。
 楼主| 发表于 2009-9-10 16:04:45 | 显示全部楼层
至于 -m 选项,我们也来使用一下吧。不过仅限这个东西牵到的东西较多。一下子说起来有点麻烦。留到有空了再来说吧。。。
[changyongid@localhost ~]$ ll -d a
drwxrwxr-x 3 changyongid changyongid 4096 09-10 15:56 a
这是刚建的a目录,如上可看到它的权限。

[changyongid@localhost ~]$ mkdir -m 777 a
[changyongid@localhost ~]$ ll -d a
drwxrwxrwx 2 changyongid changyongid 4096 09-10 16:00 a
可以看到,用-m参数,再建立目录a,它的权限就全开了。。
注:777是权限的一种表达方式。下面的rwx分别表试一个文件的读、写、可执行 权限。
那么7的二进制也就是111,正好对应rwx。那么对应位为1就表示这个权限开了。
那么 777也就表示rwx rwx rwx喽。。。这三组是不同的。分别对应了 属主用户 、 属主的组、其他。

好吧好吧。先说到这里,再说下去话又多了。可以上网查一下,比我说的清楚。
发表于 2009-9-13 11:03:11 | 显示全部楼层
周末不更新?
 楼主| 发表于 2009-9-13 17:58:06 | 显示全部楼层
这周末回学校了。。毕业 后第一次回校。。
又走了走平时经常走的那些校园小道,又想起毕业时同学们一起在“跳骚市场”卖旧货的情景,见了一些还在校园里的好友。
校园依然亲切,却已不再有从前的那种感觉了。因为人都不在了……

我仍然会偶尔梦见学校,梦见那些可爱的同学们。
我还是没有适应完全离开
 楼主| 发表于 2009-9-13 18:38:28 | 显示全部楼层
[changyongid@Fedora ~]$ rmdir --help
Usage: rmdir [OPTION]... DIRECTORY...
rmdir 选项...   目录....
Remove the DIRECTORY(ies), if they are empty.
如果目录是空的话,则删除这个目录

      --ignore-fail-on-non-empty
                  ignore each failure that is solely because a directory
                    is non-empty
忽略任何因目录为非空而造成的错误
  -p, --parents   remove DIRECTORY and its ancestors; e.g., `rmdir -p a/b/c' is
                    similar to `rmdir a/b/c a/b a'
删除指定的目录,若该目录的上层目录变成了空目录,则一并删除
  -v, --verbose   output a diagnostic for every directory processed
显示指定执行的过程
      --help     display this help and exit
显示帮助信息,并退出
      --version  output version information and exit
输出版本信息,并退出

说明:如果目录为空的话,则会删除该目录,-p时,必须每级的目录都为空,否则不能删除。
 楼主| 发表于 2009-9-13 18:47:39 | 显示全部楼层
例:
先建立目录。用mkdir
  1. [changyongid@Fedora test]$ mkdir -p a/b/c
复制代码
这样,可以看一下现在目录结构
  1. [changyongid@Fedora test]$ tree a
  2. a
  3. `-- b
  4.     `-- c

  5. 2 directories, 0 files
复制代码
那么,我们先删除c目录。
  1. [changyongid@Fedora test]$ rmdir a/b/c
  2. [changyongid@Fedora test]$ tree a
  3. a
  4. `-- b

  5. 1 directory, 0 files
复制代码
看到最后一行是显示“1 directory, 0 files”这样的信息。
现在已经删除了c目录了。因为这个目录为空,直接rmdir就删除掉了。

现在删除b目录,删除之前,我们在b目录里面放置一个文件。看目录非空时会怎么样。
  1. [changyongid@Fedora test]$ touch a/b/test
  2. [changyongid@Fedora test]$ tree a
  3. a
  4. `-- b
  5.     `-- test

  6. 1 directory, 1 file
复制代码
看到了,已经在b里建了一个文件test。
  1. [changyongid@Fedora test]$ rmdir a/b/
  2. rmdir: failed to remove `a/b/': Directory not empty
复制代码
看到了吧,无法删除。如此的话。我们就不能删除b目录了。

大家看到这里,会不会有一个疑问,当我们要删除一个目录,包括目录里面的所有文件我们都要一起删除,这怎么办呢?
 楼主| 发表于 2009-9-13 18:50:13 | 显示全部楼层
对于楼上提出的疑问,用rm就可以了。rm命令比较强大,相比之下,rmdir命令的作用就显得很单薄了。几乎很少用到,一般用rm就可以了。对于rm。放到以后再 讲。

ps:才发现公社还有贴代码的功能。真是浅薄了。
 楼主| 发表于 2009-9-13 19:03:28 | 显示全部楼层
前两个命令是针对目录操作的。很多时候,我们进入了一个目录,第一件事情就是要看一下这个目录里面有些什么文件,又有哪些子目录。
或是,在我们当前的位置,想查看另外路径的某目录里的信息,但此时又不想进入该目录。咋末办呢?

对于第一个问题,答案就是ls命令。即,下面将要说明的。
对于第二个问题,其实,这个问题在我们之前的学习中一定也碰到过了。这里先说明一下。
对于路径,在Linux里有 相对路径 和 绝对路径 之分。
乍一听起来,还真有点玄乎,其实很简单。我们要访问某个文件或目录(其实目录也是一种文件,Linux里,一切皆为文件),而此文件在别的路径径下,我们如何访问到呢?
举个例子:
          我们有一个目录a,其里面的结构如下:
  1. [changyongid@Fedora test]$ tree a
  2. a
  3. |-- b
  4. |   `-- test
  5. `-- c

  6. 2 directories, 1 file
复制代码
即a里有两个目录,b和c。(b里有一个test文件)
现在我们在c目录里。那么我们如何访问到test文件呢?

1.通过相对路径。即相对于我们当前目录的路径。
那么我们用vim访问test文件的话,命令就可以这么写
  1. [changyongid@Fedora c]$ vim ../b/test
复制代码
.. 表示父目录。这就是相对路径
2.通过绝对路径。绝对路径,就是一个文件在文件系统里面的固定的位置,它跟当前我们在哪个位置无关。所以绝对路径是以 / 开头的。
查看一下当前目录的绝对路径还记得么?pwd
 楼主| 发表于 2009-9-13 19:06:02 | 显示全部楼层
接着,再来看看ls
可以help一下,但是ls 的命令参数太多了。以至于我们无法在一屏里面完整的看出。这个时候我们可以用less。
这个我们已经学过了。看到了吧,之前学过的命令都是很重要的。
  1. [changyongid@Fedora c]$ ls --help
  2. \Usage: ls [OPTION]... [FILE]...
  3. List information about the FILEs (the current directory by default).
  4. Sort entries alphabetically if none of -cftuvSUX nor --sort.

  5. Mandatory arguments to long options are mandatory for short options too.
  6.   -a, --all                  do not ignore entries starting with .
  7.   -A, --almost-all           do not list implied . and ..
  8.       --author               with -l, print the author of each file
  9.   -b, --escape               print octal escapes for nongraphic characters
  10.       --block-size=SIZE      use SIZE-byte blocks
  11.   -B, --ignore-backups       do not list implied entries ending with ~
  12.   -c                         with -lt: sort by, and show, ctime (time of last
  13.                                modification of file status information)
  14.                                with -l: show ctime and sort by name
  15.                                otherwise: sort by ctime
  16.   -C                         list entries by columns
  17.       --color[=WHEN]         control whether color is used to distinguish file
  18.                                types.  WHEN may be `never', `always', or `auto'
  19.   -d, --directory            list directory entries instead of contents,
  20.                                and do not dereference symbolic links
  21.   -D, --dired                generate output designed for Emacs' dired mode
  22.   -f                         do not sort, enable -aU, disable -ls --color
  23.   -F, --classify             append indicator (one of */=>@|) to entries
  24.       --file-type            likewise, except do not append `*'
  25.       --format=WORD          across -x, commas -m, horizontal -x, long -l,
  26.                                single-column -1, verbose -l, vertical -C
  27.       --full-time            like -l --time-style=full-iso
  28.   -g                         like -l, but do not list owner
  29.       --group-directories-first
  30.                              group directories before files.
  31.                                augment with a --sort option, but any
  32.                                use of --sort=none (-U) disables grouping
  33.   -G, --no-group             in a long listing, don't print group names
  34.   -h, --human-readable       with -l, print sizes in human readable format
  35.                                (e.g., 1K 234M 2G)
  36.       --si                   likewise, but use powers of 1000 not 1024
  37.   -H, --dereference-command-line
  38.                              follow symbolic links listed on the command line
  39.       --dereference-command-line-symlink-to-dir
  40.                              follow each command line symbolic link
  41.                              that points to a directory
  42.       --hide=PATTERN         do not list implied entries matching shell PATTERN
  43.                                (overridden by -a or -A)
  44.       --indicator-style=WORD  append indicator with style WORD to entry names:
  45.                                none (default), slash (-p),
  46.                                file-type (--file-type), classify (-F)
  47.   -i, --inode                print the index number of each file
  48.   -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  49.   -k                         like --block-size=1K
  50.   -l                         use a long listing format
  51.   -L, --dereference          when showing file information for a symbolic
  52.                                link, show information for the file the link
  53.                                references rather than for the link itself
  54.   -m                         fill width with a comma separated list of entries
  55.   -n, --numeric-uid-gid      like -l, but list numeric user and group IDs
  56.   -N, --literal              print raw entry names (don't treat e.g. control
  57.                                characters specially)
  58.   -o                         like -l, but do not list group information
  59.   -p, --indicator-style=slash
  60.                              append / indicator to directories
  61.   -q, --hide-control-chars   print ? instead of non graphic characters
  62.       --show-control-chars   show non graphic characters as-is (default
  63.                              unless program is `ls' and output is a terminal)
  64.   -Q, --quote-name           enclose entry names in double quotes
  65.       --quoting-style=WORD   use quoting style WORD for entry names:
  66.                                literal, locale, shell, shell-always, c, escape
  67.   -r, --reverse              reverse order while sorting
  68.   -R, --recursive            list subdirectories recursively
  69.   -s, --size                 print the allocated size of each file, in blocks
  70.   -S                         sort by file size
  71.       --sort=WORD            sort by WORD instead of name: none -U,
  72.                              extension -X, size -S, time -t, version -v
  73.       --time=WORD            with -l, show time as WORD instead of modification
  74.                              time: atime -u, access -u, use -u, ctime -c,
  75.                              or status -c; use specified time as sort key
  76.                              if --sort=time
  77.       --time-style=STYLE     with -l, show times using style STYLE:
  78.                              full-iso, long-iso, iso, locale, +FORMAT.
  79.                              FORMAT is interpreted like `date'; if FORMAT is
  80.                              FORMAT1FORMAT2, FORMAT1 applies to
  81.                              non-recent files and FORMAT2 to recent files;
  82.                              if STYLE is prefixed with `posix-', STYLE
  83.                              takes effect only outside the POSIX locale
  84.   -t                         sort by modification time
  85.   -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  86.   -u                         with -lt: sort by, and show, access time
  87.                                with -l: show access time and sort by name
  88.                                otherwise: sort by access time
  89.   -U                         do not sort; list entries in directory order
  90.   -v                         sort by version
  91.   -w, --width=COLS           assume screen width instead of current value
  92.   -x                         list entries by lines instead of by columns
  93.   -X                         sort alphabetically by entry extension
  94.   -1                         list one file per line

  95. SELinux options:

  96.   --lcontext                 Display security context.   Enable -l. Lines
  97.                              will probably be too wide for most displays.
  98.   -Z, --context              Display security context so it fits on most
  99.                              displays.  Displays only mode, user, group,
  100.                              security context and file name.
  101.   --scontext                 Display only security context and file name.
  102.       --help     display this help and exit
  103.       --version  output version information and exit

  104. SIZE may be (or may be an integer optionally followed by) one of following:
  105. kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

  106. By default, color is not used to distinguish types of files.  That is
  107. equivalent to using --color=none.  Using the --color option without the
  108. optional WHEN argument is equivalent to using --color=always.  With
  109. --color=auto, color codes are output only if standard output is connected
  110. to a terminal (tty).  The environment variable LS_COLORS can influence the
  111. colors, and can be set easily by the dircolors command.

  112. Exit status:
  113. 0  if OK,
  114. 1  if minor problems (e.g., cannot access subdirectory),
  115. 2  if serious trouble (e.g., cannot access command-line argument).
复制代码
 楼主| 发表于 2009-9-13 19:06:26 | 显示全部楼层
本帖最后由 changyongid 于 2009-9-13 21:02 编辑

重复发了一行。编辑掉……
 楼主| 发表于 2009-9-13 19:10:56 | 显示全部楼层
从楼上可以看到,ls的参数太多了,以至于我们怀疑是否有必要一行一行的看下去?

这绝对是有必要的。为什么呢?
因为ls是Linux里面用到最多的命令之一。使用的频率如此之高,还是非常有必要好好的掌握一下,这会对我们以后的使用Linux时的效率大大提高。
 楼主| 发表于 2009-9-13 21:31:00 | 显示全部楼层
ls ,即 list的意思。

格式: ls [OPTION]... [FILE]...
列出文件的详细信息。默认时列出当前目录的详细信息,即当前目录里的内容。

  -a, --all                  显示目录里的所有文件,包括隐藏文件
  -A, --almost-all           与a差不多,但不列出. 和..   。这两个分别表式当前目录和上一级目录。

  -b, --escape              把文件名中不可输出的字符用反斜杠加字符编号的形式列出

  -B, --ignore-backups       不显示以 ~ 结尾的文件(这些一般是备份文件)
  -c                       以修改时间排序
  -C                         按列出输,纵向排序

  -d, --directory           只显示目录文件,而不显示其他的文件。

  -g                         如 -l,但不显示所属用户
      --group-directories-first
                             group directories before files.
                               augment with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group             不显示组信息
  -h, --human-readable       with -l, print sizes in human readable format
                               (e.g., 1K 234M 2G)

  -i, --inode                显示节点信息

  -l                         列出详细的信息
  -L                  列出链接文件名而不显示链接到的文件
  -n, --numeric-uid-gid     如 -l ,但显示用户和组时,不是显示他们的名字,而是id号
  -N,           不限制文件长度
  -o                        除组以外的详细信息
  -p, --indicator-style=slash
                            在每个文件名后附上一个字符以说明该文件的类型。*表示普通文件  /表示目录  @表示符号链接  |表示FIFOs  =表示套接字

  -q, --hide-control-chars   用?代替不可输出的字符

  -Q,      把输出的文件名用引号括起来
  -r, --reverse            对目录反向排序
  -R, --recursive           列出所有子目录下的文件
  -s, --size                 在每个文件名旁边输出该文件的大小
  -S                        以文件大小排序

  -t                        以修改时间排序

  -u                       以访问时间排序
  -U                        不排序
  
  -1                        逐行显示

暂且列这么多吧。真是挺多的。
 楼主| 发表于 2009-9-13 21:39:03 | 显示全部楼层
  1. 可以试一下以下几个选项
  2. ls -l
  3. ls -a
  4. ls -la
  5. ls -sh
  6. ls -i
  7. ls -1
  8. 这里就不一一列出来了
复制代码
 楼主| 发表于 2009-9-15 09:25:45 | 显示全部楼层
OH shit!
I can not input chinese now,and i am sorry for my poor english.
 楼主| 发表于 2009-9-16 08:36:05 | 显示全部楼层
it is ok now!
 楼主| 发表于 2009-9-16 08:51:40 | 显示全部楼层
cp
格式:cp [OPTION]... [-T] SOURCE DEST
           cp [OPTION]... SOURCE... DIRECTORY
           cp [OPTION]... -t DIRECTORY SOURCE...

功能:将 SOURCE 复制(copy)到 DEST ,或者将多个 SOURCE 复制到 DEST

OPTION说明:
  -a, --archive                作用等同于 -dR --preserve=all (一般在拷贝目录时用)

  -d                                拷贝时保留链接
  -f, --force                     强制拷贝,不需要提示
  -i, --interactive            与用户交互,在覆盖文件之前请求用户的确认。

  -l, --link                        链接文件,而不是拷贝

  -n, --no-clobber           不覆盖已存在的文件

  -p                                复制后的文件将保留原文件的权限、属主、修改时间等

  -R, -r, --recursive       递归作用于整个目录,包括目录下的所有文件和子目录。在对整个目录进行操作时用。


  -t, --target-directory=DIRECTORY  指定目标路径
 楼主| 发表于 2009-9-16 08:56:33 | 显示全部楼层
实例:
             假设当前目录有个test.c文件。

  1. [changyongid@localhost test]$ ls
  2. test.c
  3. [changyongid@localhost test]$ cp test.c tt                      //直接复制。后面的路径可以是另外的目录里
  4. [changyongid@localhost test]$ ls
  5. test.c  tt
  6. [changyongid@localhost test]$ cp -l test.c ll             //用-l 链接一下,可以看到实际上是做了个硬链接
  7. [changyongid@localhost test]$ ll
  8. total 0
  9. -rw-rw-r-- 2 changyongid changyongid 0 2009-09-16 08:53 ll
  10. -rw-rw-r-- 2 changyongid changyongid 0 2009-09-16 08:53 test.c
  11. -rw-rw-r-- 1 changyongid changyongid 0 2009-09-16 08:54 tt
复制代码
 楼主| 发表于 2009-9-16 08:59:33 | 显示全部楼层
由于cp过于是默认覆盖已存在文件的,所以直接使用这个命令时会有一点危险。在~/.bashrc里面用alias将其替换成 cp -i 即可,加上-i参数,每次要覆盖之前,它都会提示,这样就安全了一些,防止误操作。
 楼主| 发表于 2009-9-16 19:51:18 | 显示全部楼层
命令 : rm
使用 : rm [选项]... 文件...
说明 : 删除文件

  -f, --force      强制删除,不需要提示
  -i                  每次删除行为之前,都提示删除动作(需要用户确认删除)
  -I                  当一次删除文件数为3个或3个以上时,或在递归删除一个目录时,只提示一次.与 -i 相比,这个选项即保障了安全又避免了删除多个文件时的多次打扰。

  -r, -R, --recursive  递归删除目录和其中的文件
  -v, --verbose         交互动作,显示正在进行的动作
   
默认情况下,rm命令不会删除目录。使用 -r 或 -R 选项来删除目录和目录内的文件。

如果一个文件名以“-”开头,比如“-foo”,用下面两个命令中任一个来删除:
  rm -- -foo

  rm ./-foo

注意,当使用rm来删除文件时,被删除的文件是可恢复的。也就是说rm只删除了它的索引结点而已。如果想要真正的删除,不可恢复,可以考虑使用shred。

我们删除一个文件,可以直接的rm删除。
删除一个目录,可使用 rm -rf ... 注意,此处的f是强制。

默认的情况下,我们的删除是不会提示信息的,显然这样是有些危险的。我就曾经犯过一个错误,错将/bin/目录里面的内容全部删除掉了,结果可想而知。当然,我当时犯的错误跟我使用超级用户root有直接的关系。
为了使我们的操作更安全,可以在.bashrc里用alias添加一行
rm='rm -i'
这样,每次我们使用rm命令时都相当于使用 rm -i ,那么每次删除都需要我们的确认了。这样做是有一定好处的,当然还要考虑到个人的习惯问题。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表