天天看點

Linux系統(三):常用指令

實驗目的:掌握linux常用指令

需求描述:

練習以下指令

檢視及切換目錄:pwd、cd、ls、du

建立目錄和檔案:mkdir、touch、ln

複制、删除、移動目錄和檔案:mv、cp、rm

查找目錄和檔案:which、find

練習指令幫助資訊的擷取

使用man、--help選項檢視find指令的幫助資訊,比較異同

使用man工具檢視man本身的手冊頁資訊,并儲存為man.txt文本檔案

指令練習:

pwd /列出目前所在目錄

[root@chan ~]# pwd
/root      

cd /切換目錄

[root@chan /]# cd /tmp
[root@chan tmp]#       

ls /列出目前目錄的内容(-a:檢視隐藏檔案,-l:檢視詳細資訊)

#列出目錄内容
[root@chan /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
#列出隐藏檔案
[root@chan mnt]# ls -a
.  ..  .1.txt  1.txt  2.txt  3.txt  4.txt
#列出詳細資訊
[root@chan mnt]# ll
總用量 0
-rw-r--r-- 1 root root 0 6月  27 15:56 1.txt
-rw-r--r-- 1 root root 0 6月  27 15:56 2.txt
-rw-r--r-- 1 root root 0 6月  27 15:56 3.txt
-rw-r--r-- 1 root root 0 6月  27 15:56 4.txt
[root@chan mnt]# ls -l
總用量 0
-rw-r--r-- 1 root root 0 6月  27 15:56 1.txt
-rw-r--r-- 1 root root 0 6月  27 15:56 2.txt
-rw-r--r-- 1 root root 0 6月  27 15:56 3.txt
-rw-r--r-- 1 root root 0 6月  27 15:56 4.txt      

​du /統計目錄及檔案占用空間(-a:分别統計檔案占用空間,-h:智能調機關)

[root@chan mnt]# du 
4 .
[root@chan mnt]# du -a
0 ./2.txt
0 ./3.txt
0 ./4.txt
0 ./.1.txt
4 ./1.txt
4 .
[root@chan mnt]# du -h
4.0K  .
[root@chan mnt]# du -ah
0 ./2.txt
0 ./3.txt
0 ./4.txt
0 ./.1.txt
4.0K  ./1.txt
4.0K  .
[root@chan mnt]#       

mkdir /建立目錄 (-p:遞歸建立)

#普通建立
[root@chan mnt]# mkdir aaa
[root@chan mnt]# ls
1.txt  2.txt  3.txt  4.txt  aaa
#遞歸建立
[root@chan mnt]# mkdir -p  bbb/ccc/ddd
[root@chan mnt]# ls bbb/ccc
ddd      

touch /建立文本檔案

[root@chan mnt]# touch 1.txt
[root@chan mnt]# ls
1.txt  aaa  bbb      

建立連結文法:ln [-s]軟連結 源檔案或目錄 連結檔案或目标位置)(預設硬連結)ln /建立連結檔案

[root@chan mnt]# ln -s /etc/netconfig /mnt/netconfig
[root@chan mnt]# ll
總用量 0
drwxr-xr-x 2 root root  6 6月  27 16:07 aaa
drwxr-xr-x 3 root root 17 6月  27 16:07 bbb
lrwxrwxrwx 1 root root 14 6月  27 16:12 netconfig -> /etc/netconfig      

複制粘貼(-p:保持屬性不變,-r:遞歸複制)

#普通複制
[root@chan mnt]# cp 1.txt /1.txt
[root@chan mnt]# ls /
1.txt  boot  etc   lib    media  opt   root  sbin  sys  usr
bin    dev   home  lib64  mnt    proc  run   srv   tmp  var
#遞歸複制
[root@chan mnt]# cp -r  bbb /bbb
[root@chan mnt]# ls /
1.txt  bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
bbb    boot  etc  lib   media  opt  root  sbin  sys  usr      

移動檔案及目錄

[root@chan mnt]# mv 1.txt /
[root@chan mnt]# ls /
1.txt  bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
bbb    boot  etc  lib   media  opt  root  sbin  sys  usr      

删除檔案或目錄rm /删除檔案(-f:強制删除,-i:需确定,-r:遞歸删除)

[root@chan mnt]# rm -rf /1.txt
[root@chan mnt]# ls /
bbb  boot  etc   lib    media  opt   root  sbin  sys  usr
bin  dev   home  lib64  mnt    proc  run   srv   tmp  var      

查找指令檔案存放目錄

[root@chan mnt]# which ls
alias ls='ls --color=auto'
  /usr/bin/ls      

查找檔案或目錄所在位置(篩選目錄檔案)

[root@chan mnt]# find /mnt -name 1.txt
/mnt/1.txt
[root@chan mnt]#       

使用man、--help選項檢視find指令的幫助資訊,比較異同

[root@chan mnt]# find --help
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

預設路徑為目前目錄;預設表達式為 -print
表達式可能由下列成份組成:操作符、選項、測試表達式以及動作:

操作符 (優先級遞減;未做任何指定時預設使用 -and):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race

比較測試 (N 可以是 +N 或 -N 或 N): -amin N -anewer FILE -atime N -cmin N
      -cnewer 檔案 -ctime N -empty -false -fstype 類型 -gid N -group 名稱
      -ilname 比對模式 -iname 比對模式 -inum N -ipath 比對模式 -iregex 比對模式
      -links N -lname 比對模式 -mmin N -mtime N -name 比對模式 -newer 檔案
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context 文本


操作: -delete -print0 -printf 格式 -fprintf 檔案 格式 -print 
      -fprint0 檔案 -fprint 檔案 -ls -fls 檔案 -prune -quit
      -exec 指令 ; -exec 指令 {} + -ok 指令 ;
      -execdir 指令 ; -execdir 指令 {} + -okdir 指令 ;

通過 findutils 錯誤報告頁 http://savannah.gnu.org/ 報告錯誤及跟蹤修定過程。如果您無法浏覽網頁,請發電子郵件至 <[email protected]>。      
[root@chan mnt]# man find      
[root@chan mnt]# man man | col -b > a.txt