Linux常用命令

一、基本命令格式


[root@mook ~]# 

root : 代表当前登录用户
mook : 主机名
~ : 当前目录(家目录)
root用户的家目录是/root
# : 超级用户的提示符(普通用户的提示符是$)

这里写图片描述

[root@mook ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog
[root@mook ~]# ls -l
总用量 44
-rw-------. 1 root root  1208 7月  28 10:11 anaconda-ks.cfg
-rw-r--r--. 1 root root 24772 7月  28 10:11 install.log
-rw-r--r--. 1 root root  7690 7月  28 10:10 install.log.syslog

“.”代表ACL权限
“1”代表引用计数
“root”代表文件所有者
“root”代表文件所属组
“1208”代表文件大小(字节)
“时间”代表文件最后一次被修改的时间
这里写图片描述

[root@mook ~]# ls -lh
总用量 44K
-rw-------. 1 root root 1.2K 7月  28 10:11 anaconda-ks.cfg
-rw-r--r--. 1 root root  25K 7月  28 10:11 install.log
-rw-r--r--. 1 root root 7.6K 7月  28 10:10 install.log.syslog

二、文件处理命令


目录处理命令

建立目录:mkdir

这里写图片描述

切换所在目录:cd

这里写图片描述
“ctrl+L”:清屏

查询所在目录位置:pwd

删除空目录:rmdir

文件处理命令

删除文件或目录:rm

这里写图片描述

复制命令:cp

这里写图片描述

剪切或改名命令:mv

这里写图片描述

常见目录的作用

这里写图片描述

这里写图片描述
bin目录下的命令用户都可以使用,但是sbin下的命令只有root用户可以使用。

这里写图片描述

这里写图片描述
这里写图片描述

链接命令

这里写图片描述

这里写图片描述
这里写图片描述
硬链接:引用计数会增加;删除一个不会影响另一个,但是它们的文件内容一致。

这里写图片描述
这里写图片描述
软链接:源文件要写绝对路径!


三、文件搜索命令


文件搜索命令:locate

只能搜索文件,按文件名搜索文件,速度快
这里写图片描述
数据库不是实时更新的,可以强制更新。
locate和命令搜索命令会遵循如下的配置文件规则:
这里写图片描述

命令搜索命令:whereis和which

只能搜索系统命令!
这里写图片描述

[root@mook ~]# whereis ls
ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz
[root@mook ~]# whoami
root
[root@mook ~]# whatis ls
ls                   (1)  - list directory contents
ls                   (1p)  - list directory contents

这里写图片描述

[root@mook ~]# which ls
alias ls='ls --color=auto'
    /bin/ls
[root@mook ~]# whereis cd
cd: /usr/share/man/man1p/cd.1p.gz /usr/share/man/man1/cd.1.gz
[root@mook ~]# which cd
/usr/bin/which: no cd in (/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

cd命令是shell的内置命令,所以没有执行文件!

这里写图片描述

文件搜索命令:find

强大!
完全匹配!!
这里写图片描述

这里写图片描述
注意:文件名加双引号!

这里写图片描述

这里写图片描述

这里写图片描述

[root@mook ~]# find . -size 25k
./install.log
[root@mook ~]# find /etc -size +2M
/etc/selinux/targeted/policy/policy.24
/etc/selinux/targeted/modules/active/policy.kern

这里写图片描述

字符串搜索命令:grep

搜索文件当中的内容
这里写图片描述

[root@mook ~]# grep "size" anaconda-ks.cfg 
#part /boot --fstype=ext4 --size=200
#part /home --fstype=ext4 --size=3000
#part swap --size=1000
#part / --fstype=ext4 --grow --size=200
[root@mook ~]# grep -v "size" anaconda-ks.cfg 
# Kickstart file automatically generated by anaconda.

#version=DEVEL
install
cdrom
lang zh_CN.UTF-8
keyboard us

这里写图片描述

四、帮助命令


帮助命令:man

[root@mook ~]# 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  alphabeti-
       cally if none of -cftuvSUX nor --sort.

       Mandatory  arguments  to long options are manda-
       tory for short options too.

       -a, --all
              do not ignore entries starting with .

/d

“/d”然后回车,按“n”,选择带“d”字符串
“/-d”然后回车,按“n”,选择查看“-d”命令选项

这里写图片描述

这里写图片描述
数字前不需要加“-”!

[root@mook ~]# man -f password
password-auth-ac [system-auth-ac] (5)  - Common configuration files for PAMified services written by authconfig(8)
[root@mook ~]# whatis password
password-auth-ac [system-auth-ac] (5)  - Common configuration files for PAMified services written by authconfig(8)

这里写图片描述

[root@mook ~]# apropos passwd
chpasswd             (8)  - update passwords in batch mode
fgetpwent_r [getpwent_r] (3)  - get passwd file entry reentrantly
getpwent_r           (3)  - get passwd file entry reentrantly
gpasswd              (1)  - administer /etc/group and /etc/gshadow
kpasswd              (1)  - change a user's Kerberos password
lpasswd              (1)  - Change group or user password
lppasswd             (1)  - add, change, or delete digest passwords
pam_localuser        (8)  - require users to be listed in /etc/passwd
pam_passwdqc         (8)  - Password quality-control PAM module
passwd               (1)  - update user's authentication token

其他帮助命令

这里写图片描述

[root@mook ~]# ls --help
用法:ls [选项]... [文件]...
列出 FILE 的信息(默认为当前目录)。
如果不指定-cftuvSUX 或--sort 选项,则根据字母大小排序。

长选项必须使用的参数对于短选项时也是必需使用的。
  -a, --all         不隐藏任何以. 开始的项目
  -A, --almost-all      列出除. 及.. 以外的任何项目
      --author          与-l 同时使用时列出每个文件的

这里写图片描述

[root@mook ~]# whereis cd
cd: /usr/share/man/man1p/cd.1p.gz /usr/share/man/man1/cd.1.gz
[root@mook ~]# help cd
cd: cd [-L|-P] [dir]
    Change the shell working directory.

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

whereis cd 无可执行文件

[root@mook ~]# whereis ls
ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz

这里写图片描述

五、压缩与解压缩命令


.zip格式压缩

这里写图片描述

[root@mook ~]# touch file
[root@mook ~]# zip file.zip file
  adding: file (stored 0%)
[root@mook ~]# ll
总用量 48
-rw-------. 1 root root  1208 728 10:11 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 81 00:20 file
-rw-r--r--. 1 root root   158 81 00:21 file.zip
-rw-r--r--. 1 root root 24772 728 10:11 install.log
-rw-r--r--. 1 root root  7690 728 10:10 install.log.syslog
[root@mook ~]# 
[root@mook ~]# mkdir dir
[root@mook ~]# touch dir/file1
[root@mook ~]# touch dir/file2
[root@mook ~]# touch dir/file3
[root@mook ~]# cd dir/
[root@mook dir]# ls
file1  file2  file3
[root@mook dir]# cd .. 
[root@mook ~]# ls
anaconda-ks.cfg  file      install.log
dir              file.zip  install.log.syslog
[root@mook ~]# zip -r dir.zip dir
  adding: dir/ (stored 0%)
  adding: dir/file2 (stored 0%)
  adding: dir/file3 (stored 0%)
  adding: dir/file1 (stored 0%)
[root@mook ~]# ll
总用量 56
-rw-------. 1 root root  1208 728 10:11 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 81 00:22 dir
-rw-r--r--. 1 root root   596 81 00:22 dir.zip
-rw-r--r--. 1 root root     0 81 00:20 file
-rw-r--r--. 1 root root   158 81 00:21 file.zip
-rw-r--r--. 1 root root 24772 728 10:11 install.log
-rw-r--r--. 1 root root  7690 728 10:10 install.log.syslo

这里写图片描述

.gz格式压缩

这里写图片描述

[root@mook ~]# ls
anaconda-ks.cfg  dir  file  install.log  install.log.syslog
[root@mook ~]# gzip file
[root@mook ~]# ls
anaconda-ks.cfg  file.gz      install.log.syslog
dir              install.log
[root@mook ~]# gzip dir
gzip: dir is a directory -- ignored
[root@mook ~]# gzip -r dir
[root@mook ~]# ll
总用量 52
-rw-------. 1 root root  1208 7月  28 10:11 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 8月   1 00:36 dir
-rw-r--r--. 1 root root    25 8月   1 00:20 file.gz
-rw-r--r--. 1 root root 24772 7月  28 10:11 install.log
-rw-r--r--. 1 root root  7690 7月  28 10:10 install.log.syslog
[root@mook ~]# cd dir/
[root@mook dir]# ll
总用量 12
-rw-r--r--. 1 root root 26 8月   1 00:22 file1.gz
-rw-r--r--. 1 root root 26 8月   1 00:22 file2.gz
-rw-r--r--. 1 root root 26 8月   1 00:22 file3.gz

这里写图片描述

[root@mook ~]# ll
总用量 52
-rw-------. 1 root root  1208 7月  28 10:11 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 81 00:36 dir
-rw-r--r--. 1 root root    25 8月   1 00:20 file.gz
-rw-r--r--. 1 root root 24772 7月  28 10:11 install.log
-rw-r--r--. 1 root root  7690 7月  28 10:10 install.log.syslog
[root@mook ~]# gzip -d file.gz 
[root@mook ~]# ll
总用量 48
-rw-------. 1 root root  1208 7月  28 10:11 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 81 00:36 dir
-rw-r--r--. 1 root root     0 8月   1 00:20 file
-rw-r--r--. 1 root root 24772 7月  28 10:11 install.log
-rw-r--r--. 1 root root  7690 7月  28 10:10 install.log.syslog
[root@mook ~]# gzip -d dir 
gzip: dir is a directory -- ignored
[root@mook ~]# gunzip dir
gzip: dir is a directory -- ignored
[root@mook ~]# gunzip -r dir
[root@mook ~]# cd dir/
[root@mook dir]# ll
总用量 0
-rw-r--r--. 1 root root 0 8月   1 00:22 file1
-rw-r--r--. 1 root root 0 8月   1 00:22 file2
-rw-r--r--. 1 root root 0 8月   1 00:22 file3

.bz2格式压缩

这里写图片描述

这里写图片描述

.tar.gz和.tar.bz2压缩格式

这里写图片描述

这里写图片描述

[root@mook ~]# ls
anaconda-ks.cfg  dir  file  install.log  install.log.syslog
[root@mook ~]# tar -zcvf dir.tar.gz dir
dir/
dir/file2
dir/file3
dir/file1
[root@mook ~]# ls
anaconda-ks.cfg  dir.tar.gz  install.log
dir              file        install.log.syslog
[root@mook ~]# tar -jcvf dir.tar.bz2 dir
dir/
dir/file2
dir/file3
dir/file1
[root@mook ~]# ls
anaconda-ks.cfg  dir.tar.bz2  file         install.log.syslog
dir              dir.tar.gz   install.log

解压到指定目录下:

[root@mook ~]# ls
anaconda-ks.cfg  dir.tar.bz2  file         install.log.syslog
dir              dir.tar.gz   install.log
[root@mook ~]# tar -jxvf dir.tar.bz2 -C /tmp/
dir/
dir/file2
dir/file3
dir/file1

[root@mook tmp]# ls
dir  yum.log

多个文件压缩并将压缩包放到指定目录下:

[root@mook ~]# tar -zcvf /tmp/test.tar.gz dir anaconda-ks.cfg 
dir/
dir/file2
dir/file3
dir/file1
anaconda-ks.cfg
[root@mook ~]# cd /tmp/
[root@mook tmp]# ls
test.tar.gz  yum.log
[root@mook tmp]# tar -zxvf test.tar.gz 
dir/
dir/file2
dir/file3
dir/file1
anaconda-ks.cfg
[root@mook tmp]# ls
anaconda-ks.cfg  dir  test.tar.gz  yum.log

查看压缩文件中的内容:

[root@mook tmp]# ls
anaconda-ks.cfg  dir  test.tar.gz  yum.log
[root@mook tmp]# tar -ztvf test.tar.gz 
drwxr-xr-x root/root         0 2016-08-01 00:41 dir/
-rw-r--r-- root/root         0 2016-08-01 00:22 dir/file2
-rw-r--r-- root/root         0 2016-08-01 00:22 dir/file3
-rw-r--r-- root/root         0 2016-08-01 00:22 dir/file1
-rw------- root/root      1208 2016-07-28 10:11 anaconda-ks.cfg

六、关机和重启命令


这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

查看之前和当前系统的运行级别:

[root@mook tmp]# runlevel
N 3

这里写图片描述

这里写图片描述

七、其他常用命令


挂载命令

这里写图片描述

这里写图片描述

[root@mook home]# ls
lost+found
[root@mook home]# vi hello.sh
[root@mook home]# chmod 755 hello.sh 
[root@mook home]# ll
总用量 20
-rwxr-xr-x. 1 root root    34 81 02:56 hello.sh
drwx------. 2 root root 16384 728 09:59 lost+found
[root@mook home]# ./hello.sh 
hello mook!!!
[root@mook home]# /home/hello.sh 
hello mook!!!
[root@mook home]# mount -o remount,noexec /home/
[root@mook home]# ls
hello.sh  lost+found
[root@mook home]# ./hello.sh
-bash: ./hello.sh: 权限不够
[root@mook home]# mount -o remount,exec /home/
[root@mook home]# ./hello.sh
hello mook!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值