1. 程式人生 > >linux學習筆記-常見問題實戰(1)

linux學習筆記-常見問題實戰(1)

現在我們幾個在工作或在面試中可能會遇到的一些linux基礎問題
1 定義一個對所有使用者都生效的命令別名:
設定別名的命令 alias
[[email protected] ~]# alias cls='clear'
如上設定只是區域性有效,當系統重啟動或切換使用者時及失效。要想對所有使用者永久有效需要修改相關配置檔案。
/etc/profile 新增 alias cls='clear'
[[email protected] ~]# source /etc/profile
對當前使用者永久有效
echo " alias cls='clear'" >> ~/.bashrc
source ~/.bashrc
2 顯示/etc/passwd不以/bin/bash結尾的行
[

[email protected] ~]# cat /etc/passwd|grep -v "/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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
3 顯示/etc/passwd中包含兩位數字或三為數字的行
[
[email protected]
~]# cat /etc/passwd|grep -E "[0-9]{2,3}"
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
4 顯示/proc/meminfo以大寫或小寫s開發的行
[
[email protected]
~]# cat /proc/meminfo|grep -i "^s"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 6820 kB
Slab: 51728 kB
SReclaimable: 17256 kB
[[email protected] ~]# cat /proc/meminfo|grep "^[Ss]"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 6820 kB
Slab: 51736 kB
SReclaimable: 17256 kB
SUnreclaim: 34480 kB

SUnreclaim: 34472 kB
[[email protected] ~]# cat /proc/meminfo|grep -v "^[^Ss]"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 6820 kB
Slab: 51780 kB
SReclaimable: 17288 kB
SUnreclaim: 34492 kB
5使用echo輸出一個絕對路徑 ,使用egrep取其基名
[[email protected] ~]# echo "/etc/passwd"|egrep -o "[^/]+/?$"
passwd
進一步使用egrep取其路徑名:
6 找出ip addr中的ip地址 要求結果只顯示ip
[[email protected] ~]# ip addr|grep "inet\>" | tr -s " "|cut -d " " -f5
host
192.168.3.255
[[email protected] ~]#
7 vim定製自動縮排4個字元
編輯 .vimrc檔案 新增以下內容
set tabstop=4
set softtabstop=4
set shiftwidth=4

8 編寫腳步,實現自動新增3個使用者,並計算三個使用者的uid之和
<待更新>
9 find的常用用法
查收 /usr下 不屬於 root bin hadoop 的檔案
find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls
215376 0 drwx------ 2 polkitd root 6 6月 10 2014 /usr/share/polkit-1/rules.d
查收最近7天修改的檔案
find /etc/ -mtime -7
find /etc/ -not -perm +222 -ls
查無宿組或宿主的檔案
localhost ~]# find /etc/ ( -nouser -o -nogroup ) -ls
134905616 0 -rw-r--r-- 1 1004 1004