0


Linux 基础入门(学习笔记通俗易懂版)

Linux基础入门(学习笔记通俗易懂版)

本文章是学习了Linux的学习记录,着重记录了我对于Linux各命令的用法与感悟,欢迎各位大佬批评指正

记录者:CYH-BI

记录日期:2023年7月7日

本篇文章将使用虚拟机并安装centos 进行实操。关于虚拟机的安装请看其他教程,篇幅过长,不一展示。

Linux简介部分

Linux起初由Linus Torvalds与1991年编写,本质上是一个UNIX操作系统。目前的发行版本有Linux内核、shell环境、桌面软件、各类系统管理软件和应用软件共同构成的一个完整的Linux操作系统。在终端输入命令后,系统将命令输给内核执行,将返回的结果输出到指定位置。

根文件系统结构简介

文件系统结构与window系统类似(但差异较大),Linux系统含有一个根目录(/),该目录下又含有众多二级目录,如:

  • /bin (binary):存放可执行文件
  • /dev (device):存放设备文件和特殊文件 (比如光盘)
  • /etc :存放系统配置文件
  • /home :普通的主目录所在位置
  • /lib :存放基本共享文件和内核模块
  • /mut (mount): 用于为需要挂在的文件系统提供挂在点(比如U盘)
  • /proc (process):存放于内核和进程有关的信息
  • /root :根目录的主目录
  • /usr (user):存放可共享的只读数据文件
  • /var (variable):存放分类数据文件

路经的使用于window类似,比如我要进入某用户(lh)目录下的my目录,那么全路径为:**/home/lh/my**

第一个 / 表示根目录,lh用户在home目录下,my目录在lh下。

基本命令用法即实操

  • shutdown功能:关闭、重启系统命令格式:shutdown[选项] 时间主要参数:**-r** (reboot) :重启系统**-h** (halt) :关闭系统**-P** (poweroff) :关机(关闭系统与电源)用法举例:shutdown-h5#5分钟后关机shutdown-r#立即重启系统shutdown-r5#5分钟后重启系统#上述太麻烦了,花里胡哨的,我一般使用下面的reboot#重启系统poweroff #立即关机
  • clear :清除当前屏幕(或终端)上显示的内容
  • su :普通用户转root用户,普通用户是 $ 开头,root 用户是 # 开头。该命令需要输入密码,且密码不可见,需要盲打。
  • man (manual):显示命令的说明手册命令格式:man 命令名如果你某个命令忘记了如何使用或者是命令有哪些参数,该命令可以给出详解,举例如下[cyh@localhost Desktop]$ manshutdown#回车,弹出说明文档,按q退出man 命令麻烦了一点,我更喜欢命令加 –help 来显示用法,比如:[cyh@localhost Desktop]$ shutdown--help#直接在屏幕输出各参数以及用法shutdown[OPTIONS...][TIME][WALL...]Shut down the system. --help Show this help-H--halt Halt the machine -P--poweroff Power-off the machine -r--reboot Reboot the machine -h Equivalent to --poweroff, overridden by --halt-k Don't halt/power-off/reboot, just send warnings --no-wall Don't send wall message before halt/power-off/reboot -c Cancel a pending shutdown[cyh@localhost Desktop]$
  • history :查看你使用过的所有命令,默认显示所有,可以指定显示条数(最近的)命令格式:history[查看命令行数]用法举例:[cyh@localhost Desktop]$ history10#显示最后10行448vim log2.sh 449cat log2.sh 450bash log2.txt 451bash log2.sh 452who453who-q454manshutdown455shutdown--help456history-10457history10
  • ls (list)命令功能:默认显示当前目录下的文件列表。如果所给参数是指定到某文件,则列出该文件有关信息主要选项:-a (all):表示列出目录所有项,包括以“ . ”开始的隐藏文件-l (list):以列表形式列出文件-d (directory):仅列出目录本身信息-R(recursive):递归列出子目录内容部分举例如下:[cyh@localhost Desktop]$ ls#在我的桌面上有6个文件,使用ls命令显示gen2go.txt gene2go log2.sh log2.txt log.txt shiyan5[cyh@localhost Desktop]$ ls ./shiyan5 #列出shiyan5中所有文件,"./"表示当前文件路径,是"/home/cyh/Desktop"的简写,与ls /home/cyh/Desktop/shiyan5是一样的guess_number.sh identical_line.shidentical_filebreaklines2.sh identical_user.shidentical_filebreaklines.sh sum_and_product2.shidentical_file.sh[cyh@localhost Desktop]$ ls-l#-l参数,以列表形式列出文件total 218464-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 gen2go.txt-rw-rw-r--. 1 cyh cyh 194901303 Jun 13 03:58 gene2go-rw-rw-r--. 1 cyh cyh 63 Jul 6 09:37 log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 log.txtdrwxrwxr-x. 2 cyh cyh 233 Apr 22 03:27 shiyan5[cyh@localhost Desktop]$ ls-a#表示列出目录所有项,包括以“ . ”开始的隐藏文件,结果显示有两个隐藏文件... gen2go.txt gene2go log2.sh log2.txt log.txt shiyan5[cyh@localhost Desktop]$ ls-d#仅列出目录本身信息.[cyh@localhost Desktop]$ ls-R#递归列出子目录,在shiyan5文件中,含有子文件,也一一列出.:gen2go.txt gene2go log2.sh log2.txt log.txt shiyan5./shiyan5:guess_number.sh identical_line.shidentical_filebreaklines2.sh identical_user.shidentical_filebreaklines.sh sum_and_product2.shidentical_file.sh[cyh@localhost Desktop]$ ls-lR#参数可以叠加使用,以列表的形式递归列出文件.:total 218464-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 gen2go.txt-rw-rw-r--. 1 cyh cyh 194901303 Jun 13 03:58 gene2go-rw-rw-r--. 1 cyh cyh 63 Jul 6 09:37 log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 log.txtdrwxrwxr-x. 2 cyh cyh 233 Apr 22 03:27 shiyan5./shiyan5:total 28-rw-rw-r--. 1 cyh cyh 361 Apr 22 03:24 guess_number.sh-rw-rw-r--. 1 cyh cyh 228 Apr 22 02:16 identical_filebreaklines2.sh-rw-rw-r--. 1 cyh cyh 338 Apr 22 02:05 identical_filebreaklines.sh-rw-rw-r--. 1 cyh cyh 205 Apr 22 01:01 identical_file.sh-rwxrw-r--. 1 cyh cyh 191 Apr 21 02:18 identical_line.sh-rwxrw-r--. 1 cyh cyh 214 Apr 21 03:06 identical_user.sh-rw-rw-r--. 1 cyh cyh 185 Apr 22 02:56 sum_and_product2.sh
  • pwd (print working directory)命令,字面意思,打印当前的目录(工作目录)完整路径命令格式:pwd[选项]举例如下:[cyh@localhost Desktop]$ pwd/home/cyh/Desktop
  • cd (change directory)更改当前目录,不加参数转跳到用户的主目录命令格式:cd[选项][文件或目录路径]举例如下:[cyh@localhost Desktop]$ cd ./shiyan5 #更改路径到shiyan5[cyh@localhost shiyan5]$ pwd#显示当前目录完整路径/home/cyh/Desktop/shiyan5[cyh@localhost shiyan5]$ cd#不加任何参数,转跳至用户主目录,可看下行"~"[cyh@localhost ~]$ cd /home/cyh/Desktop/shiyan5 #使用全路径[cyh@localhost shiyan5]$ #到达shiyan5目录``````[cyh@localhost shiyan5]$ cd..#返回上一层目录[cyh@localhost Desktop]$ cd../..#返回上两层目录
  • stat 获取某文件基本信息命令格式:stat 文件或路径举例:[cyh@localhost shiyan5]$ ll #查看以下该目录下有什么文件total 28-rw-rw-r--. 1 cyh cyh 361 Apr 22 03:24 guess_number.sh-rw-rw-r--. 1 cyh cyh 228 Apr 22 02:16 identical_filebreaklines2.sh-rw-rw-r--. 1 cyh cyh 338 Apr 22 02:05 identical_filebreaklines.sh-rw-rw-r--. 1 cyh cyh 205 Apr 22 01:01 identical_file.sh-rwxrw-r--. 1 cyh cyh 191 Apr 21 02:18 identical_line.sh-rwxrw-r--. 1 cyh cyh 214 Apr 21 03:06 identical_user.sh-rw-rw-r--. 1 cyh cyh 185 Apr 22 02:56 sum_and_product2.sh[cyh@localhost shiyan5]$ stat ./sum_and_product2.sh #查看该目录下sum_and_product2.sh文件,结果如下所示 File: ‘./sum_and_product2.sh’ Size: 185 Blocks: 8 IO Block: 4096 regular fileDevice: fd00h/64768d Inode: 78261 Links: 1Access: (0664/-rw-rw-r--) Uid: (1000/ cyh) Gid: (1000/ cyh)Context: unconfined_u:object_r:user_home_t:s0Access: 2023-04-22 02:56:37.126073472 -0400Modify: 2023-04-22 02:56:33.784094697 -0400Change: 2023-04-22 02:56:33.785094676 -0400 Birth: -[cyh@localhost shiyan5]$
  • touch 命令更新一个文件的访问和修改时间,如果没有该文件则新建该文件(我一般用来新建文件)命令格式:touch 文件或路径目录举例:[cyh@localhost Desktop]$ touch first_file.txt #在桌面上新建first_file.txt文件[cyh@localhost Desktop]$ ll #查看以下桌面文件,ll为' ls -l '的简写total 218464-rw-rw-r--. 1 cyh cyh 0 Jul 1112:07 first_file.txt-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 gen2go.txt-rw-rw-r--. 1 cyh cyh 194901303 Jun 13 03:58 gene2go-rw-rw-r--. 1 cyh cyh 63 Jul 6 09:37 log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 log.txtdrwxrwxr-x. 2 cyh cyh 233 Apr 22 03:27 shiyan5[cyh@localhost Desktop]$
  • mkdir 命令:新建目录部分参数:-p 递归创建目录-m 给文件赋予某权限(请看下一个命令)命令格式:mkdir 目录路径举例:[cyh@localhost Desktop]$ mkdir first_dir #在当前目录下创建first_dir文件夹[cyh@localhost Desktop]$ ls-l#查看结果中是否有first_dir文件(结果第一个就是)total 218464drwxrwxr-x. 2 cyh cyh 6 Jul 1112:14 first_dir-rw-rw-r--. 1 cyh cyh 0 Jul 1112:07 first_file.txt-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 gen2go.txt-rw-rw-r--. 1 cyh cyh 194901303 Jun 13 03:58 gene2go-rw-rw-r--. 1 cyh cyh 63 Jul 6 09:37 log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 log.txtdrwxrwxr-x. 2 cyh cyh 233 Apr 22 03:27 shiyan5

看到这里,新手就有疑惑了,有很多东西看不懂,比如,使用 ll -l 命令返回结果怎么看,那么请看如下:

drwxrwxr-x. 2 cyh  cyh          6 Jul 1112:14 first_dir
-rw-rw-r--. 1 cyh  cyh          0 Jul 1112:07 first_file.txt

在这里我截取了部分文件信息,我拆开来解释:

drwxrwxr-x. 2 cyh  cyh          6 Jul 1112:14 first_dir
第一个字母 d ,表示这是一个目录(directory),如果是 - ,代表这是个文件
rwxrwxr-x ,r 代表可读(read), w 代表可写(write),x 代表可执行,- 代表没有
           前三个表示管理员(root)对该文件所拥有的权限(rwx,可读可写可执行)
           中间三个表示该文件的拥有者对该文件所拥有的权限(rwx,可读可写可执行)
           左后三个表示除了上面两种,其他人对该文件所拥有的权限(r-x,可读不可写可执行)
           rwx可用数字表示,r=4,w=2,x=1
cyh 第一个表示用户名,第二个表示所属组
6 Jul 1112:14 创建或修改日期

创建一个最高权限的first_file2目录

mkdir-m777  first_file2

第一个7,表示管理员拥有的权限,4+2+1 (rwx),同理,第二个表示目录所属者,第三个所属组其他人

  • mv 命令功能:移动或重命名文件或目录命令格式:mv[选项] 源文件或目录路径 目标文件或目录路径主要选项:**-b** (backup):如果存在同名文件,覆盖前先备份原来的文件。**-f** (force):强制覆盖同名文件使用举例:[cyh@localhost first_dir]$ touch test1 test2 #先创建两个文件[cyh@localhost first_dir]$ ll #查看一下结果,该命令为 ls -l 的缩写total 0-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:05 test1-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:05 test2[cyh@localhost first_dir]$ mv-b test2 test1 #将test2改名为test1[cyh@localhost first_dir]$ ll #查看一下结果,该命令为 ls -l 的缩写,test1~表示备份文件total 0-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:05 test1-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:05 test1~[cyh@localhost first_dir]$ mv test1 /home/cyh/Desktop/shiyan5 #将test1移动到这个目录[cyh@localhost first_dir]$ ll /home/cyh/Desktop/shiyan5 #查看结果,有test1文件total 28-rw-rw-r--. 1 cyh cyh 361 Apr 22 03:24 guess_number.sh-rw-rw-r--. 1 cyh cyh 228 Apr 22 02:16 identical_filebreaklines2.sh-rw-rw-r--. 1 cyh cyh 338 Apr 22 02:05 identical_filebreaklines.sh-rw-rw-r--. 1 cyh cyh 205 Apr 22 01:01 identical_file.sh-rwxrw-r--. 1 cyh cyh 191 Apr 21 02:18 identical_line.sh-rwxrw-r--. 1 cyh cyh 214 Apr 21 03:06 identical_user.sh-rw-rw-r--. 1 cyh cyh 185 Apr 22 02:56 sum_and_product2.sh-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:05 test1[cyh@localhost first_dir]$
  • cp (copy)复制文件或目录命令格式:cp[选项] 源文件或者目录路径 目标文件或路径主要选项:**-f** (force):强制覆盖同名文件**-b** (backup):如果存在同名文件,覆盖前先备份原来的文件。**-r** (recursive):以递归的方式复制文件(一个目录里可能还有目录,该该项可以复制目录里面的所有所有)举例:[cyh@localhost first_dir]$ ll #查看first_dir内容total 0-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:05 test1~[cyh@localhost first_dir]$ cp /home/cyh/Desktop/shiyan5/test1 /home/cyh/Desktop/first #将shiyan5中的test1文件复制到first_dir中_dir[cyh@localhost first_dir]$ ll #再次查看first_dir内容total 0-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:22 test1-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:05 test1~[cyh@localhost first_dir]$ ll /home/cyh/Desktop/shiyan5 #查看shiyan5内容,test1还在total 28-rw-rw-r--. 1 cyh cyh 361 Apr 22 03:24 guess_number.sh-rw-rw-r--. 1 cyh cyh 228 Apr 22 02:16 identical_filebreaklines2.sh-rw-rw-r--. 1 cyh cyh 338 Apr 22 02:05 identical_filebreaklines.sh-rw-rw-r--. 1 cyh cyh 205 Apr 22 01:01 identical_file.sh-rwxrw-r--. 1 cyh cyh 191 Apr 21 02:18 identical_line.sh-rwxrw-r--. 1 cyh cyh 214 Apr 21 03:06 identical_user.sh-rw-rw-r--. 1 cyh cyh 185 Apr 22 02:56 sum_and_product2.sh-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:05 test1[cyh@localhost first_dir]$ cp-r /home/cyh/Desktop/shiyan5 /home/cyh/Desktop/first_di #复制目录,将shiyan5整个目录复制到first_dir中r[cyh@localhost first_dir]$ ll #查看一下first_dir中的内容,存在shiyan5且是个目录total 0drwxrwxr-x. 2 cyh cyh 246 Jul 13 01:26 shiyan5-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:22 test1-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:05 test1~[cyh@localhost first_dir]$ ll /home/cyh/Desktop #查看一下桌面上的shiyan5还在不在,答案是在total 218464drwxrwxr-x. 3 cyh cyh 48 Jul 13 01:26 first_dir-rw-rw-r--. 1 cyh cyh 0 Jul 1112:07 first_file.txt-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 gen2go.txt-rw-rw-r--. 1 cyh cyh 194901303 Jun 13 03:58 gene2go-rw-rw-r--. 1 cyh cyh 63 Jul 6 09:37 log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 log.txtdrwxrwxr-x. 2 cyh cyh 246 Jul 13 01:11 shiyan5[cyh@localhost first_dir]$当一个目录里有许多文件时,复制该目录时,应使用递归复制(-r),否则报错哦!
  • rm (remove):该命令用于删除文件或目录命令格式:rm[选项] 文件或路径-f (force):强制删除文件**-r** (recursive):递归的意思,可以删除某目录下的所有文件基子目录用法举例:[cyh@localhost first_dir]$ ll #查看该目录下有什么total 0drwxrwxr-x. 2 cyh cyh 246 Jul 13 01:26 shiyan5-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:22 test1-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:05 test1~[cyh@localhost first_dir]$ rm test1~ #删除test1~备份文件[cyh@localhost first_dir]$ ll #查看结果,删掉了total 0drwxrwxr-x. 2 cyh cyh 246 Jul 13 01:26 shiyan5-rw-rw-r--. 1 cyh cyh 0 Jul 13 01:22 test1[cyh@localhost first_dir]$ rm-r /home/cyh/Desktop/first_dir #递归删除first_dir目录中的内容[cyh@localhost first_dir]$ ll #查看结果,显示温控total 0[cyh@localhost first_dir]$有些文件需要还能加参数 -f ,但是请谨慎操作,一旦使用 -f 删除,就永久删除,无法找回。
  • rmdir :删除目录,特别注意只能删除空目录用法举例:[cyh@localhost shiyan1]$ lltotal 0drwxrwxr-x. 2 cyh cyh 6 Jul 29 09:58 shiyan2drwxrwxr-x. 3 cyh cyh 21 Jul 29 09:59 shiyan_feikng[cyh@localhost shiyan1]$ rmdir shiyan_feikngrmdir: failed to remove ‘shiyan_feikng’: Directory not empty #提示错误,目录不为空[cyh@localhost shiyan1]$ ll shiyan2 #shiyan2为空total 0[cyh@localhost shiyan1]$ rmdir shiyan2 #空目录删除[cyh@localhost shiyan1]$ lltotal 0drwxrwxr-x. 3 cyh cyh 21 Jul 29 09:59 shiyan_feikng
  • cat (concatenate) :查看文件内容命令格式:cat[选项] 文件路径-n 显示行号(number)用法举例:[cyh@localhost shiyan1]$ cat sample.txt #当前目录下查看sample.txt文件内容The linux system is greatI like it2023.07.29[cyh@localhost shiyan1]$ cat-n sample.txt #显示行号12 The linux system is great 3 I like it 42023.07.29[cyh@localhost shiyan1]$ 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 exitWith 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'
  • more :显示内容,首先显示一个屏幕的内容,如果内容过多,一个屏幕显示不下,需要按Enter 键显示下一行,按Space 显示下一屏的内容,按Ctrl + c 退出。命令格式:more 文件路径用法举例,我手套有个人类基因组功能注释文件,很大。[cyh@localhost Desktop]$ more gen2go.txt96061 GO:0003674 ND enables molecular_function - Function96061 GO:0005576 HDA located_in extracellular region 27068509 Component96061 GO:0005576 IDA located_in extracellular region 3458201 Component96061 GO:0005576 TAS located_in extracellular region - Component96061 GO:0005615 HDA located_in extracellular space 16502470 Component96061 GO:0005886 IBA is_active_in plasma membrane 21873635 Component96061 GO:0008150 ND involved_in biological_process - Process96061 GO:0031093 TAS located_in platelet alpha granule lumen -Component96061 GO:0034774 TAS located_in secretory granule lumen - Component96061 GO:0062023 HDA located_in collagen-containing extracellularmatrix 27559042 Component--More--(0%)
  • tail :显示文本文件末尾部分,默认显示10行命令格式:tail[选项] 文件路径-n 指定显示行数用法举例:[cyh@localhost Desktop]$ tail gen2go.txt #默认显示最后10行9606124188214 GO:0003677 IEA enables DNA binding - Function9606124188214 GO:0005634 IEA located_in nucleus - Component9606124188214 GO:0030527 IEA enables structural constituent of chromatin - Function9606124188214 GO:0046982 IEA enables protein heterodimerization activity - Function9606125316803 GO:0005515 IPI enables protein binding 32958672 Function9606125316803 GO:0005615 IDA located_in extracellular space 32958672 Component9606125316803 GO:0005730 IDA located_in nucleolus 32958672 Component9606127138866 GO:0003674 ND enables molecular_function - Function9606127138866 GO:0008150 ND involved_in biological_process -Process9606127138866 GO:0016020 IEA located_in membrane - Component[cyh@localhost Desktop]$ tail-n2 gen2go.txt #指定显示最后两行9606127138866 GO:0008150 ND involved_in biological_process -Process9606127138866 GO:0016020 IEA located_in membrane - Component[cyh@localhost Desktop]$ tail--help#查看其他参数及参数详解Usage: tail[OPTION]... [FILE]...Print the last 10 lines of each FILE to standard output.With more than one FILE, precede each with a header giving the file name.With no FILE, or when FILE is -, read standard input.Mandatory arguments to long options are mandatory for short options too. -c, --bytes=K output the last K bytes; or use -c +K to output bytes starting with the Kth of each file -f, --follow[={name|descriptor}] output appended data as the file grows; an absent option argument means 'descriptor'-F same as --follow=name --retry -n, --lines=K output the last K lines, instead of the last 10; or use -n +K to output starting with the Kth --max-unchanged-stats=N with --follow=name, reopen a FILE which has not changed size after N (default 5) iterations to see if it has been unlinked or renamed (this is the usual case of rotated log files); with inotify, this option is rarely useful --pid=PID with -f, terminate after process ID, PID dies -q, --quiet, --silent never output headers giving file names --retry keep trying to open a fileif it is inaccessible -s, --sleep-interval=N with -f, sleepfor approximately N seconds (default 1.0) between iterations; with inotify and --pid=P, check process P at least once every N seconds -v, --verbose always output headers giving file names --help display this help and exit--version output version information and exit
  • wc (word count) 显示文件行数,单词数,字节数等命令格式:wc[选项] 文件列表或目录-c (character) :显示文件字节数**-l** (line) :显示行数**-w** (word) : 显示单词数用法举例:[cyh@localhost shiyan1]$ wc-clw sample.txt #查看文件行数4,单词数9,字节数484948 sample.txt[cyh@localhost shiyan1]$ cat sample.txt #确认文件(第一行什么都没有,没有内容,也是一行)The linux system is greatI like it2023.07.29[cyh@localhost shiyan1]$ wc--help#查看全部选项详解Usage: wc[OPTION]... [FILE]... or: wc[OPTION]... --files0-from=FPrint newline, word, and byte counts for each FILE, and a total line ifmore than one FILE is specified. With no FILE, or when FILE is -,read standard input. A word is a non-zero-length sequence of charactersdelimited by white space.The options below may be used to selectwhich counts are printed, always inthe following order: newline, word, character, byte, maximum line length. -c, --bytes print the byte counts -m, --chars print the character counts -l, --lines print the newline counts --files0-from=F read input from the files specified by NUL-terminated names infile F; If F is - thenread names from standard input -L, --max-line-length print the length of the longest line -w, --words print the word counts --help display this help and exit--version output version information and exitGNU coreutils online help: <http://www.gnu.org/software/coreutils/>For complete documentation, run: info coreutils 'wc invocation'
  • date 查看时间(电脑或服务器系统显示时间),也可以修改系统时间举例:[cyh@localhost shiyan1]$ date#2023年7月29号下午十点45分55秒星期六Sat Jul 2910:45:55 EDT 2023我一般用于查看脚本运行了多少时间,在命令前后各一个,如下[cyh@localhost Desktop]$ cat log2.sh #先查看一下脚本内容#!/bin/bashecho$(date"+%Y-%m-%d %H:%M:%S")#显示运行for循环结束前系统时间foriin{1,2,3}#for循环doecho"I like R "#循环输出该内容doneecho$(date"+%Y-%m-%d %H:%M:%S")#显示运行for循环结束后系统时间[cyh@localhost Desktop]$ bash log2.sh #运行脚本,时间太短,2023-07-29 10:54:34I like RI like RI like R2023-07-29 10:54:34[cyh@localhost Desktop]$
  • who :列出当前系统的登入用户命令格式:who[选项]-q 显示当前所有登录的用户名及当前在线人数用法举例:[cyh@localhost Desktop]$ who#我远程登入使用两个窗口,所以有pts/0与pts/1之分cyh :0 2023-07-29 09:56 (:0)cyh pts/0 2023-07-29 09:57 (192.168.1.9)cyh pts/1 2023-07-29 10:10 (192.168.1.9)[cyh@localhost Desktop]$ who-q#一个root(cyh),其他是远程登录的不同窗口(可以认为一个账户户不同登入)cyh cyh cyh# users=3[cyh@localhost Desktop]$

文本编辑

在看到这里,观众老爷们会问,什么是文本编辑。这个问题好哇!很容易理解,其实就是编辑文本,像word一样,改变文本内容。常用的文本编辑器是vi ,不同的发行版也有其他的文本编辑器,比如:geditEmacsnano 等。我一般使用vimvimvi 的升级版。

vim 编辑器有三种工作模式:命令模式、编辑模式、末行模式。

  • 命令模式使用 vi [文件路径] ** 就可以启动vim** 编辑器进入命令模式,文件路径可以指定编辑文件所在位置,如果文件不存在就会创建一个新的文件。使用vi [文件路径] ** 命令后会转调至编辑页面,此时文件还无法输入字符。需要按某个字母就可以进行编辑,并且编辑器最后一行会显示“–INSERT–”** 就表明进入编辑模式可以进行编辑。字母有以下:- i :按i 之后就可以在光标当前位置输入字符,并进入编辑模式- a :在当前光标的下一个位置开始输入字符,并进入编辑模式- /字符串 :该命令非常方便定位在第一个特定字符后。按下**/** 后屏幕底部会出现**/** ,在**/** 号的后面输入你需要搜索的字符,后按Enter 后会从当前位置向尾部搜索,并定位在第一个匹配的字符串旁。非常方便在文字较多的文本。- 等等还有好多(自行探索)
  • 编辑模式在使用命令模式按相应所需字符后,就可以进入编辑模式,该模式下就可以进行文本编辑。如果需要退出就需要按Esc 键重新回到命令模式
  • 末行模式当文本编辑完成后,按Esc 键由编辑模式回到命令模式,在按 号(冒号) 就可以进入末行模式,并在编辑器底部显示 “ : ” ,后面接末行模式下命令提示符,命令提示符如下:- w :写入到启动编辑器时你定义的指定路径下的文件中,可以理解为保存文件。- q :退出编辑器,会提示你保存更改后文件,如果没有更改文件就不会有提示- q! :强制退出编辑器,所编辑的文件全部 保存。- wq :保存文本并退出编辑器- wq! : 强制性保存文本并退出编辑器

用法举例:

vim /home/cyh/Desktop/sample.txt  #使用该命令后会进入编辑器,进入命令模式#按命令模式中你所需要的字符,例如 i ,在当前光标位置输入文本,编辑器底部出现--INSERT--,进入编辑模式# 编辑文本....   编辑后按Esc键,退出编辑模式,进入命令模式,按冒号进入末行模式,编辑器底部出现冒号#在冒号后输入末行模式你需要的字符后回车,系统按你输入的指令执行

符号、特殊符号、正则表达式、重定向、管道符的使用

该部分是在学习前面部分的进阶。灵活度与难度较大。但勤加练习又很简单

通配符

通配符号有好多种,包括 ** * ** 、**?** 、**[ ]** 、**-** 、

  • ***** 用于用于表示任意长度的任何字符用法举例:[cyh@localhost etc]$ cd /etc/abrt[cyh@localhost abrt]$ lltotal 12-rw-r--r--. 1 root root 850 Oct 12020 abrt-action-save-package-data.conf-rw-r--r--. 1 root root 2118 Oct 12020 abrt.conf-rw-r--r--. 1 root root 31 Oct 12020 gpg_keys.confdrwxr-xr-x. 2 root root 95 Mar 12 09:47 plugins[cyh@localhost abrt]$ ls-l /etc/abrt/*.conf #查看该文件下以.conf结尾的文件-rw-r--r--. 1 root root 850 Oct 12020 /etc/abrt/abrt-action-save-package-data.conf-rw-r--r--. 1 root root 2118 Oct 12020 /etc/abrt/abrt.conf-rw-r--r--. 1 root root 31 Oct 12020 /etc/abrt/gpg_keys.conf
  • ? 表示任意一个字符,能够更准确的找到忘记文件名有摸棱两可的文件。用法举例:[cyh@localhost Desktop]$ lltotal 218464-rw-rw-r--. 1 cyh cyh 0 Jul 1112:07 first_file.txt-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 gen2go.txt-rw-rw-r--. 1 cyh cyh 194901303 Jun 13 03:58 gene2go-rw-rw-r--. 1 cyh cyh 124 Jul 2910:53 log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 log.txtdrwxrwxr-x. 3 cyh cyh 45 Jul 2910:09 shiyan1drwxrwxr-x. 2 cyh cyh 246 Jul 13 01:11 shiyan5[cyh@localhost Desktop]$ ls-l /home/cyh/Desktop/???.txt-rw-r--r--. 1 root root 236 Jul 523:12 /home/cyh/Desktop/log.txt[cyh@localhost Desktop]$ ls-l /home/cyh/Desktop/????.txt-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 /home/cyh/Desktop/log2.txt[cyh@localhost Desktop]$ ls-l /home/cyh/Desktop/??.txtls: cannot access /home/cyh/Desktop/??.txt: No such file or directory[cyh@localhost Desktop]$ ls-l /home/cyh/Desktop/l??.txt-rw-r--r--. 1 root root 236 Jul 523:12 /home/cyh/Desktop/log.txt[cyh@localhost Desktop]$由上面的结果可以看出,必须是一个问号匹配一个字符。
  • [ ] 、**-** 用于指定符号或者是数值的范围, 表示非(相反的)用法举例:[cyh@localhost Desktop]$ ls-l /home/cyh/Desktop/[a-z]*.* #显示a到z开头的所有格式的文件-rw-rw-r--. 1 cyh cyh 0 Jul 1112:07 /home/cyh/Desktop/first_file.txt-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 /home/cyh/Desktop/gen2go.txt-rw-rw-r--. 1 cyh cyh 124 Jul 2910:53 /home/cyh/Desktop/log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 /home/cyh/Desktop/log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 /home/cyh/Desktop/log.txt[cyh@localhost Desktop]$ ls-l /home/cyh/Desktop/[g-k]*.* #显示g到k开头的所有格式的文件-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 /home/cyh/Desktop/gen2go.txt[cyh@localhost Desktop]$ ls-l /home/cyh/Desktop/[!g-k]*.* ##显示除g到k开头外的所有格式的文件-rw-rw-r--. 1 cyh cyh 0 Jul 1112:07 /home/cyh/Desktop/first_file.txt-rw-rw-r--. 1 cyh cyh 124 Jul 2910:53 /home/cyh/Desktop/log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 /home/cyh/Desktop/log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 /home/cyh/Desktop/log.txt[cyh@localhost Desktop]$
特殊符号

特殊字符有:分号(**;** )、**&** 、转义字符( \ ** )、输入重定向(< 、<<** )、输出重定向(**>、>>** )、管道符 (**|** )等

  • 分号(**;** )当需要连续运行几条命令并且不想写脚本时,这是命令之间就可以使用分号( ; )隔开,且命令是连续运行的,输出结果是连续运行后的输出结果。举例如下:[cyh@localhost Desktop]$ ll #先查看以下桌面的文件total 218464-rw-rw-r--. 1 cyh cyh 0 Jul 1112:07 first_file.txt-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 gen2go.txt-rw-rw-r--. 1 cyh cyh 194901303 Jun 13 03:58 gene2go-rw-rw-r--. 1 cyh cyh 124 Jul 2910:53 log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 log.txtdrwxrwxr-x. 3 cyh cyh 45 Jul 2910:09 shiyan1drwxrwxr-x. 2 cyh cyh 246 Jul 13 01:11 shiyan5[cyh@localhost Desktop]$ cd ~ #返回到根目录[cyh@localhost home]$ cd /home;cd cyh;cd Desktop;cd shiyan1 #使用分号连续执行命令[cyh@localhost shiyan1]$ #返回结果,目录更该到了shiyan1
正则表达式

正则表达式是一种由特定语法规则定义的模式匹配工具。 在Linux系统中,正则表达式常用于文本处理、日志分析、配置文件修改等任务。 Linux系统中最常见的正则表达式工具是grep、sed和awk。

以下是部分符号含义:
元字符***作用前一个字符匹配0次或任意多次。.匹配除了换行符外的任意一个字符。+匹配前面的子表达式一次或多次。?匹配前面的子表达式0次或一次。^匹配行首。例如:^hello会匹配以hello开头的行。$匹配行尾。例如:hello&会匹配以hello结尾的行。[]匹配中括号中指定的任意一个字符,只匹配一个字符。例如:[aoeiu]匹配任意一个元音字母;[0-9]匹配任意一位数字;[a-z][0-9]匹配小写字母和一位数字组成的两位字符。[^]匹配除中括号中字符以外的任意一个字符。例如:[0-9]匹配任意一位非数字字符;[a-z]匹配任意一位非小写字母。\转义符。用于取消特殊符号的含义。{n}表示其前面的字符恰好出现n次。例如:[0-9]{4}匹配任意4位数字,[1][3-8][0-9]{9}匹配手机号码。{n,}表示其前面的字符出现不小于n次。例如:[0-9]{2,}表示两位及其以上的数字。{n,m}表示其前面的字符至少出现n次,最多出现m次。例如:[a-z]{6,8}匹配6到8位的小写字母。[[:XXX:]]grep工具预定义的一些类,如[[:alpha:]]匹配一个字母。
懒得举例了:看这位博主得吧:Linux操作:正则表达式 - 知乎 (zhihu.com)

重定向
  • 输入重定向(**< 、<<** )、输出重定向(**>、>>** )一、输出重定向(**>、>>** )定向添加某些内容到某地(某文件),可以与echo 命令搭配使用**>** 定向添加某些内容到某地(某文件),方式是覆盖输入,就是源文件内容会被覆盖>> 定向添加某些内容到某地(某文件),方式是在文件末尾加入例如:添加内容:I like Linux system 到桌面上 shiyan1 文件夹中 sample.txt 文件中1、末尾加入,使用 >> ** , 请谨慎使用,执行前务必检查[cyh@localhost shiyan1]$ pwd#查看当前目录/home/cyh/Desktop/shiyan1[cyh@localhost shiyan1]$ ll #检查是否含有sample.txt文件total 4-rw-rw-r--. 1 cyh cyh 48 Jul 2910:09 sample.txtdrwxrwxr-x. 3 cyh cyh 21 Jul 29 09:59 shiyan_feikng[cyh@localhost shiyan1]$ cat sample.txt #先查看以下sample.txt里面的内容The linux system is greatI like it2023.07.29[cyh@localhost shiyan1]$ echo"I like Linux system">>./sample.txt #添加指定内容[cyh@localhost shiyan1]$ cat sample.txt #查看是否添加了指定内容The linux system is greatI like it2023.07.29I like Linux system[cyh@localhost shiyan1]$2、覆盖加入(全部覆盖掉),使用 >请谨慎使用,执行前务必检查**[cyh@localhost shiyan1]$ echo"I like Linux system">./sample.txt[cyh@localhost shiyan1]$ cat sample.txtI like Linux system3、多文件合并使用多文件合并可以使用cat 命令。[cyh@localhost shiyan1]$ ll #先查看文件内容(非必须)total 16-rw-rw-r--. 1 cyh cyh 80 Aug 17 05:14 cdx.sh-rw-rw-r--. 1 cyh cyh 94 Aug 1721:26 ne.txt-rw-rw-r--. 1 cyh cyh 20 Aug 17 04:52 sample.txtdrwxrwxr-x. 3 cyh cyh 21 Jul 29 09:59 shiyan_feikng-rw-rw-r--. 1 cyh cyh 14 Aug 17 05:14 z[cyh@localhost shiyan1]$ cat sample.txt > ne.txt #将sample.txt的内容定向覆盖输入到ne.txt文件中[cyh@localhost shiyan1]$ cat sample.txt #查看sample.txt文件内容I like Linux system[cyh@localhost shiyan1]$ cat ne.txt #查看覆盖后的ne.txt文件内容I like Linux system[cyh@localhost shiyan1]$ cat cdx.sh #查看cdx.sh内容#!/bin/bashz=3cat ne.txt > zwhileread zdoecho"the number is ${z} "done#这行是空的,无内容[cyh@localhost shiyan1]$ cat cdx.sh sample.txt >> ne.txt #将两个文件按先后顺序在ne.txt文件末尾加入,结果显示,输入的两个文件只要是cat能看的都可以[cyh@localhost shiyan1]$ cat ne.txt #查看结果I like Linux systemI like Linux system#!/bin/bashz=3cat ne.txt > zwhileread zdoecho"the number is ${z} "doneI like Linux system[cyh@localhost shiyan1]$二、输入重定向(**< 、<<** )。和输出相似。用法举例:[cyh@localhost shiyan1]$ cat< ne.txt #将文件内容重定向入cat查看。12672399[cyh@localhost shiyan1]$ cat<< stop >ne.txt #键盘输入内容,以stop为终止关键字,定向入cat,定向输入到ne.txt文件(覆盖输入)> The linux is great> I like it> stop #结束关键字,在这个之前的内容会被覆盖输入[cyh@localhost shiyan1]$ cat ne.txt #查看结果The linux is greatI like it[cyh@localhost shiyan1]$ cat<< end >> ne.txt #同上,在末尾加入> hallo > hi> end[cyh@localhost shiyan1]$ cat ne.txt #查看结果The linux is greatI like ithallohi[cyh@localhost shiyan1]$ cat<< lalala >>ne.txt #以lalala结尾> hhhhhhhhhhhhh> llllllllllll> lelelelalala lalala #这行也有个lalala字符串,但是并没有结束输入> lalala #从输入内容来看,关键字必须占新的一行,否者无法识别总结:输入输出方面,搞清楚输入输出方向。例如,在举个例子:[cyh@localhost shiyan1]$ echo"">ne.txt #将文件里的内容删除(第一行不为空,只是没有内容)[cyh@localhost shiyan1]$ cat ne.txt #查看文件[cyh@localhost shiyan1]$ ll #找一下文件,举个例子total 16-rw-rw-r--. 1 cyh cyh 80 Aug 17 05:14 cdx.sh-rw-rw-r--. 1 cyh cyh 1 Aug 1721:44 ne.txt-rw-rw-r--. 1 cyh cyh 20 Aug 17 04:52 sample.txtdrwxrwxr-x. 3 cyh cyh 21 Jul 29 09:59 shiyan_feikng-rw-rw-r--. 1 cyh cyh 14 Aug 17 05:14 z[cyh@localhost shiyan1]$ cat< cdx.sh >ne.txt #将cdx.sh内容重定向入cat命令,定向输出到ne.txt[cyh@localhost shiyan1]$ cat ne.txt #查看结果#!/bin/bashz=3cat ne.txt > zwhileread zdoecho"the number is ${z} "done[cyh@localhost shiyan1]$错误输出重定向在 **>** 前面加一个2,可以将命令执行过程产生的错误信息于结果区分开来,但都是输出在总端上,因此可以使用重定向输入到文件中保存。举例:[cyh@localhost shiyan1]$ ll #查看以下有哪些文件total 16-rw-rw-r--. 1 cyh cyh 80 Aug 17 05:14 cdx.sh-rw-rw-r--. 1 cyh cyh 80 Aug 1721:45 ne.txt-rw-rw-r--. 1 cyh cyh 20 Aug 17 04:52 sample.txtdrwxrwxr-x. 3 cyh cyh 21 Jul 29 09:59 shiyan_feikng-rw-rw-r--. 1 cyh cyh 80 Aug 1721:55 z[cyh@localhost shiyan1]$ cat cdx1.sh 2> error.txt #该命令会报错,因为没有这个文件,将信息输入到error.txt文件[cyh@localhost shiyan1]$ cat error.txt #查看文件内容,是报错提示信息cat: cdx1.sh: No such file or directory[cyh@localhost shiyan1]$ ll /home/cyh 2>> error.txt #附加重定向,运行后结果在屏幕输出total 4drwxr-xr-x. 4 cyh cyh 137 Jul 2910:53 Desktopdrwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Documentsdrwxr-xr-x. 3 cyh cyh 19 Mar 1511:03 Downloads-rw-rw-r--. 1 cyh cyh 359 Jun 1 01:05 guss.shdrwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Musicdrwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Picturesdrwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Publicdrwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Templatesdrwxrwxr-x. 2 cyh cyh 42 Apr 7 07:50 test1drwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Videos[cyh@localhost shiyan1]$ cat error.txt #结果显示,并没有输入在文件中,为什么???因为2表示的是将错误信息输入到文件中,上步命令并没有报错,所以并不会在文件中添加任何信息cat: cdx1.sh: No such file or directory那么,我们需要将屏幕输出结果于错误信息都输出到某文件,又该如何操作呢??这是后我们就需要改一下命令,如下:[cyh@localhost shiyan1]$ ll /home/cyh > error.txt 2>&1#将屏幕输出结果定向入error.txt文件。并且错误信息也可以输入[cyh@localhost shiyan1]$ cat error.txttotal 4drwxr-xr-x. 4 cyh cyh 137 Jul 2910:53 Desktopdrwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Documentsdrwxr-xr-x. 3 cyh cyh 19 Mar 1511:03 Downloads-rw-rw-r--. 1 cyh cyh 359 Jun 1 01:05 guss.shdrwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Musicdrwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Picturesdrwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Publicdrwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Templatesdrwxrwxr-x. 2 cyh cyh 42 Apr 7 07:50 test1drwxr-xr-x. 2 cyh cyh 6 Mar 1210:18 Videos[cyh@localhost shiyan1]$ ll /home/cyh1 > error.txt 2>&1#cyh1文件是没有的[cyh@localhost shiyan1]$ cat error.txt #将错误信息成功输入到文件ls: cannot access /home/cyh1: No such file or directory到这里我们据可以发现为什么没有附加重定向呢?上述代码是不是又两个 **>** 呢?那怎么操作呢??看到这里,我们应该了解了。一个 **>** 是覆盖,两个是末尾附加,那么应该该哪一个呢?我们举例试一下就知道了:[cyh@localhost shiyan1]$ ll /home/cyh1 > error.txt 2>&1#先生成一个结果[cyh@localhost shiyan1]$ cat error.txt #查看一些错误信息,预期结果,没有问题ls: cannot access /home/cyh1: No such file or directory[cyh@localhost shiyan1]$ ll /home/cyh1 > error.txt 2>>&1#我们修改后面为两个>号-bash: syntax error near unexpected token `&' #报错,没有&这个东西[cyh@localhost shiyan1]$ ll /home/cyh1 > error.txt 2>>1#我们删掉&[cyh@localhost shiyan1]$ cat error.txt #查看结果,结果没有,整个文件为空,所以不行[cyh@localhost shiyan1]$ ll /home/cyh1 > error.txt 2>&1#我们修改前面的,先把结果生成一个进去,更好展示[cyh@localhost shiyan1]$ ll /home/cyh1 >> error.txt 2>&1#修改前面的[cyh@localhost shiyan1]$ cat error.txt #展示结果,符合预期,所以我们秩序记住,在文件前面的才是重定向的ls: cannot access /home/cyh1: No such file or directoryls: cannot access /home/cyh1: No such file or directory
  • & 用于后台执行命令,当执行文件需要一定时间时,我们可以挂在后台执行,当前总端就可以执行其他任务。我一般会搭配nohup 使用举例如下:[cyh@localhost shiyan1]$ cat cdx.sh &#在结尾加一个& 后台运行,不好用[1]3655#进程号(ID)[cyh@localhost shiyan1]$ #!/bin/bashz=3cat ne.txt > zwhileread zdoecho"the number is ${z} "done^C #按Ctrl+c结束,不好用[1]+ Done cat cdx.sh[cyh@localhost shiyan1]$ nohupbash cdx.sh &#加一个nohup[1]3695[cyh@localhost shiyan1]$ nohup: ignoring input and appending output to ‘nohup.out’ #它说日志文件输出在‘nohup.out’中#这里按一个回车[1]+ Done nohupbash cdx.sh[cyh@localhost shiyan1]$ ll #我们查看一下是否有‘nohup.out’文件total 20-rw-rw-r--. 1 cyh cyh 80 Aug 17 05:14 cdx.sh-rw-rw-r--. 1 cyh cyh 80 Aug 1721:45 ne.txt-rw-------. 1 cyh cyh 137 Aug 1721:55 nohup.out #在这里-rw-rw-r--. 1 cyh cyh 20 Aug 17 04:52 sample.txtdrwxrwxr-x. 3 cyh cyh 21 Jul 29 09:59 shiyan_feikng-rw-rw-r--. 1 cyh cyh 80 Aug 1721:55 z[cyh@localhost shiyan1]$ cat nohup.out #查看以下内容#!/bin/bashz=3cat ne.txt > zwhileread zdoecho"the number is ${z} "donecdx.sh: line 4: read: read error: 0: Bad file descriptor[cyh@localhost shiyan1]$ #总结:不好用到这里,我一般会重定向与**&** 搭配使用,在这里举一个转录组序列比对的例子:脚本内容:#!/bin/bash#maker:YongHongChen#date:2023-04-24#This script used to match sequence by hisat2#the "GRCh38_index" is index file which contain "*.1.ht2" "*2.ht2" ... "*.8.ht2"#" -p 3 " is thread#" -1 " input one file;" -2 " input second file;their are can use .fa.gz/.fq format,their were all trimed#" -S " is a output option that it will generate .sam format file# Attention: the sam file is so big,if you input 1G .fq file,it will generate about 10G resulta=1b=2echo$(date"+%Y-%m-%d %H:%M:%S")#This command is uesd to generate the current timeforiin{1,2,3}doecho"ly${i}_${a} and ly${i}_${b} are begin" hisat2 -p3 /home/cyh/Desktop/hugene_dir/GRCh38_index -1 /home/cyh/rna_seq1/trimmomatic_ed1/ly${i}_${a}_paired.fq.gz -2 /home/cyh/rna_seq1/trimmomatic_ed1/ly${i}_${b}_paired.fq.gz -S /home/cyh/rna_seq1/hisat2_ed1/ly${i}_mached.sam echo"ly${i}_${a} and ly${i}_${b} are finish"echo"NT${i}_${a} and NT${i}_${b} are begin" hisat2 -p3 /home/cyh/Desktop/hugene_dir/GRCh38_index -1 /home/cyh/rna_seq1/trimmomatic_ed1/NT${i}_${a}_paired.fq.gz -2 /home/cyh/rna_seq1/trimmomatic_ed1/NT${i}_${b}_paired.fq.gz -S /home/cyh/rna_seq1/hisat2_ed1/NT${i}_mached.sam echo"NT${i}_${a} and NT${i}_${b} are finish"doneecho"All complete"echo$(date"+%Y-%m-%d %H:%M:%S")执行脚本:[cyh@localhost shiyan1]$ nuhup bash hisat2.sh >hisat2.txt 2>&1将所有信息全部输入hisat2.txt文件,包括总端输出信息以及错误信息。
  • 管道符管道符 | ,可以把一系列命令连接起来,把前一个命令的输出结果作为输入输给后面的一个命令,一个接一个。如果你接触过 R 语言中的 %>% 就非常号理解。用法举例:[cyh@localhost shiyan1]$ cd /home/cyh/Desktop #更改当前目录[cyh@localhost Desktop]$ ll #查看当前目录total 218464-rw-rw-r--. 1 cyh cyh 0 Jul 1112:07 first_file.txt-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 gen2go.txt-rw-rw-r--. 1 cyh cyh 194901303 Jun 13 03:58 gene2go-rw-rw-r--. 1 cyh cyh 124 Jul 2910:53 log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 log.txtdrwxrwxr-x. 3 cyh cyh 123 Aug 1722:54 shiyan1drwxrwxr-x. 2 cyh cyh 246 Jul 13 01:11 shiyan5[cyh@localhost Desktop]$ cat gen2go.txt |more#使用管道符,将文件的内容more一下96061 GO:0003674 ND enables molecular_function - Function96061 GO:0005576 HDA located_in extracellular region 27068509 Component96061 GO:0005576 IDA located_in extracellular region 3458201 Component96061 GO:0005576 TAS located_in extracellular region - Component96061 GO:0005615 HDA located_in extracellular space 16502470 Component96061 GO:0005886 IBA is_active_in plasma membrane 21873635 Component96061 GO:0008150 ND involved_in biological_process - Process96061 GO:0031093 TAS located_in platelet alpha granule lumen -Component96061 GO:0034774 TAS located_in secretory granule lumen - Component96061 GO:0062023 HDA located_in collagen-containing extracellularmatrix 27559042 Component--More脚本内容举例:(不要在乎是否看懂)脚本将序列比对软件(hisat2 )生成的结果 sam 文件给 samtools 排序并转换为 bam 文件。#!/bin/bash#maker:YongHongChen#date:2023-04-24a=1b=2echo$(date"+%Y-%m-%d %H:%M:%S")#This command is uesd to generate the current timeforiin{1,2,3}doecho"ly${i}_${a} and ly${i}_${b} are begin" hisat2 -p3 /home/cyh/Desktop/hugene_dir/GRCh38_index -1 /home/cyh/rna_seq1/trimmomatic_ed1/ly${i}_${a}_paired.fq.gz -2 /home/cyh/rna_seq1/trimmomatic_ed1/ly${i}_${b}_paired.fq.gz -S| samtools sort -@ 20-o /home/chyh/rna_seq1/samtools_result/ly${i}.bam /home/cyh/rna_seq1/hisat2_ed1/ly${i}_mached.sam echo"ly${i}_${a} and ly${i}_${b} are finish"doneecho"All complete"echo$(date"+%Y-%m-%d %H:%M:%S")

Linux编写脚本

在这之前,我举例过几个脚本的例子,UU们可能疑惑不知怎么写,那么接下来,我们看看脚本编写,这里我们只用 bash ,不用perl 举例。对 perl 感兴趣的去看其他大佬的文章。

在运行脚本前,我们得又一个脚本文件(这里以**.sh** 文件举例)。

1、创建脚本文件

  • 你可以先创建文件再编写
[cyh@localhost shiyan1]$ touch frist_script.sh  #先创建脚本[cyh@localhost shiyan1]$ ll   #查看一下是否创建成功
total 0
-rw-rw-r--. 1 cyh cyh  0 Aug 1723:16 frist_script.sh
drwxrwxr-x. 3 cyh cyh 21 Jul 29 09:59 shiyan_feikng
[cyh@localhost shiyan1]$ vim frist_script.sh  #编写脚本
  • 直接使用vim ,如果没有文件会自动创建(推荐)
vim frist_script.sh  #创建并编写脚本

2、编写脚本与文本编辑操作一样,直接参考前面得,这里不加以描述。

3、编写内容

  • 首行内容

如果你编写得是 .sh 的文件,那么就由 bash 执行,如果是 .pl 文件,那么就由 perl 执行。第一行必须是编译方式的描述,并且格式固定

#!/bin/sh

#!/bin/bash

如果是 perl

#!/bin/perl  或 #!/usr/bin/perl  #perl解释器路径

其他内容写你需要的命令。

脚本举例:(for循环后面讲)

#!/bin/bash  #如何是bash的第一行必须为这个,或者可以写成 #!/bin/sh#maker:YongHongChen   #创建者名 (非必须,有最好)#date:2023-04-24       #创建时间(非必须,有最好)a=1b=2echo$(date"+%Y-%m-%d %H:%M:%S")#This command is uesd to generate the current timeforiin{1,2,3}doecho"ly${i}_${a} and ly${i}_${b} are begin"
  hisat2 -p3 /home/cyh/Desktop/hugene_dir/GRCh38_index -1 /home/cyh/rna_seq1/trimmomatic_ed1/ly${i}_${a}_paired.fq.gz -2 /home/cyh/rna_seq1/trimmomatic_ed1/ly${i}_${b}_paired.fq.gz -S| samtools sort -@ 20-o /home/chyh/rna_seq1/samtools_result/ly${i}.bam /home/cyh/rna_seq1/hisat2_ed1/ly${i}_mached.sam
  echo"ly${i}_${a} and ly${i}_${b} are finish"doneecho"All complete"echo$(date"+%Y-%m-%d %H:%M:%S")
  • 非首行内容非首行内容写你需要的命令。(注释除外,注释使用**#** 号)上述脚本中含有变量 ab ,后面再讲,如果学过 C或python ,那就很好理解了

4、执行脚本

bash frist_script.sh #bash 脚本文件
perl frist_script.pl #perl脚本

如果弹出没有权限执行,则给予权限

chmod u+x frist_script.sh  #设置可执行权限

注意:执行脚本本身就可以当作执行一条命令,所以脚本里面套一个脚本是可以的。

脚本进阶

编写脚本内容之前,我们需要了解一些基础知识。

一、变量

变量有环境变量与内部变量,环境变量可以理解为整个系统下的默认的变量,在整个系统下通用的变量,是在命令行定义的变更,举个例子:我在终端写 a=3 那么不在重启前,不改变数值前 a=3 。类似于C语言函数外的变量。

变量的定义注意事项(大多编程语言都是这样)

  • 变量名称可以由字母、数字和下划线组成,但不能以数字开头。
  • 在 Bash 中,变量的默认类型都是字符串型,如果要进行数值运算,则必须指定变量类型为数值型。
  • 变量用等号连接值,等号左右两侧不能有空格。
  • 变量的值如果有空格,需要使用单引号或双引号包括。
  • 在变量的值中,可以使用“\”转义符。
  • 如果需要增加变量的值,那么可以进行变量值的叠加。不过,变量需要用双引号包含“ 变 量 名 ” 或 用 变量名”或用 变量名”或用{变量名}包含。
  • 如果是把命令的结果作为变量值赋予变量,则需要使用反引号或$()包含命令。
  • 环境变量名建议大写,便于区分。

参数变量****作用$nN 为数字,$0 代表命令本身,$1-

        9 
       
      
        代 
       
      
        表 
       
      
        第 
       
      
        一 
       
      
        到 
       
      
        第 
       
      
        九 
       
      
        个 
       
      
        参 
       
      
        数 
       
      
        , 
       
      
        十 
       
      
        以 
       
      
        上 
       
      
        的 
       
      
        参 
       
      
        数 
       
      
        需 
       
      
        要 
       
      
        用 
       
      
        大 
       
      
        括 
       
      
        号 
       
      
        包 
       
      
        含 
       
      
        , 
       
      
        如 
       
      
     
       9 代表第一到第九个参数,十以上的参数需要用大括号包含,如 
      
     
   9代表第一到第九个参数,十以上的参数需要用大括号包含,如{10}。$*这个变量代表命令行中所有的参数,$* 把所有的参数看成一个整体。$@这个变量也代表命令行中所有的参数,不过 $@ 把每个参数区分对待。$#这个变量代表命令行中所有参数的个数。

当我们定义了变量后,就需要取它的值,我们使用**

     ∗ 
    
   
     ∗ 
    
   
     取 
    
   
     值 
    
   
     , 
    
   
     例 
    
   
     如 
    
   
     : 
    
   
     ∗ 
    
   
     ∗ 
    
   
  
    ** 取值,例如:** 
   
  
∗∗取值,例如:∗∗a** 取变量a的值

**

      ∗ 
     
    
      ∗ 
     
    
      用 
     
    
      法 
     
    
      有 
     
    
      ∗ 
     
    
      ∗ 
     
    
   
     ** 用法有** 
    
   
 ∗∗用法有∗∗ 、"$"、$0、 
  
   
    
    
      1 
     
    
      、 
     
    
   
     1、 
    
   
 1、n、KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲、@、$ *、 
   
    
     
     
       ? 
      
     
       、 
      
     
    
      ?、 
     
    
  ?、()、、 
   
    
     
      
     
       、 
      
     
    
      {}、 
     
    
  、{#}、 
   
    
     
     
       [ 
      
     
       ] 
      
     
       、 
      
     
    
      []、 
     
    
  []、-、 
   
    
     
     
       ! 
      
     
       、 
      
     
    
      !、 
     
    
  !、$** 等

上面常用的有 **

      , 
     
    
   
     , 
    
   
 ,{},$[]** ,用于取值。

**

      [ 
     
    
      ] 
     
    
      ∗ 
     
    
      ∗ 
     
    
      中 
     
    
      括 
     
    
      号 
     
    
      里 
     
    
      写 
     
    
      的 
     
    
      是 
     
    
      表 
     
    
      达 
     
    
      式 
     
    
      , 
     
    
      ∗ 
     
    
      ∗ 
     
    
   
     []** 中括号里写的是表达式,** 
    
   
 []∗∗中括号里写的是表达式,∗∗()** 里写的是命令,**$?** 判断上一个命令是否运行成功。例如:
[cyh@localhost Desktop]$ echo $[5+6]11[cyh@localhost Desktop]$ echo$(seq210)2345678910[cyh@localhost Desktop]$ echo$?#判断上一条命令是否执行成功,返回0表示成功0[cyh@localhost Desktop]$ lll
bash: lll: command not found...
[cyh@localhost Desktop]$ echo$?#返回非0表示失败,返回值0-255,超出部分%256取余127[cyh@localhost Desktop]$

内部变量理解为局部变量,脚本中的变量,脚本中的变量不会影响终端中的变量,在C语言中全局变量不会受到局部变量影响。

[cyh@localhost shiyan1]$ a=0#全局变量,我定义了一个变量a,它的值为0[cyh@localhost shiyan1]$ echo$a#我们可以使用echo输出值0[cyh@localhost shiyan1]$ cat frist_script.sh
#!/bin/basha=1#脚本中的局部变量a1=2#定义变量a1echo"a=$a"echo"a1=$a1"#输出变量a1的值,注意需要使用$符号取值echo"a3=$a3"#这个a3是没有定义的,所以预计会输出空值[cyh@localhost shiyan1]$ bash frist_script.sh  #执行脚本a=1a1=2a3=[cyh@localhost shiyan1]$ echo$a#执行脚本后全局变量值未改变0[cyh@localhost shiyan1]$ echo$a1#找不到脚本局部变量值,没有被定义的变量为空[cyh@localhost shiyan1]$

变量只要是赋予它值就一定会改变变量值吗?

不一定,只读变量就不可以随意改变值,只读变量只能查看,不能更改,那么要改变值,咋办??只能重启。

[cyh@localhost Desktop]$ readonlya="tai ku la"#设置只读变量[cyh@localhost Desktop]$ echo$a#输出a值
tai ku la
[cyh@localhost Desktop]$ a="na zheng shi tai ku la"#更改a值,发现报错
-bash: a: readonly variable
[cyh@localhost Desktop]$
二、字符串

字符串就是一串字符,一个英文句子就是一个字符串,要输出字符串,我们需要用引号包起来。

[cyh@localhost shiyan1]$ echo"I like Linux"
I like Linux
  • 字符串截取 expr substr从某位置截取指定长度的字符[cyh@localhost shiyan1]$ expr substr "I like linux"27#从第二个位置,截取长度为7的子字符,注意空格也占一个位置 like l
  • 字符串定位 expr index在字符串中查找某元素,返回位置。[cyh@localhost shiyan1]$ expr index "I like linux" ik #在字符串中查找ik,返回的位置4[cyh@localhost shiyan1]$ expr index "I like linux" o #若没有找到,返回00
  • 字符串匹配 expr macth两个字符串,后一个在前一个匹配。默认从起始位置匹配,没有匹配到就返回0值,匹配到就返回能匹配的字符个数[cyh@localhost shiyan1]$ expr match "I like linux"" li"0[cyh@localhost shiyan1]$ expr match "I like linux""I li"4[cyh@localhost shiyan1]$ expr match "I like linux""I lig"#元素得一样,否者不能匹配0
  • 计算字符长度 expr length返回长度数值,一个字符算一个,空格也算[cyh@localhost shiyan1]$ expr length "I like linux"12
三、函数

$变量名 表示取值。

分支选择结构
  • if 条件语句

说人话就是C语言中得 if 条件语句等

shell 脚本定义格式如下:

写法一:

if[ 条件表达式 ];then
    命令1
elif[ 条件表达式 ];then
    命令2
else
    命令3
fi#fi 是if反过来写

写法二:

if[ 条件表达式 ]then
    命令1
elif[ 条件表达式 ]then
    命令2
else
    命令3
fi

这是多条件分支。也有单个的。如果学过C语言,那就不难了。

if[ 条件表达式 ]then
    命令1
fi

注意:if 后面有个空格,中括号里与条件表达式之间前后有空格,不然会报错。

到这里,UU们蠢蠢欲动了,但是有个问题?条件表达式些啥呢?

接下来我们看看条件表达式,很明显,就是一个式子。

  • 字符串比较这一部分,是用来比较字符与字符串的=比较两个字符是否相等!=比较两个字符是否不相等< 或 >比较两个字符串长度-n判断字符串长度是否大于0-z判断字符串长度是否等于0……用法举例:[cyh@localhost shiyan1]$ vim frist_script.sh #编写脚本[cyh@localhost shiyan1]$ cat frist_script.sh #看一下脚本内容#!/bin/basha="I like linux"b="I like linua"c="like"if[ a!=b ]thenecho"a=b"elif[ a!=c ]thenecho"a!=c"fiif[ a>b ]thenecho"a>b"fiif[-n a ]thenecho"a length > 0"fi[cyh@localhost shiyan1]$ bash frist_script.sh #执行脚本a=ba>ba length >0[cyh@localhost shiyan1]$
  • 数值比较这一部分是数值的比较-eq比较两个数是否相等-ge比较一个数是否大于或等于另一个数-le比较一个数是否小于或等于另一个数-gt比较一个数是否大于另一个数-lt比较一个数是否小于另一个数-ne比较两个数是否不相等……用法举例:[cyh@localhost shiyan1]$ vim frist_script_number.sh #编写脚本[cyh@localhost shiyan1]$ cat frist_script_number.sh #查看脚本内容#!/bin/basha=1b=2c=1d=3if[$a-eq$b]#判断等于 thenecho"a=b"elseecho"a!=b"fiif[$d-gt$b]#判断大于thenecho"d>b"elseecho"d<b"fiif[$d-lt$c]#判断小于thenecho"d<c"elseecho"d>c"fiif[$a-ne$c]#判断不相等thenecho"a!=c"elseecho"a=c"fi[cyh@localhost shiyan1]$ bash frist_script_number.sh #执行脚本a!=bd>bd>ca=c[cyh@localhost shiyan1]$
  • 文件属性判断该部分用于判断文件属性-d确定文件是否为目录-f确定文件是否为普通文件-e确定文件是否存在-r确定是否对文件设置读许可-w确定是否对文件设置写许可-x确定是否对文件设置执行许可-s确定文件名是否具有大于零的长度……用法举例:[cyh@localhost shiyan1]$ ll #查看当前目录文件内容total 8-rw-rw-r--. 1 cyh cyh 0 Aug 18 04:51 b-rw-rw-r--. 1 cyh cyh 291 Aug 18 05:03 frist_script_number.sh-rw-rw-r--. 1 cyh cyh 193 Aug 18 04:51 frist_script.shdrwxrwxr-x. 3 cyh cyh 21 Jul 29 09:59 shiyan_feikng[cyh@localhost shiyan1]$ vim diff_file.sh #创建编写脚本[cyh@localhost shiyan1]$ cat diff_file.sh #查看脚本内容#!bin/basha="/home/cyh/Desktop/shiyan1/diff_file.sh"b="/home/cyh/Desktop/shiyan1/file.txt"if[-d$a]#$a表示取a的值thenecho"$a is not a directory"elseecho"$a is a file"fiif[-e$b]thenecho"file.txt had exist"elseecho"file.txt not find"fiif[-r$a]thenecho"the diff_file.sh is readable"elseecho"the diff_file.sh is not readable"fi[cyh@localhost shiyan1]$ bash diff_file.sh #执行脚本,并返回结果/home/cyh/Desktop/shiyan1/diff_file.sh is a filefile.txt not findthe diff_file.sh is readable
  • 逻辑操作符逻辑操作符常见的主要有 && 、**||** 、**!** ,就是或与非啦!这部分内容与C语言 中的类似。在这之前,我们接触了单个**&** ,与单个**|** ,在这部分我们可以联系起来。& 表示任务在后台执行&& 表示前一条命令执行成功时,才执行后一条命令 。[cyh@localhost Desktop]$ ll #查询目录内容total 218464-rw-rw-r--. 1 cyh cyh 0 Jul 1112:07 first_file.txt-rw-rw-r--. 1 cyh cyh 28788565 Jun 13 04:00 gen2go.txt-rw-rw-r--. 1 cyh cyh 194901303 Jun 13 03:58 gene2go-rw-rw-r--. 1 cyh cyh 124 Jul 2910:53 log2.sh-rw-rw-r--. 1 cyh cyh 985 Jul 6 09:38 log2.txt-rw-r--r--. 1 root root 236 Jul 523:12 log.txtdrwxrwxr-x. 3 cyh cyh 109 Aug 19 05:12 shiyan1drwxrwxr-x. 2 cyh cyh 246 Jul 13 01:11 shiyan5[cyh@localhost Desktop]$ cat log2.sh &&echo"good"#查看已有的文件后输出good#!/bin/bashecho$(date"+%Y-%m-%d %H:%M:%S")foriin{1,2,3}doecho"I like R "doneecho$(date"+%Y-%m-%d %H:%M:%S")good[cyh@localhost Desktop]$ cat log22.sh &&echo"good"#文件不存在,即无法执行echocat: log22.sh: No such file or directory[cyh@localhost Desktop]$| 表示管道,上一条命令的输出,作为下一条命令参数(输入)[cyh@localhost ~]$ echo"I like linux"|wc-l1|| 表示上一条命令执行失败后,才执行下一条命令[cyh@localhost Desktop]$ cat log22.sh ||echo"good"#执行失败后执行输出goodcat: log22.sh: No such file or directorygood
  • case 语句该语句类似于C 语言中的switch 语句,case 只能判断一种条件。格式如下:case $变量名 in “值1”) 如果变量的值等于值1,则执行此程序1 ;;#注意两个分号结尾一个语句 … … … … *) 如果变量的值都不是以上的值,则执行此程序 ;;esac#倒过来写结束程序举例如下:[cyh@localhost Desktop]$ vim case.sh #编写脚本[cyh@localhost Desktop]$ cat case.sh #查看脚本#!/bin/bashread-p"please input number:"-t30 number #使用read 从键盘输入一个数给变量numbercase$numberin"20")echo"you input number is 20";;"30")echo"you input number is 30";; *)echo"you input number is other";;esac[cyh@localhost Desktop]$ bash case.sh #执行脚本please input number:30you input number is 30[cyh@localhost Desktop]$ bash case.sh #输入数据50please input number:50you input number is other分支语句还有许多,但大多不常用。
循环语句
  • for循环与其他编程语言类似。写法格式一:for 变量 in 值1 值2 值3…do 语句done写法格式二:#for ((变量赋值;测试条件表达式;迭代过程))for((i=1;i<=10;i++))#该写法与C类似,但是注意有两个括号,括号里的数值只是举例,i为变量名do 语句done``````for((i=1;i<=10;i++));do 语句;done#如果这样写注意分号写法三:for 变量名 in{值1,值2,值3…}#使用大括号将取值括起来,并用逗号隔开do 语句done举例:(脚本)#!/bin/bash#maker:YongHongChen#date:2023-07-08#This script used to match sequence by hisat2#the "GRCh38_index" is index file which contain "*.1.ht2" "*2.ht2" ... "*.8.ht2"#" -p 3 " is thread#" -1 " input one file;" -2 " input second file;their are can use .fa.gz/.fq format,their were all trimed#" -S " is a output option that it will generate .sam format file# Attention: the sam file is so big,if you input 1G .fq file,it will generate about 10G resulta=1b=2echo$(date"+%Y-%m-%d %H:%M:%S")#打印当前时间foriin{1,2,3}#取值我使用括号括起,表示从括号里面取值doecho"ly${i}_${a} and ly${i}_${b} are begin" hisat2 -p20--dta-x /home/chenyh/ly_NT_RNAseq/reference_GRCh38/GRCh38_index -1 /home/chenyh/ly_NT_RNAseq/fastp_result/ly${i}_${a}_trimmed.fq.gz -2 /home/chenyh/ly_NT_RNAseq/fastp_result/ly${i}_${b}_trimmed.fq.gz -S /home/chenyh/ly_NT_RNAseq/hisat2_result/ly${i}_mached.sam echo"ly${i}_${a} and ly${i}_${b} are finish"doneecho"All complete"echo$(date"+%Y-%m-%d %H:%M:%S")``````[cyh@localhost Desktop]$ vim for.sh[cyh@localhost Desktop]$ cat for.sh#!/bin/bashsum=0for((i=0;i<=50;i++))dosum=$(( $sum+$i ))doneecho"the sum of 1+2+3+...+50=$sum"[cyh@localhost Desktop]$ bash for.shthe sum of 1+2+3+...+50=1275
  • while 循环用法格式:while[ 条件判断式 ]#注意while后面有个空格,条件判断式两端有空格,否则报错do 语句done

用法举例:

#!/bin/bashi=1sum=0#如果变量i的值小于等于50,则执行循环while[$i-le100]dosum=$(( $sum+$i ))i=$(( $i+2))doneecho"The sum of 1+2+3+…+50 is: $sum"
[cyh@localhost Desktop]$ vim while.sh  #编写脚本[cyh@localhost Desktop]$ cat while.sh    #查看内容#!/bin/bashi=1sum=0#如果变量i的值小于等于50,则执行循环while[$i-le100]dosum=$(( $sum+$i ))i=$(( $i+2))doneecho"The sum of 1+2+3+…+50 is: $sum"[cyh@localhost Desktop]$ bash while.sh  #执行脚本
  The sum of 1+2+3+…+50 is: 2500[cyh@localhost Desktop]$

循环语句还有多个,比如until 等。这里不一一举例。

循环控制命令

在循环过程中,会有不同的需求,例如,符合条件需要终止循环,以及退出本轮循环。

  • break :终止循环。
  • continue :退出本轮循环,继续下轮循环。举例如下:[cyh@localhost Desktop]$ vim while.sh #编写脚本[cyh@localhost Desktop]$ cat while.sh #查看脚本#!/bin/bashi=1sum=0#如果变量i的值小于等于50,则执行循环while[$i-le100]doif[$i-lt45]thensum=$(( $sum+$i ))i=$(( $i+2))continue;#退出本轮循环,后续语句不执行echo"$i < 45"fiecho"$i > 45"break;#结束循环doneecho"The sum of 1+2+3+…+45 is: $sum"[cyh@localhost Desktop]$ bash while.sh #执行脚本45>45The sum of 1+2+3+…+45 is: 484[cyh@localhost Desktop]$

用户管理

Linux 有多个用户,主要类型如下:

  • 超级用户(root):它具有一切权限, 只有进行系统维护(例如: 建立用户等)或其他必要情形下才用超级用户登录, 以避免系统出现安全问题。 在root 用户下,在等待输入的命令行前面,最后一行是**#** ,普通用户是**$**
  • 系统用户:是Linux系统正常工作所必需的内建的用户, 主要是为了满足相应的系统进程对文件属主的要求而建立的, 系统用户不能用来登录, 例如: bin、daemon、 adm、 lp等用户。注意该用户没有home目录
  • 普通用户:是为了让使用者能够使用Linux系统资源而建立的, 我们的大多数用户属于此类。

在这部分有许多关于用户管理的相关命令,如下:

  • useradd 命令功能:增加用户账号,但是要使用root 用户创建。用法格式:useradd [选项] 用户名主要参数:**-e** (expire):使用“YYYY-MM-DD”的格式指定用户账号过期日期**-d** (directory):该选项后面指定路劲参数,用于指定用户主目录路径**-G** :该选项后面需给出组群ID或组群名称参数,用于指定用户所属的附加组群**-g** :该选项后面需给出组群ID或组群名称参数,用于指定用户所属的初始组群**-u** :该参数用于指定用户UID,需在后面给出数字参数**-r** :创建的用户为系统用户举例:
useradd-u1000 cyh  #添加普通用户,UID为100
useradd-r cyh   #添加系统用户
  • passwd 命令功能:设置用户密码。(需要root账户)格式:passwd[选项][用户]部分选项:**-d** (delete):删除用户账户密码,用户登入不需要密码**-l** (lock):锁定用户账户**-u** (unlock):解锁用户账号**-S** (status):查看用户密码状态用法举例:[root@localhost cyh]# passwd -S cyh #查看用户的密码状态cyh PS 1969-12-30 0999997-1(Password set, SHA512 crypt.)[root@localhost cyh]# passwd cyh #更改用户(cyh)密码Changing password for user cyh.New password: #输入密码的过程密码密码不会显示,不是你的键盘失灵Retype new password:passwd: all authentication tokens updated successfully.
  • id 命令功能:查看用户账号UID,GID格式:id 用户名用法举例:[root@localhost cyh]# id cyhuid=1000(cyh)gid=1000(cyh)groups=1000(cyh),10(wheel)
  • userdel 命令功能:删除用户账户格式:userdel[选项] 用户名参数:**-r** :删除用户的主目录和邮件文件内容

Linux安装软件

一、使用yum 安装软件。

我安装软件通常使用两种方式,然而Linux安装软件有多种方式,目前centos 可以使用yum 进行软件的安装与升级。但是,并不是所有软件yum 都含有,可以把yum 理解为手机应用商店。

yum( Yellow dog Updater, Modified)是一个在 Fedora 和 RedHat 以及 SUSE 中的 Shell 前端软件包管理器。基于 RPM 包管理,能够从指定的服务器自动下载 RPM 包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。

yum 是基于rpm 的软件在线管理器,部分参数如下:

  • install [软件名列表] :安装列表中的软件。
  • update [软件名列表]:更新列表中的软件包。(不另加任何参数情况下)
  • remove [软件名列表]:移除列表中的软件包。(删除/卸载软件)
  • list [软件名列表]:列出软件包的各种信息。
  • clean [软件名列表]:清除yum 缓存。另加参数all 删除所有缓存。…

yum常用命令

  • 列出所有可更新的软件清单命令:yum check-update
  • 更新所有软件命令:yum update
  • 仅安装指定的软件命令: yum install <package_name>
  • 仅更新指定的软件命令:yum update <package_name>
  • 列出所有可安裝的软件清单命令:yum list
  • 删除软件包命令:yum remove <package_name>
  • 查找软件包命令:**yum search **
  • 清除缓存命令:- yum clean packages: 清除缓存目录下的软件包- yum clean headers: 清除缓存目录下的 headers- yum clean oldheaders: 清除缓存目录下旧的 headers- yum clean, yum clean all (= yum clean packages; yum clean oldheaders) :清除缓存目录下的软件包及旧的 headers

我使用yum 安装软件的步骤。

我们需要的软件包不清楚yum 源中是否有,我可以查询一下,以samtools 软件为例:

下面例子中出现了该包的一些信息,但是没有显示可以安装,可能yum 源中没有。

[cyh@localhost Desktop]$ yum search samtools
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
 * base: ftp.sjtu.edu.cn
 * centos-sclo-rh: ftp.sjtu.edu.cn
 * centos-sclo-sclo: ftp.sjtu.edu.cn
 * elrepo: dfw.mirror.rackspace.com
 * epel: ftp.cse.buffalo.edu
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
ius                                                                               209/209
================================= N/S matched: samtools ==================================
samtools.x86_64 : Tools for nucleotide sequence alignments in the SAM format

  Name and summary matches only, use "search all"for everything.
[cyh@localhost Desktop]$

对于yum 源中有的软件包,只需要使用install 安装。(需要使用管理员账户)

[root@localhost Desktop]# yum install gcc  #
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Could not get metalink https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=x86_64 error was
14: curl#6 - "Could not resolve host: mirrors.fedoraproject.org; Unknown error"
 * base: mirrors.bfsu.edu.cn
 * centos-sclo-rh: mirrors.bfsu.edu.cn
 * centos-sclo-sclo: mirrors.bfsu.edu.cn
 * elrepo: mirror-hk.koddos.net
 * epel: dfw.mirror.rackspace.com
 * extras: mirrors.bfsu.edu.cn
 * updates: mirrors.bfsu.edu.cn
http://mirrors.qlu.edu.cn/centos/7.9.2009/os/x86_64/repodata/repomd.xml: [Errno 14]curl#6 - "Could not resolve host: mirrors.qlu.edu.cn; Unknown error"...
http://mirrors.bfsu.edu.cn/centos/7.9.2009/updates/x86_64/Packages/kernel-headers-3.10.0-1160.95.1.el7.x86_64.rpm: [Errno 14]curl#6 - "Could not resolve host: mirrors.bfsu.edu.cn; Unknown error"
Trying other mirror.

Error downloading packages:
  glibc-headers-2.17-326.el7_9.x86_64: [Errno 256] No more mirrors to try.
  kernel-headers-3.10.0-1160.95.1.el7.x86_64: [Errno 256] No more mirrors to try.
  glibc-devel-2.17-326.el7_9.x86_64: [Errno 256] No more mirrors to try.
  gcc-4.8.5-44.el7.x86_64: [Errno 256] No more mirrors to try.
  cpp-4.8.5-44.el7.x86_64: [Errno 256] No more mirrors to try.
  • 对于使用yum ,可以另外装一些源,源可以理解为不同得应用商店。这部分自行百度(没有空写了)。我安装得源有RemiEPELphp 等。
  • 对于yum 源中没有的软件包,我们可以直接浏览器找到官网复制下载链接。 linux 下使用wget 命令下载(或其他)。后续直接解压(部分需要编译)一:对于不需要编译的软件,以Hisat2 软件为例:1、使用 wget 下载 Hista2 安装包(该地址可能会变,仅供参考)[root@localhost Desktop]# wget ftp://ftp.ccb.jhu.edu/pub/infphilo/hisat2/downloads/hisat2-2.1.0-Linux_x86_64.zip2、解压(压缩包为.zip 文件,使用unzip 解压)[root@localhost Desktop]# unzip unzip /home/cyh/biosoft/hisat2-2.1.0-Linux_x86_64.zip3、配置环境,将信息写入 ~/.bashrc 或者 /etc/profile[root@localhost Desktop]# vim ~/.bashrc #编辑该该文件export PATH="/你的全路径/hisat2-2.1.0: $PATH"#在~/.bashrc文件末尾输入这行内容,路径是你自己的[root@localhost Desktop]# source ~/.bashrc #重启文件,立即生效二、对于需要编译的软件(以samtools 为例)#安装依赖,该软件需要依赖[root@localhost Desktop]# yum install bzip2-devel ncurses-libs ncurses-devel xz-devel zlib-devel [root@localhost Desktop]# wget https://github.com/samtools/samtools/releases/download/1.17/samtools-1.17.tar.bz2 #download packages[root@localhost Desktop]# tar -jxvf samtools-1.17.tar.bz2 #解压解压后,你会得到一个文件夹,文件名一般是软件名称带版本信息,例如:samtools-1.17进入该文件,你会看到文件里面含有configure 的文件夹(与之同级的还有许多源代码文件),含有这个文件的一般都是需要编译的。在于configure 同级目录里输入以下内容:(注意路径是你的路径,不是我的)[root@localhost Desktop]# ./configure --prefix=/home/lh/biosoft/samtools-1.17 #进入samtools-1.17文件,注意路径是你的目录,需要全路径然后依次执行下面语句,不会报错就不要管他,千万不要中断,让他跑,可能会要一点时间,取决你的电脑编译速度于软件大小。(注意:make命令你电脑得先有或者这个命令安装过了)[root@localhost Desktop]# make #编译[root@localhost Desktop]# make install #编译后安装最后按上述方法配置好环境。我的建议是,不能死磕yum ,有可能你需要的包没有或很难安装或者有各种各样的依赖。:在安装软件部分,最稳妥,最方便,最不会掉进坑的技巧是直接浏览器搜索某软件如何安装,例如:linux下samtools软件如何安装。有大量教程避免走弯路。

本文结束,如果您能看到这里,那么对于Linux,应该是比较熟悉了。但是,也许你会发现一个问题,本篇博客并不是Linux系统得全部内容,以上便是我总结得,常用得一些命令与知识点,里面有些知识解释得并不全面,如果,你感兴趣,可以自行探索。然后对于以上内容有问题,可以留言。


本文转载自: https://blog.csdn.net/qq_74093550/article/details/133470216
版权归原作者 CYH-BI 所有, 如有侵权,请联系我们删除。

“Linux 基础入门(学习笔记通俗易懂版)”的评论:

还没有评论