[100]第一波命令及总结

打算陆陆续续的整理下linux常用命令

7,ls - list directory contents

-a, --all
-l  use a long listing format
-h
-d 只显示目录

ls -F #目录后append /

-t  (按照时间顺序排序)sort by modification time, newest first
-r, (按照时间顺序逆序排序,最新在上)reverse order while sorting

生产案例: 查找最新文件?
ls -ltr

-i  查看inode节点


## ctime
[root@n1 ~]# ls -l
total 16
-rw-------. 1 root root 1271 Dec 27 04:48 anaconda-ks.cfg

[root@n1 ~]# alias ll
alias ll='ls -l --color=auto'

8,cp

-a = -pbr

cp 1.txt{,.ori} == cp 1.txt 1.txt.ori

{ } A{b,c} ==>bash ==> AB AC

/etc/ssh/sshd_config{,ori} ==> bash ==> /etc/ssh/sshd_config /etc/ssh/sshd_config.ori

cp /etc/{ssh/sshd_config,dhcp/}

/etc/ssh/
/etc/dhcp/



案例: 如何做到不交互提示(为何有提示?因为cp有alias cp='cp -i'')
方法1:
cp a.log /tmp

方法2:
/bin/cp a.log /tmp

方法3:
unalias cp
cp a.log /tmp

9,mv/rm

多种姿势排除某个文件删除

/tmp/ 作为linux回收站

删除文件的正确姿势?
1,使用mv命令移动到/tmp(回收站),之后统一删除
2,cd 目的目录; find . -type f(d) -name ""|xargs rm #注:这里alias失效, alias只有在bash直接执行有效

[root@n1 test]# alias rm
alias rm='rm -i'

10,find

11,xargs

第一波预测试

1,将"inet 192.168.2.11  netmask 255.255.255.0  broadcast 192.168.2.255"输入maotai.txt

ifconfig eth0|sed -n '2p'> maotai.txt
2.过滤出
192.168.2.11 255.255.255.0 192.168.2.255

[root@n1 test]# awk -F '[ :]+' '{print $3,$5,$7}' maotai.txt
172.17.0.1 255.255.0.0 0.0.0.0
3.使用awk,取出/etc/passwd的10到20行,将结果的第三列重定向到p.txt

awk -F ':' 'NR>9 && NR<20 {print $3}' /etc/passwd  > p.txt
4.实现以下效果,并持久化生效
[root@n1 test]# rm -f p.txt
Do not use rm command

[root@n1 test]# alias rm='echo "Do not use rm command"'
放到/etc/bashrc
6.删除除了p.txt其他文件

find . -type f ! -name p.txt |xargs 
m -f
7.打印/etc/passwd的第2-5行.(不下于3种方法)(模拟环境用seq,  seq 开始 公差 结束)

head -5 /etc/passwd|tail -4

sed -n '2,5p' /etc/passwd

awk 'NR>1 && NR<6' /etc/passwd
8.将/etc/passwd的第1列和最后1列互换位置
如修改前: root:x:0:0:root:/root:/bin/bash
修改后: /bin/bash:root:x:0:0:root:/root


[root@n1 test]# awk -F ':' 'NR==1 {print $7":"$2":"$3":"$4":"$5":"$6":"$1}' /etc/passwd
/bin/bash:x:0:0:root:/root:root

[root@n1 test]# awk -F ':' '{print $7":"$2":"$3":"$4":"$5":"$6":"$1}' /etc/passwd
9,将/data目录下及其子目录下所有以扩展名为txt结尾的文件,文件内容中maotai的字符串全部替换为maomao

[其他方法参考](http://blog.51cto.com/oldboy/1750481)

- 不加xargs:显示的是文件名列表
[root@n1 test]# find . -type f -name "*".txt|cat
./p.txt
./dir1/2maotai.txt
./dir1/4maotai.txt
./dir1/5maotai.txt

- 加上xargs,显示各个文件内容
find . -type f -name "*.txt"|xargs cat
find . -type f -name "*.txt"|xargs -n1 cat #存在效率问题

cat 1.txt
cat 2.txt

cat 1.txt 2.txt

- 解题:
find . -type f -name "*".txt|xargs sed -i 's#maotai#maomao#g'
10,查找/data下所有7天以前的以log结尾的大于1M的文件,移动到tmp下

find . -type f -name "*.txt" -size +1M -mtime +7|xargs -i mv {} /tmp

11. 什么是linux运行级别,请描述linux运行级别的不同数字的含义?
cat /etc/inittab(centos6)
0 关机
1 单用户模式
2 多用户模式,无NFS
3 多用户模式
4 无用
5 X11
6 重启
12.请描述buffer和cache的区别?
播放器正在缓冲(buffer): 网速不对等情况下, 如下载,先写到内存,后写到磁盘.解决速度不对等问题.
qq的缓存(cache): cpu经常用到的数据,cpu嫌放内存太慢.
13.请说出你知道的下列字符在linux里代表的含义
~
-
.
..
|
>
>>
<   # xargs -n 2 < test.txt
<<
!
原文地址:https://www.cnblogs.com/iiiiher/p/8494447.html