一、基本命令格式
[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 7月 28 10:11 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 8月 1 00:20 file
-rw-r--r--. 1 root root 158 8月 1 00:21 file.zip
-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 ~]#
[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 7月 28 10:11 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 8月 1 00:22 dir
-rw-r--r--. 1 root root 596 8月 1 00:22 dir.zip
-rw-r--r--. 1 root root 0 8月 1 00:20 file
-rw-r--r--. 1 root root 158 8月 1 00:21 file.zip
-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.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 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 ~]# 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 8月 1 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 8月 1 02:56 hello.sh
drwx------. 2 root root 16384 7月 28 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!!!