Linux命令入门杂谈

Linux学习任重道远,对于初学者来说,看到多如繁星的命令,就让人望而却步,接下来就说说Linux命令的一些入门学习,希望我可以给看到的人帮助。
我认为学习Linux命令分为下面三个方面,命令的格式、分类以及执行过程,最后重要的是,自己学会如何学习使用一个刚了解的命令。
一:命令的格式:
格式:命令 选项(长选项 短选项) 参数(例如 文件名 路径 用户等等)
例如:
(1、多个选项或多个参数用一个空格的空白字符隔开
 [[email protected] ~]# ls -a -l /home
total 4
drwxr-xr-x. 3 root root 18 Nov 8 16:16 .
dr-xr-xr-x. 18 root root 235 Nov 8 16:16 ..
drwx------. 14 wang wang 4096 Nov 8 16:30 wang
[[email protected] ~]#

[[email protected] ~]# touch a.txt b.txt
[[email protected] ~]# ls
anaconda-ks.cfg b.txt Documents initial-setup-ks.cfg Pictures Templates
a.txt Desktop Downloads Music Public Videos
[[email protected] ~]#

(2、一条命令很长,写不完,可以加反斜杠( \) ,分成多行来写
[[email protected] ~]# shutdown -h \
> 18:30 \
> ‘system will be down !!!‘
Shutdown scheduled for Fri 2017-11-10 18:30:00 CST, use ‘shutdown -c‘ to cancel.
[[email protected] ~]#

注意:单命令直接后跟一个反斜杠就可以,如果是带有选项和参数的命令,保持每行最后先打一个空格,在跟一个反斜杠
(3、多条命令一起执行,可以用分号( ;)隔开

[[email protected] ~]# cat /etc/issue;ls;pwd
Users is \u
Tty is \l
Time is \t
Date is \d
Hostname is \n
\S
Kernel \r on an \m
anaconda-ks.cfg b.txt Documents initial-setup-ks.cfg Pictures Templates
a.txt Desktop Downloads Music Public Videos
/root
[[email protected] ~]#

(4、取消或结束命令 ctrl+c(强制退出)或者ctrl+d(正常退出)
[[email protected] ~]# sleep 1000
^C
[[email protected] ~]#

二:命令的分类:

(1内部命令:被构建在shell之中,常驻内存,在任何时候都可以使用,执行速度非常快
(2外部命令:都是以文件的形式存在,是一个个单独的可执行文件,存放于磁盘

如何知道是内部命令还是外部命令呢?
用法:#type 后跟命令 查看给定的指令为“外部指令”、“命令别名”或者“内部指令”
-a 后跟命令 在环境变量“PATH”指定的路径中,显示给定指令的信息,包括命令别名

例如:查看cat和cd、ls是什么命令?
[[email protected] ~]# type cat
cat is /usr/bin/cat //有路径显示,表示为外部命令
[[email protected] ~]# type cd
cd is a shell builtin //显示shell builtin ,表示内部命令
[[email protected] ~]#
[[email protected] ~]# type ls
ls is aliased to `ls --color=auto‘ //显示alias,表示为命令别名
[[email protected] ~]#

注意:有些命令是内部命令,外部命令也有
例如:
[[email protected] ~]# type -a echo
echo is a shell builtin
echo is /usr/bin/echo
[[email protected] ~]#

也就解释了系统某些内部命令丢失,也还是可以执行的原因

关于内部命令的启用和禁用
用法:enable 列出已开启的内部命令
后跟命令 启用命令
-n 后跟命令 禁用命令
-n 查看被禁用的命令

举例:
查看启用的内部命令:
[[email protected] ~]# enable -a
enable .
enable :
enable [
enable alias
....................
enable unset
enable wait
[[email protected] ~]#

禁用内部命令:
>[[email protected] ~]# enable -n pwd
[[email protected] ~]#

查看被禁用的命令:
>[[email protected] ~]# help
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
These shell commands are defined internally. Type `help‘ to see this list.
Type `help name‘ to find out more about the function `name‘.
Use `info bash‘ to find out more about the shell in general.
Use `man -k‘ or `info‘ to find out more about commands not in this list.
A star (*) next to a name means that the command is disabled.
job_spec [&] history [-c] [-d offset] [n] or history -anr>
(( expression )) if COMMANDS; then COMMANDS; [ elif COMMANDS;>
. filename [arguments] jobs [-lnprs] [jobspec ...] or jobs -x comma>
: kill [-s sigspec | -n signum | -sigspec] pid>
[ arg... ] let arg [arg ...]
[[ expression ]] local [option] name[=value] ...
builtin [shell-builtin [arg ...]] pushd [-n] [+N | -N | dir]
caller [expr] *pwd [-LP]
...
hash [-lr] [-p pathname] [-dt] [name ...] while COMMANDS; do COMMANDS; done
help [-dms] [pattern ...] { COMMANDS ; }
[[email protected] ~]#

命令前面有个星号,表示已禁用,也可用enable命令查
[[email protected] ~]# enable -n
enable -n pwd
[[email protected] ~]#

关于别名命令别名使用:
用法: #alias 命令A="BBB ccc" 定义‘BBB ccc’命令的别名为命令A
      #unalias 命令A 取消别名命令A
    以上只是临时,关机失效
     #alias 查看已有可用的所有别名
例如:
[[email protected] ~]# alias
alias cp=‘cp -i‘
................................
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
[[email protected] ~]#

注意:1、在某些情况别名不想取消,又想使用定义内的原始命令,在原始命令上加单引号
例如:定义 #alias ifconfig="ifconfig ens33"
想使用ifconfig原始命令,不取消定义别名,可以这样加单引号执行即可 #‘ifconfig‘
2、以上操作重启或注销都会丢失
不丢失,需要写入文件,文件在家目录下
/home/用户/.bashrc 只对系统当前用户生效
/etc/bashrc 对系统所有用户生效

这个时候说下,对于刚修改的配置文件,想让它生效又不想重启,可以如下操作
soure 文件名 使修改后的文件立即生效
3、别名优先级大于内部命令,内部命令优先级大于外部命令

三:命令的执行过程:
系统先判断是否是内部或外部命令----是内部命令就直接执行;是外部命令,在hash缓存表查找----缓存表找不到去PATH变量路径查找---还找不到,命令不存在

命令hash缓存表的使用:
用法:
hash 显示当前系统命令缓存表
-l 显示缓存表的详细信息
-d 命令 清除指定命令缓存
-r 清除所有hash缓存
例如:
[[email protected] ~]# hash
hits command
1 /usr/bin/pwd
2 /usr/sbin/service
2 /usr/bin/cat
1 /usr/bin/touch
1 /usr/bin/sleep
4 /usr/sbin/shutdown
12 /usr/bin/ls
[[email protected] ~]# hash -l
builtin hash -p /usr/bin/pwd pwd
builtin hash -p /usr/sbin/service service
builtin hash -p /usr/bin/cat cat
builtin hash -p /usr/bin/touch touch
builtin hash -p /usr/bin/sleep sleep
builtin hash -p /usr/sbin/shutdown shutdown
builtin hash -p /usr/bin/ls ls
[[email protected] ~]#

关于环境变量路径:
echo $PATH 查看所有的环境变量
[[email protected] ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[[email protected] ~]#

当命令不是内部命令,查了hash表也没有,就会在PATH路径从左到右的顺序查找

小技巧: which cmd 查看命令的环境变量路径
-a cmd 查看命令所有存在的路径

[[email protected] ~]# which -a ls
alias ls=‘ls --color=auto‘
/usr/bin/ls
[[email protected] ~]#

四:命令的帮助:
help man info
再多的命令,人是记不完的,还是学会查看命令帮助,学会自学。
用法:
命令 --help 查看命令帮助
help 命令 查看内部命令帮助
man 命令 查看外部命令帮助
man一个命令进入详细页,空格向尾部翻屏,b向首部翻屏
d向尾部翻半屏,u向首部翻半屏
q退出信息页。
/关键词 以当前位置,向尾部搜索想要查找的关键词 n下一个,N上一个
?关键词 以当前位子,想首部搜索想要查找的关键词 n下一个,N上一个
注意看到中间有日期号,表示最后一页到了

info 命令 查看详细的命令帮助
whatis whatis命令显示手册部分的页眉行。然后可以发出man命令以获取附加的信息。whatis命令等同于使用man -f命令
如果不能用,执行命令: #makewhatis centos6的开启
# mandb centos7的开启
例如:
[[email protected] ~]# cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit
With no FILE, or when FILE is -, read standard input.
Examples:
cat f - g Output f‘s contents, then standard input, then g‘s contents.
cat Copy standard input to standard output.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils ‘cat invocation‘
[[email protected] ~]#

[[email protected] ~]# man cat | more
CAT(1) User Commands CAT(1)
NAME
cat - concatenate files and print on the standard output
SYNOPSIS
cat [OPTION]... [FILE]...
DESCRIPTION
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends
display $ at end of each line
-n, --number
number all output lines
-s, --squeeze-blank
suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs
display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version
output version information and exit
With no FILE, or when FILE is -, read standard input.
EXAMPLES
cat f - g
Output f‘s contents, then standard input, then g‘s contents.
cat Copy standard input to standard output.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report cat trans‐
lation bugs to <http://translationproject.org/team/>
AUTHOR
Written by Torbjorn Granlund and Richard M. Stallman.
COPYRIGHT
Copyright ? 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or
later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WAR‐
RANTY, to the extent permitted by law.
SEE ALSO
tac(1)
The full documentation for cat is maintained as a Texinfo manual. If the info and cat
programs are properly installed at your site, the command
info coreutils ‘cat invocation‘
should give you access to the complete manual.
GNU coreutils 8.22 November 2016 CAT(1)
[[email protected] ~]#

[[email protected] ~]# whatis cat
cat (1) - concatenate files and print on the standard output
cat (1p) - concatenate and print files
[[email protected] ~]#

时间: 2024-08-30 05:24:52

Linux命令入门杂谈的相关文章

Python入门--Linux命令入门

Linux命令入门 pwd               显示当前目录 cd                  切换目录  如:cd /   切换到根目录 cd ..               切换到上级目录 ls                   显示当前目录下的所有文件和文件夹 ls -l                列出当前目录下的文件及文件夹的详细信息 ls -a               显示当前目录下所有文件及文件夹(包括隐藏文件) vi .test           创建一

Linux 入门学习-LINUX命令行描述及常用命令

Linux基础入门之(常用命令) 1.命令行构成 命令提示符一般有GUI.GLI两种接口 1.1命令行组成:命令提示符.prompt.bash(使用的shell) 1.1.1  提示符格式默认为系统变量设置 使用echo命令输出PS1系统本地变量 [[email protected] testdir]# echo $PS1 [\[email protected]\h \W]\$ 1.1.2  用户登录提示符: $:表示普通用户 #:表示系统用户 pwd 显示当前用户所在目录 [[email pr

linux的入门命令

根目录下的一些目录或设备: /boot:系统启动相关的文件,例如内核,initrd,grub(bootloader引导加载器) /dev:设备文件 块设备:随机访问,数据块 字符设备:线性访问,按字符为单位 设备号:主设备号(major)和次设备号(minor) /etc:配置文件 /home:用户的家目录,每一个用户家目录通常默认为/home/USERNAME /root:管理员家目录 /lib:库文件及内核模块文件 内核模块文件:/lib/modules 静态库:.a 动态库:.so /me

Linux系统入门命令100条 转

https://www.howtoforge.com/linux-commands/ 2017-04-27 RiboseYim 睿哥杂货铺 Author : Himanshu Arora 原文:https://www.howtoforge.com/linux-commands/ 本文的特点是非常简洁,将繁杂的Linux命令行筛选出100条左右,非常适合入门学习.此外,将领域知识以"条目+示例"的方式来整理,类似编字典一样,在编辑的过程中可以促进学习者加深认识,也方便日后持续改进(增加注

Linux常用命令-入门篇(一)

1    For循环 语法:begin for i in reverse 1..10 loop insert into users values(i,'奥巴马'): end loop: end; 注意:循环变量 i 是隐含增加的,所以无法看到 2    goto语句 goto 语句用于跳转到特定的位置去执行语句.由于goto语句会减少程序的可读性,所以一般情况下 不建议使用goto语句 3    null语句 null语句不会执行任何操作,但是可以增加程序的可读性 4    创建返回值是一个结果

20165301 预备作业三:Linux安装及命令入门

预备作业三:Linux安装及命令入门 VirtualBox虚拟机的安装 在进行安装之前,原本以为有了娄老师的安装教程会是一件很容易的事情.万万没想到,在自己实际动手操作中,还是遇到了许多困难.通过与同学进行讨论和搜索百度引擎,最终顺利解决了大部分问题.(以下所有问题仅针对于win10系统) 首先遇到的问题是我新建虚拟机时,只能选择32位,而没有64位 百度后发现要在安装虚拟机前要检查电脑是否虚拟化 打开任务管理器-性能-CPU 如未虚拟化,需进行设置. 重启电脑按del进入电脑的BIOS界面,并

20165223 Linux安装及命令入门

预备作业3:Linux安装及命令入门 一.VirtualBox和Ubuntu的安装 通过学习实践基于VirtualBox虚拟机安装Ubuntu图文教程,我开始学习虚拟机的安装,根据教程一步步试着安装. 安装VirtualBox 我先点击进入了VirtualBox官网,却只看到了5.2.6和5.1.32两个版本,于是先试着把5.2.6下了下来,想试试看能不能正常安装.然而事情并不尽如人意,安装过程中还是遇到了一些小障碍. 在官网安装的时候下载速度特别慢 在官网中按教程下载的5.2.6版本却在官网中

[转帖]linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习)

https://www.cnblogs.com/caozy/p/9261224.html 总结的挺好的 我之前想总结 一直懒 这次国庆加班 也本来计划来着 感冒了 作罢 .. 作者:蓝藻(罗蓝国度) 创建时间:2018.7.3编辑时间:2019.7.16 前言 本文特点 授之以渔:了解命令学习方法.用途:不再死记硬背,拒绝漫无目的: 准确无误:所有命令执行通过(环境为centos7),拒绝复制粘贴: 实用性高:命令多为实际工作中用到的,实例讲解,拒绝纯理论: 条理清晰:分类归纳,快速找到想要的命

linux入门系列5--新手必会的linux命令

上一篇文章"linux入门系列4--vi/vim编辑器"我们讨论了在linux下如何快速高效对文本文件进行编辑和管理,本文将进一步学习必须掌握的linux命令,掌握这些命令才能让计算机更懂你. 就我们前文已经装好的centos操作系统而言,我们操作通常有2种方式:第一种就是通过桌面图形化操作:另外一种则是通过终端输入命令. 针对服务器而言我们通常是采用远程连接工具连接到服务器,然后以命令的方式进行管理.我们输入命令后计算机怎么就知道该做什么呢?那就得靠命令行工具(也称终端)进行翻译解释