mkdir
-m 指定要创建目录的权限
-p 递归创建目录
[root@tz home]# mkdir -m 777 test [root@tz home]# ll 总用量 8 drwx------. 2 oldboy oldboy 4096 12月 7 19:57 oldboy drwxrwxrwx. 2 root root 4096 12月 7 20:00 test
[root@tz home]# mkdir -p test/newtest
rmdir
删除目录,目录必须为空
cp
-a 相当于-dpr
-p 连同档案的属性一起复制过去
-r 递归,复制目录
-f 强制复制
-u 若目标文件存在,源文件比目标文件新才复制
-i 询问
rm
-r 进入目录依次删除目录下的文件再删除目录
-f 强制删除
-i 询问
如下因为rm是rm -i 的别名,所以会依次询问是否删除文件
[root@tzPC tz]# rm -r root/ rm: descend into directory ‘root/’? y rm: remove regular file ‘root/.bash_logout’? y rm: remove regular file ‘root/.cshrc’?
删除排除某个文件的其他所有文件
#开启扩展通配符 $ shopt -s extglob #查看是否开启 $ shopt -s #删除除full.sql其他所有文件 $ rm -rf !(full.sql)
man
-k 查找相关命令
如图查找包含ps的相关命令
[root@tzPC /]# man -k ps
abrt-action-analyze-oops (1) - Calculate and save UUID and duplicate hash for a problem data director...
abrt-action-analyze-vmcore (1) - Extracts the oops message from the kernel core and install the kerne...
abrt-action-check-oops-for-hw-error (1) - Checks dmesg element, marks problem as not-reportable if ha...
abrt-action-save-kernel-data (1) - Creates uReport mandatory files for kernel oopses.
abrt-dump-oops (1) - Extract oops from FILE (or standard input)
abrt-harvest-pstoreoops (1) - Reconstruct oops from /sys/fs/pstore/* files, create ABRT problems and ...
abrt-merge-pstoreoops (1) - Scans files for split oops message. Can print and/or delete them.
abrt-oops.conf (5) - Configuration file for ABRT's Kernel Oops extractor
accessdb (8) - dumps the content of a man-db database in a human readable format
B::Concise (3pm) - Walk Perl syntax tree, printing concise info about ops
B::Debug (3pm) - Walk Perl syntax tree, printing debug info about ops
B::Terse (3pm) - Walk Perl syntax tree, printing terse info about ops
btrfs-balance (8) - balance block groups on a btrfs filesystem
capsh (1) - capability shell wrapper
info ps
查看关于ps命令的帮助信息
ps -help
查看关于ps命令的帮助信息
ls
-a 显示隐藏文件
-R 递归显示目录
-F 区分文件跟目录
目录后有/,可执行文件后有*,软硬链接后有@
[root@tzPC /]# ls -F / bin@ dev/ home/ lib64@ mnt/ proc/ run/ srv/ tmp/ var/ boot/ etc/ lib@ media/ opt/ root/ sbin@ sys/ usr/
?代表一个字符
[root@tzPC ~]# ls 2018-05-12_21:10:44 check_cj.sh for4.sh if-2.sh print.sh test2.sh useradd.sh anaconda-ks.cfg check.sh for5.sh if-3.sh root.tar.gz test3.sh useradd.txt break.sh for1.sh for6.sh log.sh special_var.sh test4.sh while1.sh case1.sh for2.sh for_99.sh ls_root.tar.gz test-1.sh test_read.sh while2.sh case2.sh for3.sh if-1.sh ping.sh test-2.sh useradd2.sh [root@tzPC ~]# ls for?.sh for1.sh for2.sh for3.sh for4.sh for5.sh for6.sh
*代表零个或多个字符
[root@tzPC ~]# ls f* for1.sh for2.sh for3.sh for4.sh for5.sh for6.sh for_99.sh
在过滤器中使用?跟*称为文件扩展匹配,指的是使用通配符进行模式匹配的过程,通配符正式名字为元字符通配符。
使用中括号指定字符跟范围匹配
[root@tzPC ~]# ls for[12].sh for1.sh for2.sh [root@tzPC ~]# ls for[1-6].sh for1.sh for2.sh for3.sh for4.sh for5.sh for6.sh
touch
创建文件,如果文件存在则更新访问时间和修改时间为当前时间,注意ls默认显示的都是文件的修改时间
-a 改变只更新访问时间
显示修改时间需要在ll后添加--time=atime参数
[root@tzPC tz]# ll total 4 -rw-r--r--. 1 root root 38 Aug 1 16:02 test.txt [root@tzPC tz]# touch -a test.txt [root@tzPC tz]# ll --time=atime total 4 -rw-r--r--. 1 root root 38 Aug 4 11:59 test.txt [root@tzPC tz]# ll total 4 -rw-r--r--. 1 root root 38 Aug 1 16:02 test.txt
mv
移动或更改文件名
文件的inode编号和时间戳不会改变
file
查看文件类型
可以确定文件的字符编码
[root@tzPC test]# file test.txt test.txt: ASCII text
可以确定文件链接到哪个文件上
[root@tzPC ~]# file ls_root.tar.gz ls_root.tar.gz: symbolic link to `root.tar.gz'
判断文件是一个脚本
[root@tzPC ~]# file for1.sh for1.sh: Bourne-Again shell script, ASCII text executable
可以确定程序编译时面向的平台以及需要何种类型的库
[root@tzPC ~]# file /bin/ls /bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=aaf05615b6c91d3cbb076af81aeff531c5d7dfd9, stripped
cat
-n 显示行号
-b 只给有文本的内容显示行号
-T 不显示制表符,制表符会被^I取代
[root@tzPC tz]# cat -T test.txt ^Iabc^Icba
tail
显示文件最后几行内容,默认显示最后10行
-n 显示最后多少行
-f 实时显示,适用于实时查看日志
[root@tzPC tz]# tail -n 2 /etc/passwd tz9:x:1015:1015::/home/tz9:/bin/bash tz10:x:1016:1016::/home/tz10:/bin/bash
head
查看文件开头几行内容,默认显示文件前10行
[root@tzPC tz]# head -2 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin
kill命令
结束进程
在Linux中,进程之间通过信号来通信。要发送进程信号,你必须是进程的属主或root用户。
kill [进程号] 关闭单个进程
killall 和pkill 关闭指定名字的进程
kill -l 列出所有支持的信号
常用kill 9
[root@tzPC ~]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
常见使用的信号,网络编程会经常使用
信号编号 信号名
1)SIGHUP 重新加载配置
2)SIGINT 键盘中断 crtl+c
3) SIGQUIT 退出
9)SIGKILL 强制终止
15)SIGTERM 终止(正常结束),缺省信号
18)SIGCONT 继续
19)SIGSTOP 停止
20) SIGTSTP 暂停 crtl+z
举例
[root@tzPC ~]# ps -aux | grep a.txt
root 3892 0.0 0.2 149148 4920 pts/0 T 18:37 0:00 vim a.txt
root 3943 0.0 0.0 112712 968 pts/0 R+ 18:53 0:00 grep --color=auto a.txt
[root@tzPC ~]# kill -9 3892
[root@tzPC ~]# killall sshd
[root@tzPC ~]# pkill sshd
[root@tzPC ~]# kill -s HUP 3940
[root@tzPC ~]# killall http*
mount
默认会输出当前系统上挂载的设备列表
[root@tzPC ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=1003188k,nr_inodes=250797,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
...
设备文件名
挂载到虚拟目录的挂载点
文件系统类型
已挂载媒体的访问状态
手动挂载命令
mount -t vfat /dev/sdb1 /media/disk
常用文件系统类型
vfat:windows长文件系统
ntfs:Windows常见文件系统,如win7,win10,winxp
iso9660:标准CD-ROM文件系统
-o参数允许在挂载文件系统时添加一些以逗号分隔的额外选项,如下
ro:以只读形式挂载
rw:以读写形式挂载
user:允许普通用户挂载文件系统
check=none:挂载文件系统时不进行完整性校验
loop:挂载一个文件
umount
移除挂载的媒体文件
du命令
默认显示当前目录下所有文件、目录、子目录的磁盘使用情况。
-c:显示所有已列出文件总的大小
-h:按常见大小单位输出,如KB
-s:显示总大小
[root@tzPC ~]# du -hs /home/tz/.viminfo 4.0K /home/tz/.viminfo
sort
对数据进行排序
默认会把数字当成字符来排序如下
[root@tzPC ~]# sort file1 1 10 12 124 133 2 22 24 33 45 64 67 88 99
-n 按数值大小排序
[root@tzPC ~]# sort -n file1 1 2 10 12 22 24 33 45 64 67 88 99 124 133
-M按月排序
[root@tzPC ~]# sort -M file2 Jan Feb Mar Apr May Jun
-t分隔字符,-k指定排序的字段
如下以:分隔,第三个字段用户ID进行排序
[root@tzPC ~]# sort -t ':' -k 3 -n /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync
-r降序排列
[root@tzPC etc]# du -sh * | sort -nr 1004K pki 656K services 604K ssh 340K sysconfig 244K vmware-tools 168K latrace.d 148K postfix 128K lvm 128K libreport 108K pam.d 104K dbus-1 72K profile.d 72K httpd
grep
[root@tzPC ~]#grep three file1 three
[root@tzPC ~]#grep t file1 two three
-v反向搜索,输入不匹配该模式的行
[root@tzPC ~]#grep -v t file1 one four five
-n显示行号
[root@tzPC ~]#grep -n t file1 2:two 3:three
-e指定多个匹配模式
[root@tzPC ~]#grep -e t -e f file1 two three four five
中括号匹配模式
[root@tzPC ~]#grep [tf] file1 two three four five
gzip
压缩
[root@tzPC ~]#gzip test.txt [root@tzPC ~]#gunzip test.txt.gz
tar
归档
-c 创建一个归档文件
-v 显示详细信息
-f 输出到文件或者目录
创建归档
[root@tzPC ~]# tar -cvf test.tar test1/ test2/ test1/ test2/
查看归档但不提取
[root@tzPC ~]# tar -tf test.tar test1/ test2/
提取归档文件或目录
[root@tzPC ~]# tar -xvf test.tar test1/ test2/ [root@tzPC ~]# ls test1 test2 test.tar test.txt
如果看到以tgz结尾的文件,这是用gzip压缩过的tar文件,使用如下命令解压
tar -zxvf filename.tgz
history
可查看最近使用过的1000条命令
!!能唤出上一条使用过的命令
命令历史记录保存在用户的主目录名称为.bash_history文件中,使用过程中的历史记录首先记录在内存中退出时才写入到文件中
如果在退出之前强制将内存中的记录写到文件中需要使用history -a 命令
强制更新后,在其他终端窗口是不会自动跟新需使用history -n命令强制读取更新后的.bash_history文件(主要是因为.bash_histroy文件只在打开的搜个终端会话中才会被读取)
echo
echo -n 不换行输出
date
修改时间
date -s "2018-5-25 21:05"
date -s 20180523 #设置撑2018年5月23日,时间会清空变为00:00:00
date -s 01:01:01 #设置具体时间不会对日期做更改
只显示年月日
[root@tzPC ~]# date +%Y-%m-%d
2020-07-15
显示年月日时间,因为有空格需要加上引号
[root@tzPC ~]# date +"%Y-%m-%d %H:%M:%S"
2020-07-15 09:50:51
年分小写只有两位,中间的连接符可以随便换
[root@tzPC ~]# date +"%y_%m_%d+%H:%M:%S".mysql.tar.gz
20_07_15+09:54:43.mysql.tar.gz
date命令加减操作:
date +%Y%m%d #显示当天年月日
date -d "+1 day" +%Y%m%d #显示明天的日期
date -d "-1 day" +%Y%m%d #显示昨天的日期
date -d "-1 month" +%Y%m%d #显示上一月的日期
date -d "+1 month" +%Y%m%d #显示下一月的日期
date -d "-1 year" +%Y%m%d #显示前一年的日期
date -d "+1 year" +%Y%m%d #显示下一年的日期
scp
远程复制命令
#从远程主机复制文件到本地 sudo scp root@10.154.0.122:remote_path/remote_file . #从本地复制文件到远端 sudo scp local_file root@192.168.0.1:remote_folder
fc
当我们输入了一串很长的命令时,执行的时候出错,我们就能输入fc,进入vim编辑器,修改我们上一条出错的命令,修改完毕后退出编辑器就能自动执行该命令。很方便。
vs
打开vim编辑器,在命令模式输入:vs b 会打开一个文件名为b的分屏