第1章 Linux优化
1.已知/tmp下已经存在test.txt文件,如何执行命令才能把/mnt/test.txt拷贝到/tmp下覆盖掉/tmp/test.txt,而让系统不提示是否覆盖(root权限下)。
[[email protected] ~]# cp /mnt/test.txt /tmp/
cp: overwrite `/tmp/test.txt‘?
复制的时候不提示是否覆盖
#方法1
[[email protected] ~]# \cp /mnt/test.txt /tmp/
#方法2 使用命令的绝对路径(全路径)
[[email protected] ~]# which cp #命令的绝对路径(全路径)
alias cp=‘cp -i‘
/bin/cp
[[email protected] ~]# cp /mnt/test.txt /tmp/
cp: overwrite `/tmp/test.txt‘? ^C
[[email protected] ~]# /bin/cp /mnt/test.txt /tmp/
#cp overwrite
#rm 是否删除
#mv 是否覆盖文件
cp === cp -i
rm === rm -i
mv === mv -i
系统的别名 给命令起了一个外号
#安全
#省事
1.1 显示系统中的别名
alias san=‘cp -i‘
alias zhang=‘mv -i‘
alias wang=‘rm -i‘
1.2 设置别名
alias wang=‘rm -i‘
alias 别名=‘命令‘
给rm命令设置别名
#第1个里程碑-配置rm别名-目标
目标:执行rm 屏幕显示rm command bny.
1.3 配置rm别名-命令
[[email protected] ~]# echo rm command bny
rm command bny
配置rm别名-临时生效
[[email protected] ~]# alias rm=‘echo rm bny‘
[[email protected] ~]# alias rm
alias rm=‘echo rm bny‘
[[email protected] ~]# rm /tmp/oldboy.txt
rm bny /tmp/oldboy.txt
1.4 配置rm别名-永久生效
修改文件 /etc/profile
[[email protected] ~]# tail -5 /etc/profile
done
unset i
unset -f pathmunge
alias rm=‘echo rm bny‘
生效
[[email protected] ~]# source /etc/profile
[[email protected] ~]# alias rm
alias rm=‘echo rm bny‘
1.5 巨坑
[[email protected] ~]# 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
1.6 重新登录 并检查
[[email protected] ~]# alias rm
alias rm=‘echo rm bny‘
如何配置别名:
1.命令行-临时生效 与检查
alias rm=‘echo rm bny‘
rm /tmp/oldboy.txt
2.修改文件-永久生效
vim /etc/profile
source /etc/profile
3.检查
4.坑-rm mv cp
/root/.bashrc
1.7 配置别名:
输入net 显示/etc/sysconfig/network-scripts/ifcfg-eth0文件的内容
1.8 完成后发出
1.alias net
2./etc/profile最后5行
1.命令行-临时生效 与检查
[[email protected] ~]# alias net=‘cat /etc/sysconfig/network-scripts/ifcfg-eth0‘
[[email protected] ~]# 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"
2.修改文件-永久生效
vim /etc/profile #编辑文件写入最后一行
[[email protected] ~]# source /etc/profile
[[email protected] ~]# alias net
alias net=‘cat /etc/sysconfig/network-scripts/ifcfg-eth0‘
3.检查
vim 快捷键
G 到达文件的最后一行
gg 到达文件的第1行
10gg 到达文件的第10行
o(小写字母O) 当前行下面插入一个空行并进入编辑模式
u 撤销
:q! 退出
[[email protected] ~]# rm oldboy.txt
rm bny oldboy.txt
[[email protected] ~]# \rm oldboy.txt
[[email protected] ~]# ll oldboy.txt
ls: cannot access oldboy.txt: No such file or directory
[[email protected] ~]# alias ll
alias ll=‘ls -l --color=auto‘
##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
##第1个里程碑-find
[[email protected] oldboy]# find /oldboy/ -type f -name "*.sh"
/oldboy/test/del.sh
/oldboy/test.sh
/oldboy/t.sh
##第2个里程碑-替换一个文件中的内容
[[email protected] oldboy]# cat t.sh
oldboy
[[email protected] oldboy]# sed ‘s#oldboy#oldgirl#g‘ t.sh
oldgirl
[[email protected] oldboy]# sed ‘s#oldboy#oldgirl#g‘ t.sh
oldgirl
[[email protected] oldboy]# cat t.sh
oldboy
#sed 修改文件的内容 把文件中的oldboy替换为oldgirl
[[email protected] oldboy]# sed -i ‘s#oldboy#oldgirl#g‘ t.sh
[[email protected]du50 oldboy]# cat t.sh
oldgirl
#第3个里程碑-把find命令找出的文件传递给 sed命令
[[email protected] oldboy]# find /oldboy/ -type f -name ".sh"
/oldboy/test/del.sh
/oldboy/test.sh
/oldboy/t.sh
[[email protected] oldboy]# find /oldboy/ -type f -name ".sh"|xargs sed ‘s#oldboy#oldgirl#g‘
oldgirl
oldgirl
oldgirl
[[email protected] oldboy]# find /oldboy/ -type f -name ".sh"|xargs sed -i ‘s#oldboy#oldgirl#g‘
[[email protected] oldboy]# find /oldboy/ -type f -name ".sh"|xargs cat
oldgirl
oldgirl
oldgirl
第2章 linux基础优化
2.1 显示系统的版本信息
[[email protected] oldboy]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[[email protected] oldboy]# uname -r
2.6.32-696.el6.x86_64
[[email protected] oldboy]# uname -m
x86_64
2.2 添加用户 设置密码 切换用户
1.添加用户
[[email protected] oldboy]# useradd oldboy
2.password 设置密码
[[email protected] 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.
3.切换用户
su - oldboy
ctrl + d 退出当前用户
原文地址:http://blog.51cto.com/13860447/2145072