zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux(1)简单shell命令

2023-09-11 14:21:14 时间

与时间相关的shell命令

1、date命令:查看或修改系统时间

例:查看系统时间
[root@localhost profile.d]# date

2019年 09月 17日 星期二 11:20:28 CST

修改系统时间:date 月日时分/date 年月日时分
[root@localhost profile.d]# date 09171121

2019年 09月 17日 星期二 11:21:00 CST

2、cal命令:显示日历

[root@localhost profile.d]# cal

      九月 2019     
日 一 二 三 四 五 六
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

3、pwd命令:显示当前目录的绝对路径

[root@localhost profile.d]# pwd

/etc/profile.d

4、cd命令:切换到指定目录

[root@localhost profile.d]# cd /etc
[root@localhost etc]# pwd

/etc

5、ls命令:显示指定目录中的文件和子目录信息

参数 :

  • -a 显示所有文件及目录 ,包括隐藏文件和隐藏子目录(ls内定将文件名或目录名称开头为"."的视为隐藏档,不会列出)
  • -l 除文件名称外,亦将文件型态、权限、拥有者、文件大小等资讯详细列出
  • -r 将文件以相反次序显示(原定依英文字母次序)
  • -t 将文件依建立时间之先后次序列出
  • -A 同 -a ,但不列出 “.” (目前目录) 及 “…” (父目录)
  • -F 在列出的文件名称后加一符号;例如可执行档则加 “*”, 目录则加 “/”
  • -R 若目录下有文件,则以下之文件亦皆依序列出

查看当前目录中的文件和子目录
[root@localhost etc]# ls

abrt                        hosts.allow               prelink.conf.d
adjtime                     hosts.deny                printcap
hosts                       ppp

[root@localhost etc]# ls -a

    hba.conf                    portreserve               yum.conf
    host.conf                   postfix                   yum.repos.d
    hostname                    ppp
    hosts                       prelink.conf.d

查看当前目录中所有文件和子目录的详细信息
[root@localhost etc]# ls -al

总用量 1664
drwxr-xr-x. 159 root root     8192 9月  17 10:25 .
dr-xr-xr-x.  17 root root     4096 9月   8 22:04 ..
drwxr-xr-x.   3 root root       97 9月   8 21:57 abrt
-rw-r--r--.   1 root root       16 9月   8 22:04 adjtime
-rw-r--r--.   1 root root      813 9月  11 2015 yum.conf
drwxr-xr-x.   2 root root       24 9月   8 22:03 yum.repos.d

6、cat命令:显示文本文件的内容

显示/etc/profile目录下的文本文件的内容
[root@localhost etc]# cat /etc/profile

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
        fi
    fi
done

unset i
unset -f pathmunge

文件退出直接输:q
查看当前目录中profile文件的内容,并在每一行前加上行号
[root@localhost etc]# cat -n profile

     1	# /etc/profile
     2	
     3	# System wide environment and startup programs, for login setup
     4	# Functions and aliases go in /etc/bashrc
     5	
     6	# It's NOT a good idea to change this file unless you know what you
     7	# are doing. It's much better to create a custom.sh shell script in
     8	# /etc/profile.d/ to make custom changes to your environment, as this
     9	# will prevent the need for merging in future updates.
    10	
    11	pathmunge () {
    12	    case ":${PATH}:" in
    13	        *:"$1":*)
    14	            ;;
    15	        *)
    16	            if [ "$2" = "after" ] ; then
    17	                PATH=$PATH:$1
    18	            else
    19	                PATH=$1:$PATH
    20	            fi
    21	    esac
    22	}
    23	

7、more命令:分屏显示文件的内容

格式:more 文件
[root@localhost etc]# more /etc/profile

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}

less命令与more命令类似
[root@localhost etc]# less /etc/profile

8、tail命令:显示文本文件的结尾部分

[root@localhost etc]# tail /etc/profile

if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

head命令显示文本文件的开头部分
[root@localhost etc]# head -10 /etc/profile
-10是显示前10行
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

9、man命令:显式指定命令的手册页帮助信息

[root@localhost etc]# man

您需要什么手册页?

q键则退出man命令
[root@localhost etc]# man ls
[root@localhost etc]# ls -help

ls:无效选项 -- e

Try 'ls --help' for more information.

[root@localhost etc]# ls --help

用法:ls [选项]... [文件]...
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			不隐藏任何以. 开始的项目
  -A, --almost-all		列出除. 及.. 以外的任何项目
      --author			与-l 同时使用时列出每个文件的作者
  -b, --escape			以八进制溢出序列表示不可打印的字符
      --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~

清屏

1、clear命令:清除当前终端的屏幕内容

[root@localhost etc]#clean

2、wc命令:显示文本文件的行数、字数、字符数

[root@localhost etc]# wc -c /etc/profile
-c仅显示文件的字节数
1750 /etc/profile

[root@localhost etc]# wc -l /etc/profile
-l仅显示文件的行数
76 /etc/profile

[root@localhost etc]# wc -w /etc/profile
-w仅显示文件的单词数
252 /etc/profile