标签:sed替换 命令 set unset 切换 lob 3.3 global 如何
1.1 10 已知/tmp下已经存在test.txt文件,如何执行命令才能把/mnt/test.txt拷贝到/tmp下覆盖掉/tmp/test.txt,而让系统不提示是否覆盖(root权限下)。#环境准备
[root@oldboy ~]# touch /mnt/test.txt /tmp/test.txt
[root@oldboy ~]# ls -l /mnt/test.txt /tmp/test.txt
-rw-r--r--. 1 root root 0 Jul 14 01:53 /mnt/test.txt
-rw-r--r--. 1 root root 0 Jul 14 01:53 /tmp/test.txt
[root@oldboy ~]# cp /mnt/test.txt /tmp/
cp: overwrite `/tmp/test.txt'?(询问是否覆盖)
[root@oldboyedu50 ~]# \cp /mnt/test.txt /tmp/(\作用为不提示,直接覆盖)
[root@oldboyedu50 ~]# which cp #命令的绝对路径(全路径)
alias cp='cp -i'
/bin/cp
[root@oldboyedu50 ~]# cp /mnt/test.txt /tmp/
cp: overwrite `/tmp/test.txt'? ^C
[root@oldboyedu50 ~]# /bin/cp /mnt/test.txt /tmp/
#cp overwrite
#rm 是否删除
#mv 是否覆盖文件
cp === cp -i
rm === rm -i
mv === mv -i
给命令起了一个外号
#安全
#省事
alias san='cp -i'
alias zhang='mv -i'
alias wang='rm -i'
alias wang='rm -i'
alias 别名='命令'
给rm命令设置别名
目标:执行rm 屏幕显示rm command bny.
[root@oldboyedu50 ~]# echo rm command bny
rm command bny
[root@oldboyedu50 ~]# alias rm='echo rm bny'
[root@oldboyedu50 ~]# alias rm
alias rm='echo rm bny'
[root@oldboyedu50 ~]# rm /tmp/oldboy.txt
rm bny /tmp/oldboy.txt
修改文件 /etc/profile
[root@oldboyedu50 ~]# tail -5 /etc/profile
done
unset i
unset -f pathmunge
alias rm='echo rm bny'
生效
[root@oldboyedu50 ~]# source /etc/profile
[root@oldboyedu50 ~]# alias rm
alias rm='echo rm bny'
[root@oldboyedu50 ~]# vim /root/.bashrc
# .bashrc
# User specific aliases and functions
# alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@oldboyedu50 ~]# alias rm
alias rm='echo rm bny'
alias rm='echo rm bny'
rm /tmp/oldboy.txt
vim /etc/profile
source /etc/profile
/root/.bashrc
输入net 显示/etc/sysconfig/network-scripts/ifcfg-eth0文件的内容
完成后发出
1.alias net
2./etc/profile最后5行
[root@oldboyedu50 ~]# alias net='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
[root@oldboyedu50 ~]# net
DEVICE=eth0
TYPE=Ethernet
UUID=8fdc5e19-5b35-49fa-b63e-1629a63af1f0
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
HWADDR=00:0C:29:59:D4:13
IPADDR=10.0.0.200
PREFIX=24
GATEWAY=10.0.0.254
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
vim /etc/profile #编辑文件写入最后一行
[root@oldboyedu50 ~]# source /etc/profile
[root@oldboyedu50 ~]# alias net
alias net='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
vim 快捷键
G 到达文件的最后一行
gg 到达文件的第1行
10gg 到达文件的第10行
o(小写字母O) 当前行下面插入一个空行并进入编辑模式
u 撤销
:q!
[root@oldboyedu50 ~]# rm oldboy.txt
rm bny oldboy.txt
[root@oldboyedu50 ~]# \rm oldboy.txt
[root@oldboyedu50 ~]# ll oldboy.txt
ls: cannot access oldboy.txt: No such file or directory
[root@oldboyedu50 ~]# alias ll
alias ll='ls -l --color=auto'
实例3-1 ##13 把/oldboy目录及其子目录下所有以扩展名 .sh结尾的文件中,文件包含oldboy的字符串全部替换为oldgirl
mkdir -p /oldboy/test
cd /oldboy
echo "oldboy">test/del.sh
echo "oldboy">test.sh
echo "oldboy">t.sh
touch oldboy.txt
touch alex.txt
实例3-2 ##第1个里程碑-find
[root@oldboyedu50 oldboy]# find /oldboy/ -type f -name "*.sh"
/oldboy/test/del.sh
/oldboy/test.sh
/oldboy/t.sh
实例3-3 ##第2个里程碑-替换一个文件中的内容
[root@oldboyedu50 oldboy]# cat t.sh
oldboy
[root@oldboyedu50 oldboy]# sed 's#oldboy#oldgirl#g' t.sh
oldgirl
[root@oldboyedu50 oldboy]# sed 's#oldboy#oldgirl#g' t.sh
oldgirl
[root@oldboyedu50 oldboy]# cat t.sh
oldboy
#sed 修改文件的内容 把文件中的oldboy替换为oldgirl
[root@oldboyedu50 oldboy]# sed -i 's#oldboy#oldgirl#g' t.sh
[root@oldboyedu50 oldboy]# cat t.sh
oldgirl
实例3-4 #第3个里程碑-把find命令找出的文件传递给 sed命令
[root@oldboyedu50 oldboy]# find /oldboy/ -type f -name "*.sh"
/oldboy/test/del.sh
/oldboy/test.sh
/oldboy/t.sh
[root@oldboyedu50 oldboy]# find /oldboy/ -type f -name "*.sh"|xargs sed 's#oldboy#oldgirl#g'
oldgirl
oldgirl
oldgirl
[root@oldboyedu50 oldboy]# find /oldboy/ -type f -name "*.sh"|xargs sed -i 's#oldboy#oldgirl#g'
[root@oldboyedu50 oldboy]# find /oldboy/ -type f -name "*.sh"|xargs cat
oldgirl
oldgirl
oldgirl
小结:
1.别名
2.sed替换
find+sed
[root@oldboyedu50 oldboy]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@oldboyedu50 oldboy]# uname -r
2.6.32-696.el6.x86_64
[root@oldboyedu50 oldboy]# uname -m
x86_64
[root@oldboyedu50 oldboy]# useradd oldboy
[root@oldboyedu50 oldboy]# passwd oldboy
Changing password for user oldboy.
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
su - oldboy
ctrl + d 退出当前用户
标签:sed替换 命令 set unset 切换 lob 3.3 global 如何
原文地址:http://blog.51cto.com/13859679/2146084