Linux常用命令

来源:互联网 发布:mac ps导入ttf 编辑:程序博客网 时间:2024/06/05 03:34

一: 常见命令

1. cd 命令

cd命令的功能是切换到指定的目录: 命令格式: cd [目录名] 目录名有几个符号有特殊的含义,“..”代表上一级目录、“~”代表HOME目录、“-”代表前一目录

2. mkdir

3. rm 删除文件夹

2. echo命令

在Linux使用 echo 并配合命令重定向是实现向文件中写入信息的快捷方式。将 echo 命令与 sudo 命令配合使用,实现向那些只有系统管理员才有权限操作的文件中写入信息。比如要向 test.asc 文件中随便写入点内容,可以:$ echo "信息" > test.asc# 或者$ echo "信息" >> test.asc下面,如果将 test.asc 权限设置为只有 root 用户才有权限进行写操作:$ sudo chown root.root test.asc然后,我们使用 sudo 并配合 echo 命令再次向修改权限之后的 test.asc 文件中写入信息:$ sudo echo "又一行信息" >> test.asc-bash: test.asc: Permission denied这时,可以看到 bash 拒绝这么做,说是权限不够。这是因为重定向符号 “>” 和 ">>" 也是 bash 的命令。我们使用 sudo 只是让 echo 命令具有了 root 权限,但是没有让 “>” 和 ">>" 命令也具有 root 权限,所以 bash 会认为这两个命令都没有像 test.asc 文件写入信息的权限。解决这一问题的途径有两种。第一种是利用 "sh -c" 命令,它可以让 bash 将一个字串作为完整的命令来执行,这样就可以将 sudo 的影响范围扩展到整条命令。具体用法如下:$ sudo sh -c 'echo "又一行信息" >> test.asc'另一种方法是利用管道和 tee 命令,该命令可以从标准输入中读入信息并将其写入标准输出或文件中,具体用法如下:$ echo "第三条信息" | sudo tee -a test.asc注意,tee 命令的 "-a" 选项的作用等同于 ">>" 命令,如果去除该选项,那么 tee 命令的作用就等同于 ">" 命令。

3. source 命令

当我修改了/etc/profile文件,我想让它立刻生效,而不用重新登录;这时就想到用source命令,如:source /etc/profilesource命令:source命令也称为“点命令”,也就是一个点符号(.),是bash的内部命令。功能:使Shell读入指定的Shell程序文件并依次执行文件中的所有语句source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。用法:source filename 或 . filenamesource命令(从 C Shell 而来)是bash shell的内置命令;点命令(.),就是个点符号(从Bourne Shell而来)是source的另一名称。


1. linux sh 的语法

sh [-a] [-c] [-C] [-e] [-E] [-f] [-h] [-i] [-I][-k] [-m] [-n] [-p] [-r] [-s] [-t] [-T] [-u] [-v] [-x] [ argument ]
-a
Export all variables assigned to.
-c
Pass the string argument to the shell to be interpreted as input. Keep in mind that this option only accepts a single string as its argument, hence multi-word strings must be quoted.
-C
Don't overwrite existing files with ``>.''
-e
If not interactive, exit immediately if any untested command fails. The exit status of a command is considered to be explic- itly tested if the command is used to control an if,elif, while, or until; or if the command is the left hand operand of an ``&&'' or ``||'' operator.
-E
Enable the built-in emacs command line editor (disables -V if it has been set).
-f
Disable pathname expansion.
-h
Makes all commands use tracked aliases.
-i
Force the shell to behave interactively.

-I
Ignore EOF's from input when interactive.
-k
tells the shell to use Korn-compatible behavior in any case where the POSIX.2 behavior is different from the behavior specified by Korn. In particular, this affects the trap command.

-m

Turn on job control (set automatically when interactive).
-n
If not interactive, read commands but do not execute them. This is useful for checking the syntax of shell scripts.
-p
Turn on privileged mode. This mode is enabled on startup if either the effective user or group id is not equal to the real user or group id. Turning this mode off sets the effective user and group ids to the real user and group ids. Also on interactive shells and when enabled, this mode sources /etc/suid_profile (in- stead of ~/.profile) after /etc/profile and ignores the contents of the ENV variable.
-r
Invokes a restricted shell. In a restricted shell, you cannot do any of the following: use the cd command; change the values of the variables env, path or shell; use > or >> to redirect output; specify command names containing /. These restrictions do not apply during execution of profile files.
-s
Read commands from standard input (set automatically if no file arguments are present). This option has no effect when set after the shell has already started running (i.e. with set).
-t
Exits after reading and executing one command.
-T
When waiting for a child, execute traps immediately. If this option is not set, traps are executed after the child exits, as specified in IEEE Std1003.2 (``POSIX'') This nonstandard option is useful to put guarding shells around childs that block signals. The surrounding shell may kill the child or it may just re- turn control to the tty and leave the child alone.
-u
Write a message to standard error when attempting to expand a variable that is not set, and if the shell is not interactive, exit immediately.
-v
The shell writes its input to standard error as it is read. Useful for debugging.
-V
Enable the built-in vi command line editor (disables -E if it has been set).
-x
Write each command to standard error (preceded by a '+ ') before it is executed. Useful for debugging.