以下所有操作为centos6.7环境
一、创建目录:mkdir
命令:mkdir data
创建递归目录参数[-p],如可创建 a/b/c
命令:mkdir -p a/b/c
[root@localhost ~]# mkdir data #创建目录
[root@localhost ~]# ll
drwxr-xr-x. 2 root root 6 4月 24 15:49 data
[root@localhost ~]# mkdir -p /data/test1/ # “-p”递归
二、创建空文件:touch
命令:touch /data/test.txt (在data目前下创建test.txt文件)
或直接在data目录下创建:touch test.txt或echo >test.txt
同时建多个文件:touch 1.txt 2.txt 3.txt或touch {a,b,c,}.txt
批量删除:rm -rf [1-9,a-z].txt
[root@localhost ~]# touch test.txt #创建空文件
[root@localhost ~]# ll
-rw-r--r--. 1 root root 0 4月 24 15:52 test.txt
三、编辑文件内容:vi
命令:vi [file] ,然后按i ,编辑完后按[:wq]强制保存并退出
方法一:
[root@localhost ~]# vi test.txt #编辑文件
或echo “i am stduying linux” >test.txt
[root@localhost ~]# echo "i am stduying linux" >test.txt #将"i am stduying linux"重定向到test.txt
[root@localhost ~]# cat test.txt #查看文件
i am stduying linux
[root@localhost ~]#
说明:“>” 为重定向,箭头指向的文件原内容覆盖
方法二:
[root@localhost ~]# echo "I love you" >>test.txt #说明:“>>”直接在文件后面追加
[root@localhost ~]# cat test.txt
i am stduying linux
I love you
[root@localhost ~]#
方法三:
[root@localhost ~]# cat >>test.txt <<EOF #“EOF”可为任何字符,但语句结束时需一致
I am stduying linux
I love you
3QEOF #“EOF”可为任何字符,但需与语句开头的一致
[root@localhost ~]# cat test.txt
I am stduying linux
I love you
3Q[root@localhost ~]#
四、拷贝文件:cp [源文件或目录] [目的目录]
将test.txt文件拷贝到tmp目录下
[root@localhost ~]# cp test.txt /tmp/
[root@localhost ~]# ls /tmp/
test.txt
[root@localhost ~]#
说明:拷贝目录时加参数“ -a、-p”例如:cd -ap /data /tmp/
cmd.run "cp -r /opt/se/modules/ /opt/se/modules.bak20180514"
[root@0 salt]# scp /opt/salt/history.py dwouxuetong@192.169.4.67:~ //192.169.4.67:为目的IP
五、文件或目录的移动:mv
命令:mv test.txt /root/
[root@localhost ~]# mv test.txt /root/data/
[root@localhost ~]# ls /root/data/
test.txt
[root@localhost ~]#
删除文件或目录
命令:rm -rf test.txt
[root@localhost ~]# rm -rf test.txt
六、过虑查看文件内容:grep、awk、sed
问题:查看test.txt文件里的除了oldboy内容
例1:
[root@localhost ~]# cat test.txt | grep -v “oldboy”
test
ll
[root@localhost ~]#
说明:cat查看;“|”相当管道作用;“-v”排除后面内容
例2:
[root@localhost ~]# grep -v oldboy test.txt -----“grep”过虑
test
ll
[root@localhost ~]#
例3:
[root@localhost ~]# head -2 test.txt #“head”显示,“-2”显示2行,默认是显示10行
test
ll
[root@localhost ~]#
七、查看系统的命令别名:alias “unalias”取消别名
[root@localhost data]# alias
alias cp=‘cp -i‘
alias egrep=‘egrep --color=auto‘
alias fgrep=‘fgrep --color=auto‘
alias grep=‘grep --color=auto‘
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias mv=‘mv -i‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
[root@localhost data]#
八、查看test.txt文件的第20至30行内容 “seq”生成行数;“tail”查看最后多少行;“head”查看最前多少行
[root@localhost ~]# seq 100 >test.txt
例1:
[root@localhost ~]# head -30 test.txt | tail -11
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~]#
例2:
[root@localhost ~]# sed -n ‘20,30p‘ test.txt #“p”打印,“-n”取消默认输出
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~]#
例3:
[root@localhost ~]# awk ‘{if(NR>19 && NR <31) print $1}‘ test.txt
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~]#
九、查找命令:find
[root@test ~]# find / -name wxli
/root/wxli
[root@test ~]
十、创建用户
[root@localhost ~]# useradd lt #创建lt用户
[root@localhost ~]# passwd lt #给lt用户建立密码
Changing password for user lt.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]#
方法2: “--stdin”为passwd的参数
[root@localhost ~]# echo "zaq1.;[="| passwd --stdin lt
Changing password for user lt.
passwd: all authentication tokens updated successfully.
[root@localhost ~]#
[root@localhost ~]# useradd libin -u 560 #创建libin用户加固定ID:560
[root@localhost ~]# tail -1 /etc/group #查看用户
libin:x:560:
[root@localhost ~]#
原文:http://blog.51cto.com/13816321/2146850