Linux基础(二):Linux常用命令 1

 目录

 1.  pwd, cd, ls, mkdir, rmdir, cp, rm, mv

pwd 命令

  cd 切换目录

ls 列出文件信息

mkdir 创建目录

 rmdir 删除空目录

cp 复制文件目录

rm 删除文件目录

mv 移动或改名文件

2.sudo, echo, touch

sudo 临时管理员权限运行

echo 显示变量的值或文本

touch 创建一个空文件或修改文件的最后修改时间

3. cat, less, more, head, tail, ln

cat 文本文件的内容 

less 交互显示文件内容

 more 交互显示文件内容

head 显示文件的前 n 行

 tail 显示文件的末 n 行

 ln 链接


1.  pwd, cd, ls, mkdir, rmdir, cp, rm, mv

pwd 命令

    显示当前工作目录的绝对路径

farsight@ubuntu:~/test$ pwd
/home/farsight/test

  cd 切换目录

farsight@ubuntu:~/test$ cd           #    回到家目录
farsight@ubuntu:~$ cd ~              #  ~ 回到家目录
farsight@ubuntu:~/test$ cd -         #  - 切换回之前的目录
farsight@ubuntu:/usr/bin$ cd ..      # .. 切换到上级目录
farsight@ubuntu:/usr/local/lib/python3.6/dist-packages$ cd ../../../bin  # 切换上三级目录

~ 通配任何用户的家目录。
      如果是 farsight 用户则表示 /home/farsight
      如果是 root 用户则表示 /roo

ls 列出文件信息

     默认情况下不显示隐藏文件(以 . 开头的文件)。
    -a 所有文件(包含隐藏文件),包括 . 和 ..
    -A 所有文件(包含隐藏文件),但不包括 . 和 ..

     -i 显示文件的 inode 号,inode 号相同的文件是同一个文件。

     -l 长格式

 farsight@ubuntu:~$ ls -l
 drwxr-xr-x  2 farsight farsight 4096 6月  29 11:13 Desktop

    该文件的 7 个部分,每部分的意思:
   
    drwxrwxr-x 第一个字母表示文件类型。有:
              d 目录,- 常规文件,l 符号链接,b 块设备,c 字符设备,p 管道
              2-4 三个字母表示“所有者”权限,r 可读,w 可写,x 可执行,并且位置固定。- 表示无权
              5-7 三个字母表示“所属组”权限
              8-10 三个字符表示“其它用户”权限
   
   第二部分,如果是目录表示其下面的子目录个数(包含 . 和 ..),如果是文件则表示硬链接数目
   
   第三部分,所有者名称
   第四部分,所属组名称
   第五部分,文件大小,对于目录表示所占磁盘空间;对于文件表示文件大小
   第六部分,最后修改日期
   第七部分,文件名

mkdir 创建目录

farsight@ubuntu:~/test$ mkdir d1 d2 d3   # 同时创建 3 个目录 d1 d2 d3
farsight@ubuntu:~/test$ ls
1  11.txt  1.txt  2.txt  3.txt  d1  d2  d3

farsight@ubuntu:~/test$ mkdir d1/d11 d2/d21   # 创建子目录
farsight@ubuntu:~/test$ ls d1 d2
d1:
d11

d2:
d21

farsight@ubuntu:~/test$ mkdir d3/d31/d311     # 要创建 d311 子目录,但它的父目录 d31 不存在,因此报错
mkdir: cannot create directory ‘d3/d31/d311’: No such file or directory

farsight@ubuntu:~/test$ mkdir -p d3/d31/d311  # 使用 -p 选项可以创建路径中不存在的中间目录
farsight@ubuntu:~/test$ ls d3 -R
d3:
d31

d3/d31:
d311

d3/d31/d311:

 rmdir 删除空目录

farsight@ubuntu:~/test$ rmdir d1/d11 d2/d21   # 删除两个子目录

farsight@ubuntu:~/test$ rmdir d3              # 非空目录不能删除
rmdir: failed to remove 'd3': Directory not empty
farsight@ubuntu:~/test$ rmdir -p d3/d31/d311  # -p 选项删除路径中所有空目录
farsight@ubuntu:~/test$ ls
1  11.txt  1.txt  2.txt  3.txt  d1  d2

farsight@ubuntu:~/test$ rmdir d*              # 使用通配符删除以 d 开头的空目录
farsight@ubuntu:~/test$ ls
1  11.txt  1.txt  2.txt  3.txt

cp 复制文件目录

farsight@ubuntu:~/test$ cp /usr/include/stdio.h .    # 复制文件到当前目录(.)
farsight@ubuntu:~/test$ ls
1  11.txt  1.txt  2.txt  3.txt  stdio.h

farsight@ubuntu:~/test$ cp *.txt stdio.h d1   # 复制多个文件到 d1 目录,使用通配符 *
farsight@ubuntu:~/test$ ls d1
11.txt  1.txt  2.txt  3.txt  stdio.h

cp -t -r(-R)参数

farsight@ubuntu:~/test$ cp -t d2 1.txt 2.txt 3.txt    # -t 选项反转源和目标的顺序,即将右边的文件复制到左边的目录 d2 中
farsight@ubuntu:~/test$ ls d2
1.txt  2.txt  3.txt

farsight@ubuntu:~/test$ cp d1 d3              # 复制目录,没有使用 -r 选项会报错
cp: -r not specified; omitting directory 'd1'
farsight@ubuntu:~/test$ cp -r d1 d3           # 复制目录,要使用 -r 选项。注意目标目录 d3 此时不存在,则 d3 与 d1 的内容相同
farsight@ubuntu:~/test$ ls d3
11.txt  1.txt  2.txt  3.txt  stdio.h
farsight@ubuntu:~/test$ cp -r d1 d3           # 复制目录,此时 d3 已存在,则将 d1 复制到 d3 下面成为其子目录
farsight@ubuntu:~/test$ ls d3
11.txt  1.txt  2.txt  3.txt  d1  stdio.h

rm 删除文件目录

farsight@ubuntu:~/test$ rm 1 11.txt           # 删除多个文件
farsight@ubuntu:~/test$ ls
1.txt  2.txt  3.txt  d1  d2  d3  stdio.h

farsight@ubuntu:~/test$ rm *.txt              # 使用通配符
farsight@ubuntu:~/test$ ls
d1  d2  d3  stdio.h

rm -r -i -f 参数

farsight@ubuntu:~/test$ rm d1                 # 删除目录报错
rm: cannot remove 'd1': Is a directory
farsight@ubuntu:~/test$ rm -r d1              # 使用 -r 选项可以删除目录,递归进入子目录进行删除,删除目录时还是调用 rmdir 命令
farsight@ubuntu:~/test$ ls
d2  d3  stdio.h

farsight@ubuntu:~/test$ rm -i stdio.h         # -i 选项在每删除一个文件时都要询问,回答 y 删除,n 不删除
rm: remove regular file 'stdio.h'? n          # 回答 n 表示不删除
farsight@ubuntu:~/test$ ls
d2  d3  stdio.h
farsight@ubuntu:~/test$ rm -i stdio.h 
rm: remove regular file 'stdio.h'? y          # 回答 y 表示删除
farsight@ubuntu:~/test$ ls
d2  d3

farsight@ubuntu:~/test$ rm -rf d2             # -f 选项表示不询问,直接删除
farsight@ubuntu:~/test$ ls
d3

mv 移动或改名文件

farsight@ubuntu:~/test$ mv d3 d1     # 将目录 d3 改名为 d1,因为 d1 不存在
farsight@ubuntu:~/test$ ls
d1

farsight@ubuntu:~/test$ mv d1 d2     # d2 是已存在的,则将 d1 移动到 d2 下面
farsight@ubuntu:~/test$ ls
d2
farsight@ubuntu:~/test$ ls d2
d1

farsight@ubuntu:~/test$ mv *.txt d2  # 移动多个文件到目录

2.sudo, echo, touch

sudo 临时管理员权限运行

echo 显示变量的值或文本

farsight@ubuntu:~/test$ echo $PATH   # 显示环境变量 PATH 的值,注意引用变量时要在前面使用 $ 符号,但对变量赋值时不能使用 $
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

farsight@ubuntu:~/test$ echo PATH=$PATH   # 显示字符串 PATH= 及变量 PATH 的值
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

farsight@ubuntu:~/test$ echo 请输入一个整数:; read MYNUM   # echo 命令显示提示文本,read 命令读取输入并保存到 MYNUM 自定义变量中
请输入一个整数:
12
farsight@ubuntu:~/test$ echo MYNUM=$MYNUM
MYNUM=12

farsight@ubuntu:~/test$ echo -n 请输入一个整数:; read MYNUM  # 默认输出字符串后换行,-n 选项不换行
请输入一个整数:34

farsight@ubuntu:~/test$ echo "Hello\nworld"      # 转义字符默认不生效
Hello\nworld
farsight@ubuntu:~/test$ echo -e "Hello\nworld"   # 使用 -e 选项启用转义
Hello
world

使用 unset 命令(bash 的 built-in 内置命令)可以删除变量和函数

farsight@ubuntu:~/test$ read MYNUM <<< 78; echo MYNUM=$MYNUM   # 将 78 输入给 MYNUM 变量
MYNUM=78

touch 创建一个空文件或修改文件的最后修改时间

farsight@ubuntu:~/test$ touch 1.txt 2.txt 3.txt    # 创建三个空文件,它们原来不存在
farsight@ubuntu:~/test$ ls -l
total 36
-rw-rw-r-- 1 farsight farsight     0 7月  13 11:17 1.txt
-rw-rw-r-- 1 farsight farsight     0 7月  13 11:17 2.txt
-rw-rw-r-- 1 farsight farsight     0 7月  13 11:17 3.txt
drwxrwxr-x 3 farsight farsight  4096 7月  13 10:28 d2
-rw-r--r-- 1 farsight farsight 29665 7月  13 11:17 stdio.h

farsight@ubuntu:~/test$ touch stdio.h              # 因为 stdio.h 已存在,修改它的最后修改时间为当前时间
farsight@ubuntu:~/test$ ls -l stdio.h 
-rw-r--r-- 1 farsight farsight 29665 7月  13 11:18 stdio.h

在编程中,由于两个系统的时间存在时间差,比如在两个时区的系统之间可能存在时间差,从东部时区打包的文件到西部时区,
就有可能存在这种情况,即西部时区解包的文件,是在未来的某个时间创建的,这显然不符合逻辑。
要想纠正这种“时间错”,可以使用 touch * 来修改所有文件的最后修改时间。

3. cat, less, more, head, tail, ln

cat 文本文件的内容 

 一般用于显示行数不多的文件内容。显示完毕返回命令行,不会停留在文件中。

farsight@ubuntu:~/test$ cat /etc/passwd    # 显示密码文件的内容

farsight@ubuntu:~/test$ cat -n /etc/passwd   # -n 显示行号

farsight@ubuntu:~/test$ cat -ns stdio.h    # -s 压缩连续的多行为一行


less 交互显示文件内容

    可以上下翻页,上下逐行滚动,查找。不想以编辑的方式查看文件内容时,可以使用 less 命令。

farsight@ubuntu:~/test$ less stdio.h    # 查看 stdio.h 的内容

    上下箭头可以上下滚动一行,回车向下滚动一行,PageUp 向上翻页,PageDown 向下翻页,
    /word 查找 word,并高亮显示所有的 word,输入 n 向下查找下一个 word,输出 N 向上查找
    ?word 查找 word,并高亮显示所有的 word,输入 N 向下查找下一个 word,输出 n 向上查找
    q 退出

 more 交互显示文件内容

    回车向下翻一行,空格向下翻一页,q 退出,显示当前所处位置的百分比

farsight@ubuntu:~/test$ more stdio.h 

head 显示文件的前 n 行

farsight@ubuntu:~/test$ head stdio.h        # 默认显示文件的前 10 行

farsight@ubuntu:~/test$ head -20 stdio.h    # -20 指定显示文件的前 20 行

farsight@ubuntu:~/test$ head -c 20 stdio.h  # -c 20 显示前 20 个字节

 tail 显示文件的末 n 行

farsight@ubuntu:~/test$ tail stdio.h        # 默认显示文件的末 10 行

farsight@ubuntu:~/test$ tail -20 stdio.h    # -20 指定显示文件的末 20 行

farsight@ubuntu:~/test$ tail -c 20 stdio.h  # -c 20 显示末 20 个字节


 ln 链接

    硬链接:  可以理解为一个文件取了多个名称。
              如果删除硬链接,只有当硬链接数为 0 时才从磁盘删除这个文件。
              限制:不能跨设备(文件系统)创建硬链接,不能对目录创建硬链接。
    符号链接:链接文件中保存了目标文件的路径。可以通过链接文件找到目标文件。
              删除链接文件,则仅将其自动删除,不会删除它所链接的目标文件。
              不存在硬链接的两个限制。

farsight@ubuntu:~/test$ ls -i           # 查看文件的 inode 号
135190 1.txt  135129 2.txt  135134 3.txt  134035 d2  135113 stdio.h
farsight@ubuntu:~/test$ ln stdio.h stdio-ln.h   # 创建硬链接
farsight@ubuntu:~/test$ ls -i
135190 1.txt  135129 2.txt  135134 3.txt  134035 d2  135113 stdio.h  135113 stdio-ln.h   # stdio.h 与 srdio-ln.h 的 inode 相同
farsight@ubuntu:~/test$ ls -l stdio*    # 硬链接数为 2
-rw-r--r-- 2 farsight farsight 29665 7月  13 11:18 stdio.h
-rw-r--r-- 2 farsight farsight 29665 7月  13 11:18 stdio-ln.h

farsight@ubuntu:~/test$ rm stdio-ln.h   # 删除其中一个硬链接,数目变为 1
farsight@ubuntu:~/test$ ls -l stdio*
-rw-r--r-- 1 farsight farsight 29675 7月  13 11:56 stdio.h

farsight@ubuntu:~/test$ ln d2 d2-ln     # 不能对目录创建硬链接
ln: d2: hard link not allowed for directory

farsight@ubuntu:~/test$ ln /dev/pts/0 pts0    # /dev 是另一个文件系统,不可以创建到当前文件系统的硬链接
ln: failed to create hard link 'pts0' => '/dev/pts/0': Invalid cross-device link

farsight@ubuntu:~/test$ ln -s stdio.h stdio-sln.h   # -s 选项创建符号链接
farsight@ubuntu:~/test$ ls -l stdio*
-rw-r--r-- 1 farsight farsight 29675 7月  13 11:56 stdio.h
lrwxrwxrwx 1 farsight farsight     7 7月  13 14:07 stdio-sln.h -> stdio.h   # stdio-sln.h 链接文件保存了目标文件的路径

farsight@ubuntu:~/test$ ln -s d2 d2-ln  # 为目录创建符号链接
farsight@ubuntu:~/test$ ls -ld d*
drwxrwxr-x 3 farsight farsight 4096 7月  13 10:28 d2
lrwxrwxrwx 1 farsight farsight    2 7月  13 14:11 d2-ln -> d2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值