天天看点

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