常用的linux命令与示例,linux常用命令及用法示例

一、 帮助命令

1. 常用快捷键

快捷键

功能

ctrl + c

停止进程

ctrl+l

清屏;彻底清屏是:reset

ctrl + q

退出

tab键

提示(更重要的是可以防止敲错)

上下键

查找执行过的命令

ctrl +alt

虚拟机linux和Windows之间切换

2. man 获得帮助信息

基本语法

man [命令或配置文件] (功能描述:获得帮助信息)

显示内容说明

信息

功能

NAME

命令的名称和单行描述

SYNOPSIS

怎样使用命令

DESCRIPTION

命令功能的深入讨论

EXAMPLES

怎样使用命令的例子

SEE ALSO

相关主题(通常是手册页)

操作示例

输入命令

[root@lancer ~]# man ls

返回结果(部分)

LS(1) User Commands LS(1)

NAME

ls - list directory contents

SYNOPSIS

ls [OPTION]... [FILE]...

DESCRIPTION

List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.

-a, --all

do not ignore entries starting with .

-A, --almost-all

do not list implied . and ..

--author

with -l, print the author of each file

3. help 获得shell内置命令的帮助信息

基本语法

help 命令 (功能描述:获得shell内置命令的帮助信息)

操作示例

查看cd命令的帮助信息

[root@lancer ~]# help cd

cd: cd [-L|[-P [-e]]] [dir]

Change the shell working directory.

Change the current directory to DIR. The default DIR is the value of the

HOME shell variable.

The variable CDPATH defines the search path for the directory containing

DIR. Alternative directory names in CDPATH are separated by a colon (:).

A null directory name is the same as the current directory. If DIR begins

with a slash (/), then CDPATH is not used.

If the directory is not found, and the shell option `cdable_vars' is set,

the word is assumed to be a variable name. If that variable has a value,

its value is used for DIR.

Options:

-L force symbolic links to be followed

-P use the physical directory structure without following symbolic

links

-e if the -P option is supplied, and the current working directory

cannot be determined successfully, exit with a non-zero status

The default is to follow symbolic links, as if `-L' were specified.

Exit Status:

Returns 0 if the directory is changed, and if $PWD is set successfully when

-P is used; non-zero otherwise.

二、 文件目录类命令

1. mkdir 创建一个新的目录

基本语法

mkdir [选项] 要创建的目录

选项说明

选项

功能

-p

创建多层目录

操作示例

创建一个目录

[root@hadoop101 ~]# mkdir xiyou

创建多层目录

[root@hadoop101 ~]# mkdir -p xiyou/dssz/meihouwang

2. cp 复制文件或目录

基本语法

cp [选项] source dest (功能描述:复制source文件到dest)

选项说明

选项

功能

-r

递归复制整个文件夹

参数说明

参数

功能

source

源文件

dest

目标文件

经验技巧

强制覆盖不提示的方法:\cp

操作示例

复制文件

[root@hadoop101 ~]# cp xiyou/dssz/suwukong.txt xiyou/mingjie/

递归复制整个文件夹

[root@hadoop101 ~]# cp -r xiyou/dssz/ ./

3. rm 移除文件或目录

基本语法

rm [选项] deleteFile (功能描述:递归删除目录中所有内容)

选项说明

选项

功能

-r

递归删除目录中所有内容

-f

强制执行删除操作,而不提示用于进行确认。

-v

显示指令的详细执行过程

操作示例

删除目录中的内容

[root@hadoop101 ~]# rm xiyou/mingjie/sunwukong.txt

递归删除目录中所有内容

[root@hadoop101 ~]# rm -rf dssz/

4. cat 查看文件内容

基本语法

cat [选项] 要查看的文件

选项说明

选项

功能描述

-n

显示所有行的行号,包括空行。

经验技巧

一般查看比较小的文件,一屏幕能显示全的。

5. more 文件内容分屏查看器

基本语法

more [要查看的文件]

more指令是一个基于VI编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容。more指令中内置了若干快捷键,详见操作说明。

操作说明

操作

功能说明

空白键 (space)

代表向下翻一页;

Enter

代表向下翻『一行』;

q

代表立刻离开 more ,不再显示该文件内容。

Ctrl+F

向下滚动一屏

Ctrl+B

返回上一屏

=

输出当前行的行号

:f

输出文件名和当前行的行号

6. less 分屏显示文件内容

基本语法

less [要查看的文件]

less指令用来分屏查看文件内容,它的功能与more指令类似,但是比more指令更加强大,支持各种显示终端。less指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示需要加载内容,对于显示大型文件具有较高的效率.

操作说明

操作

功能说明

空白键

向下翻动一页;

[pagedown]

向下翻动一页

[pageup]

向上翻动一页;

/字串

向下搜寻『字串』的功能;n:向下查找;N:向上查找;

?字串

向上搜寻『字串』的功能;n:向上查找;N:向下查找;

q

离开 less 这个程序;

7. echo 输出内容到控制台

基本语法

echo [选项] [输出内容]

选项说明

-e: 支持反斜线控制的字符转换

控制字符

作用

\

输出\本身

\n

换行符

\t

制表符,也就是Tab键

操作示例

[root@lancer ~]# echo "hello\tworld"

hello\tworld

[root@lancer ~]# echo -e "hello\tworld"

hello world

[root@lancer ~]#

8.head 显示文件头部内容

基本语法

head 文件 (功能描述:查看文件头10行内容)

head -n 5 文件 (功能描述:查看文件头5行内容,5可以是任意行数)

选项说明

选项

功能

-n

指定显示头部内容的行数

操作示例

查看文件的头2行

[root@lancer ~]# head -n 2 anaconda-ks.cfg

#version=DEVEL

# System authorization information

[root@lancer ~]#

9.tail 输出文件尾部内容

基本语法

(1)tail 文件 (功能描述:查看文件后10行内容)

(2)tail -n 5 文件 (功能描述:查看文件后5行内容,5可以是任意行数)

(3)tail -f 文件 (功能描述:实时追踪该文档的所有更新)

选项说明

选项

功能

-n

输出文件尾部n行内容

-f

显示文件最新追加的内容,监视文件变化

操作示例

查看文件最后1行内容

[root@hadoop101 ~]# tail -n 1 smartd.conf

实时追踪该档的所有更新

[root@hadoop101 ~]# tail -f houge.txt

10. > 覆盖 和 >> 追加

基本语法

(1)ll >文件 (功能描述:列表的内容写入文件a.txt中(覆盖写))

(2)ll >>文件 (功能描述:列表的内容追加到文件aa.txt的末尾)

(3)cat 文件1 > 文件2 (功能描述:将文件1的内容覆盖到文件2)

(4)echo “内容” >> 文件 (功能描述:将内容追加到到文件的末尾)

操作示例

将ls查看信息写入到文件中

[root@hadoop101 ~]# ls -l>houge.txt

将ls查看信息追加到文件中

[root@hadoop101 ~]# ls -l>>houge.txt

采用echo将hello单词追加到文件中

[root@hadoop101 ~]# echo hello>>houge.txt

11. ln 软链接

基本语法

ln -s [原文件或目录] [软链接名] (功能描述:给原文件创建一个软链接)

软链接也成为符号链接,类似于windows里的快捷方式,有自己的数据块,主要存放了链接其他文件的路径。

操作示例

创建软连接

[root@lancer ~]# pwd

/root

[root@lancer ~]# ll

total 8

-rw-------. 1 root root 1953 Feb 11 23:44 anaconda-ks.cfg

-rw-r--r--. 1 root root 2001 Feb 11 23:59 initial-setup-ks.cfg

[root@lancer ~]# ln -s /home/lancer ./homeLink

[root@lancer ~]# ll

total 8

-rw-------. 1 root root 1953 Feb 11 23:44 anaconda-ks.cfg

lrwxrwxrwx. 1 root root 12 Feb 13 12:55 homeLink -> /home/lancer

-rw-r--r--. 1 root root 2001 Feb 11 23:59 initial-setup-ks.cfg

[root@lancer ~]#

删除软连接(注意删除时最后不能带斜杠,带斜杠时删除无效)

[root@lancer ~]# rm -rf homeLink/

[root@lancer ~]# ll

total 8

-rw-------. 1 root root 1953 Feb 11 23:44 anaconda-ks.cfg

lrwxrwxrwx. 1 root root 12 Feb 13 12:55 homeLink -> /home/lancer

-rw-r--r--. 1 root root 2001 Feb 11 23:59 initial-setup-ks.cfg

[root@lancer ~]# rm -rf homeLink

[root@lancer ~]# ll

total 8

-rw-------. 1 root root 1953 Feb 11 23:44 anaconda-ks.cfg

-rw-r--r--. 1 root root 2001 Feb 11 23:59 initial-setup-ks.cfg

[root@lancer ~]#

进入软连接实际物理路径

注意: 直接cd到软连接目录时,输入pwd命令显示的是软连接目录; 输入cd -P进入软连接目录时,输入pwd命令显示的是真实目录。

[root@lancer ~]# ln -s /home/lancer ./homeLink

[root@lancer ~]# ll

total 8

-rw-------. 1 root root 1953 Feb 11 23:44 anaconda-ks.cfg

lrwxrwxrwx. 1 root root 12 Feb 13 12:58 homeLink -> /home/lancer

-rw-r--r--. 1 root root 2001 Feb 11 23:59 initial-setup-ks.cfg

[root@lancer ~]# cd homeLink/

[root@lancer homeLink]# pwd

/root/homeLink

[root@lancer homeLink]# cd /root/

[root@lancer ~]# ll

total 8

-rw-------. 1 root root 1953 Feb 11 23:44 anaconda-ks.cfg

lrwxrwxrwx. 1 root root 12 Feb 13 12:58 homeLink -> /home/lancer

-rw-r--r--. 1 root root 2001 Feb 11 23:59 initial-setup-ks.cfg

[root@lancer ~]# cd -P homeLink/

[root@lancer lancer]# pwd

/home/lancer

[root@lancer lancer]#

12.history 查看已经执行过历史命令

基本语法

history (功能描述:查看已经执行过历史命令)

操作示例

[root@lancer lancer]# history

1 ll

2 pwd

3 man ls

4 help cd

经验技巧

输入 !+历史命令编号,可以直接重复执行历史命令

[root@lancer lancer]# !2

pwd

/home/lancer

[root@lancer lancer]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值