linux的shell常用命令续

来源:互联网 发布:淘宝网中老年大码棉袄 编辑:程序博客网 时间:2024/04/28 05:56

shell在这里不是希尔排序,而是一种提供使用界面的软件,也叫命令解释器,类似于DOS下的command.com,用于接受用户明年,然后调用相关应用程序。

shell是一种程序设计语言,作为命令语言,可以交互式的解释和执行用户输入的命令或者自动解释执行预先定义好的一连串命令

shell同样拥有流程控制语句,包括循环和分支,定义各种变量和参数


注意区分大小写,中间的空格

注意:开头的

# 是ROOT用户的提示符

$,是普通用户的提示符


命令执行结果告诉我正在使用bash,而bash是shell的一种,linux默认使用,常用的shell有四类:

1、Bourne Shell(简称sh)

(1)Bourne Shell是AT&T Bell实验室的 Steven Bourne为AT&T的Unix开发的,它是Unix的默认Shell,也是其它Shell的开发基础。Bourne Shell在编程方面相当优秀,但在处理与用户的交互方面不如其它几种Shell。

2、C-Shelll(简称csh)

(2)C Shell是加州伯克利大学的Bill Joy为BSD Unix开发的,与sh不同,它的语法与C语言很相似。它提供了Bourne Shell所不能处理的用户交互特征,如命令补全、命令别名、历史命令替换等。但是,C Shell与BourneShell并不兼容

3、Korn Shell(简称ksh)

(3)Korn Shell是AT&T Bell实验室的David Korn开发的,它集合了C Shell和Bourne Shell的优点,并且与Bourne Shell向下完全兼容。Korn Shell的效率很高,其命令交互界面和编程交互界面都很好。

4、Bourne Again Shell (简称bash)

(4)Bourne Again Shell (即bash)是自由软件基金会(GNU)开发的一个Shell,它是Linux系统中一个默认的ShellBash不但与Bourne Shell兼容,还继承了C Shell、Korn Shell等优点。

选择其他的shell,exec 命令,execute的缩写(执行的意思)


查看得知;我们在使用sh

注意exit命令,是退出shell,使用它就是全部退出了!终端界面会关闭!!

我们一般使用exec命令,想用哪类shell,就切换即可。而且是向前覆盖的!

另外补文件目录操作常用的命令

1、清屏clear命令(ctrl+l组合键,小写的,效果一样也是清屏)

使用之后,自动清楚屏幕,回到刚刚打开时候的界面


2、显示日期命令date


3、PS(process status)

是LINUX下最常用的也是非常强大的进程查看命令。使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等。总之大部分信息都是可以通过执行该命令得到的。

我们继续启动几个bash,我们再使用ps,就会发现有多个进程出现


其中PID是进程id,TTY是终端的编号

比如,我再打开一个终端,使用ps命令,发现TTY有了变化!


不论是进程id还是终端ID都是唯一的!!!记住!!!

最后的TIME是时间,是指的是cpu运行的时间,0说明都不到一秒钟,就是0,CMD就是命令的意思。

4、注意cd不能跨级改变,必须一级挨着一级的进入或者退出,还有cd目录时,目录后面可以加/或者不加,没有区别


5、linux的远程操作命令telnet

假如现在有一个远程的服务器UNIX,我们可以将这个终端连接远程服务器,进行远程登录!

输入

login:

Password:

即可

联想—无盘工作站,就是这样子远程操作


如果退出,则使用exit退出shell,或者logout退出连接,或者ctrl+d组合键退到本地

6、查看linux的用户密码


这样我们无权限,那就直接去双击打开文件


找到dashuai这个用户,每行分别使用:分开,以前的linux或者unix第二个空是密码,现在变成了x,因为不安全,把密码文件单独放到了同目录下的shadow文件中


我们看看它的属性;群组属于root,只读访问,访问权限是无!其他用户也是无!就是不让看的意思。即使打开了,也是加密的密码形式,不是明文,比较安全。


dashuai:x:1000:1000:Ubuntu,,,:/home/dashuai:/bin/bash

继续看最后面的目录是登录shell,为某个用户设置的shell,/bin/bash  这个bash也是一个文件,显示的是文件的绝对路径。在根目录下的bin目录里。是一个可执行文件。

/home/dashuai是用户的根目录

然后一段用来写一些用户的注释和说明,仅仅是说明,别的用没有。

然后紧着是两个一样或者不一样的数字,第一个是用户id,每个用户一个编号,第二个数字是群组的id,就是在linux里可以把很多用户划分为一个组,每个组一个id。其实没什么意思。

第一个段当然就是当前的用户的用户名了。

7、shell的命令格式

可以带参数或者不带,或者带一些选项,在unix里都是 - 打头。或者两个都带。比如ls还可以加指定目录,显示指定目录

dashuai@ubuntu:~$ ls /bin    dev   initrd.img      lost+found  opt   run      srv  usrboot   etc   initrd.img.old  media       proc  sbin     sys  varcdrom  home  lib             mnt         root  selinux  tmp  vmlinuzdashuai@ubuntu:~$ ls mntls: cannot access mnt: No such file or directorydashuai@ubuntu:~$ ls /mnthgfs


还可以同时显示两个目录,中间隔开一个空格即可。

dashuai@ubuntu:~$ ls /mnt /lib/lib:apparmor                              libnss_mdns_minimal.so.2brltty                                libnss_mdns.so.2cpp                                   libply-boot-client.so.2crda                                  libply-boot-client.so.2.0.0firmware                              libply.so.2hdparm                                libply.so.2.0.0i386-linux-gnu                        libply-splash-core.so.2init                                  libply-splash-core.so.2.0.0klibc-zWH8w1XQzP3rnxOk2AnX61hn0sI.so  libply-splash-graphics.so.2ld-linux.so.2                         libply-splash-graphics.so.2.0.0libcryptsetup.so.4                    libxtables.so.7libcryptsetup.so.4.2.0                libxtables.so.7.0.0libip4tc.so.0                         linux-sound-baselibip4tc.so.0.0.0                     lsblibip6tc.so.0                         moduleslibip6tc.so.0.0.0                     plymouthlibiptc.so.0                          recovery-modelibiptc.so.0.0.0                      resolvconflibnewt.so.0.52                       securitylibnewt.so.0.52.11                    systemdlibnss_mdns4_minimal.so.2             terminfolibnss_mdns4.so.2                     udevlibnss_mdns6_minimal.so.2             ufwlibnss_mdns6.so.2                     xtables/mnt:hgfs

所以,linux里,命令是必须的,命令后面带选项和参数是任意多个的。选项用 - 打头开始写,参数直接写,参数是指定对谁(目标)来做某个命令,选项是细微调整的作用。

当然参数和选项也可以没有,并且命令,选项,参数大小写严格区分,敏感的,大小写不一样,意义不同。case sensitive!

并且不同于windows,在windows里没有这么一说。在windows里开一个终端,开始——cmd,使用dir和ls类似,Dir,dIr,DIR等和dir的效果是一样的。都没有问题。其余类推

记住在unix/linux里严格区分大小写。比如ls,我们用LS,Ls,lS就都不行;

dashuai@ubuntu:~$ lsDesktop    Downloads         family  Pictures  Templates   VideosDocuments  examples.desktop  Music   Public    Ubuntu Onedashuai@ubuntu:~$ LSThe program 'LS' is currently not installed. You can install it by typing:sudo apt-get install sldashuai@ubuntu:~$ lSlS: command not founddashuai@ubuntu:~$ LsLs: command not found


并且记住,每个命令的选项可以使用man来查看,帮助。比如man date,注意其实他不是说真的是男人的意思,是英文online manual pages中间的manual的单词缩写。在线手册

DATE(1)                          User Commands                         DATE(1)NAME       date - print or set the system date and timeSYNOPSIS       date [OPTION]... [+FORMAT]       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]DESCRIPTION       Display the current time in the given FORMAT, or set the system date.       -d, --date=STRING              display time described by STRING, not `now'       -f, --file=DATEFILE              like --date once for each line of DATEFILE       -r, --reference=FILE              display the last modification time of FILE       -R, --rfc-2822              output  date  and time in RFC 2822 format.  Example: Mon, 07 Aug Manual page date(1) line 1 (press h for help or q to quit)

打印和设置系统的日期和时间,可以带多个选项,选项后面是格式。并且只有管理员可以设置日期时间,其他的不行!

在下面告诉了,各种选项的含义和用法。

dashuai@ubuntu:~$ date +%D05/19/14
按照指定的格式显示


具体到什么选项,使用date --help

dashuai@ubuntu:~$ date --helpUsage: date [OPTION]... [+FORMAT]  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]Display the current time in the given FORMAT, or set the system date.  -d, --date=STRING         display time described by STRING, not `now'  -f, --file=DATEFILE       like --date once for each line of DATEFILE  -r, --reference=FILE      display the last modification time of FILE  -R, --rfc-2822            output date and time in RFC 2822 format.                            Example: Mon, 07 Aug 2006 12:34:56 -0600      --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.                            TIMESPEC=`date', `seconds', or `ns' for                            date and time to the indicated precision.                            Date and time components are separated by                            a single space: 2006-08-07 12:34:56-06:00  -s, --set=STRING          set time described by STRING  -u, --utc, --universal    print or set Coordinated Universal Time      --help     display this help and exit      --version  output version information and exitFORMAT controls the output.  Interpreted sequences are:  %%   a literal %  %a   locale's abbreviated weekday name (e.g., Sun)  %A   locale's full weekday name (e.g., Sunday)  %b   locale's abbreviated month name (e.g., Jan)  %B   locale's full month name (e.g., January)  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)  %C   century; like %Y, except omit last two digits (e.g., 20)  %d   day of month (e.g., 01)  %D   date; same as %m/%d/%y  %e   day of month, space padded; same as %_d  %F   full date; same as %Y-%m-%d  %g   last two digits of year of ISO week number (see %G)  %G   year of ISO week number (see %V); normally useful only with %V  %h   same as %b  %H   hour (00..23)  %I   hour (01..12)  %j   day of year (001..366)  %k   hour, space padded ( 0..23); same as %_H  %l   hour, space padded ( 1..12); same as %_I  %m   month (01..12)  %M   minute (00..59)  %n   a newline  %N   nanoseconds (000000000..999999999)  %p   locale's equivalent of either AM or PM; blank if not known  %P   like %p, but lower case  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)  %R   24-hour hour and minute; same as %H:%M  %s   seconds since 1970-01-01 00:00:00 UTC  %S   second (00..60)  %t   a tab  %T   time; same as %H:%M:%S  %u   day of week (1..7); 1 is Monday  %U   week number of year, with Sunday as first day of week (00..53)  %V   ISO week number, with Monday as first day of week (01..53)  %w   day of week (0..6); 0 is Sunday  %W   week number of year, with Monday as first day of week (00..53)  %x   locale's date representation (e.g., 12/31/99)  %X   locale's time representation (e.g., 23:13:48)  %y   last two digits of year (00..99)  %Y   year  %z   +hhmm numeric time zone (e.g., -0400)  %:z  +hh:mm numeric time zone (e.g., -04:00)  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)  %Z   alphabetic time zone abbreviation (e.g., EDT)By default, date pads numeric fields with zeroes.The following optional flags may follow `%':  -  (hyphen) do not pad the field  _  (underscore) pad with spaces  0  (zero) pad with zeros  ^  use upper case if possible  #  use opposite case if possibleAfter any flags comes an optional field width, as a decimal number;then an optional modifier, which is eitherE to use the locale's alternate representations if available, orO to use the locale's alternate numeric symbols if available.Examples:Convert seconds since the epoch (1970-01-01 UTC) to a date  $ date --date='@2147483647'Show the time on the west coast of the US (use tzselect(1) to find TZ)  $ TZ='America/Los_Angeles' dateShow the local time for 9AM next Friday on the west coast of the US  $ date --date='TZ="America/Los_Angeles" 09:00 next Fri'Report date bugs to bug-coreutils@gnu.orgGNU coreutils home page: <http://www.gnu.org/software/coreutils/>General help using GNU software: <http://www.gnu.org/gethelp/>For complete documentation, run: info coreutils 'date invocation'

还有,比如我想中间隔开空格,需要使用引号引起来,或者交叉使用格式直接挨着写就行。

dashuai@ubuntu:~$ date +%F%A%T2014-05-19Monday19:16:19dashuai@ubuntu:~$ date +"%F %A %T"2014-05-19 Monday 19:16:37

设置时间必须是管理员才可以,我们在passwd文件里查看,root是管理员,组ID为0,我们可以再去etc的group文件里查看组id



这里在学一个命令,帮助查看。

id命令

dashuai@ubuntu:~$ iduid=1000(dashuai) gid=1000(dashuai) groups=1000(dashuai),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),107(lpadmin),124(sambashare)dashuai@ubuntu:~$ id rootuid=0(root) gid=0(root) groups=0(root)
可以显示当前用户的id,组id,所属的组,还可以指定其他用户的id,组id,所属于的组。

=============================================================================================================

小结;

1、熟悉linux的文件系统,是从 / 开始的树状结构


2、

绝对路径;以 / 打头,在其他地方/表示分割

相对路径;从当前目录之下开始

要么是绝对路径要么是相对路径

cd ./abc和cd abc是等的,即只要不是绝对路径那就是相对路径!


3、当前目录;cd . 或者到上一目录是 .. 或者 ~ 表示主目录


4、linux支持多用户,每个用户都要登录才能使用,每个用户都有一个用户名username,一个密码passwd,一个用户id,也即是UID,另外每个用户也属于一个组,有一个组id,叫GID。另外管理员还指定了用户登录了哪个shell。这些信息都在根目录下的etc的passwd文件内,只读无法修改,只有管理员可以修改。并且密码是x代表的,真正的密码文件在etc的shadow文件内,都是加密存储的。


5、shell是和用户直接打交道的,每个终端都至少有一个shell,多个的条件下,只能有一个起作用。当运行一个shell的时候又开了一个shell,那么之前的那个shell就处于等待状态,直到新的退出为止。如果一个终端没有shell,那么就没法工作,就会关闭!比如退出shell的时候会关闭终端。


6、常用的命令,切换shell的exec命令,远程登录telnet,帮助man,清屏clear或者ctrl+小写l的组合键,创建目录mkdir,列出文件ls,查看进程ps,改变目录cd,打印当前路径pwd,上一个目录..,本目录.,根目录/,查看设置日期时间date,显示id的id命令,新建空文件touch,查看文件内容cat命令,新建有内容的文件echo命令,复制原文件是cp命令,删除文件或者目录rmdir命令,移动目录或者文件是mv,统计文档的内容wc命令,grep查看文件的指定关键词,more,less分页显示文本内容,Head,tail分别显示文件开头和结尾内容,tree显示目录树。

shell的每一个命令都对应一个可执行文件,基本上命令和可执行文件是对等的,在bin目录下。至于怎么执行就是内核的事情了,shell就不管了。


当然shell也有一些内部命令。


接触这些的目的就是为了能在linux环境下进行开发,目的不是为了精通linux内核或者系统。分清主次,术业有专攻。


0 0