Linux---shell基本指令

1. 显示当前目录 pwd

wangzhengchao@ubuntu:~$ cd /home/wangzhengchao/Desktop/
wangzhengchao@ubuntu:~/Desktop$ pwd
/home/wangzhengchao/Desktop

 2. 改变目录 cd

wangzhengchao@ubuntu:~$ pwd
/home/wangzhengchao
wangzhengchao@ubuntu:~$ cd Downloads/usr
wangzhengchao@ubuntu:~/Downloads/usr$ pwd
/home/wangzhengchao/Downloads/usr
wangzhengchao@ubuntu:~/Downloads/usr$ cd ..
wangzhengchao@ubuntu:~/Downloads$ pwd
/home/wangzhengchao/Downloads
wangzhengchao@ubuntu:~/Downloads$ cd
wangzhengchao@ubuntu:~$ pwd
/home/wangzhengchao

注意:

  • 当登录系统后,总是处于当前用户主目录中 /home/wangzhengchao
  • ..代表当前目录到上一级目录
  • . 代表当前目录
  • ~代表用户主目录
  • 可以使用命令cd ../..直接进入根目录

3. 列出目录内容 ls

ls基本语法 ls [option] [file]

-a 显示所有文件,包括隐藏文件
-l 显示文件到各种属性
wangzhengchao@ubuntu:~$ pwd
/home/wangzhengchao
wangzhengchao@ubuntu:~$ ls
Desktop    Downloads         Music     Public     Videos
Documents  examples.desktop  Pictures  Templates
wangzhengchao@ubuntu:~$ ls -l
总用量 44
drwxr-xr-x 2 wangzhengchao wangzhengchao 4096 9月  25 14:29 Desktop
drwxr-xr-x 5 wangzhengchao wangzhengchao 4096 9月  26 21:14 Documents
drwxr-xr-x 6 wangzhengchao wangzhengchao 4096 9月  26 21:40 Downloads
-rw-r--r-- 1 wangzhengchao wangzhengchao 8980 9月  23 02:55 examples.desktop
drwxr-xr-x 2 wangzhengchao wangzhengchao 4096 9月  23 09:32 Music
drwxr-xr-x 2 wangzhengchao wangzhengchao 4096 9月  23 09:32 Pictures
drwxr-xr-x 2 wangzhengchao wangzhengchao 4096 9月  23 09:32 Public
drwxr-xr-x 2 wangzhengchao wangzhengchao 4096 9月  23 09:32 Templates
drwxr-xr-x 2 wangzhengchao wangzhengchao 4096 9月  23 09:32 Videos

ls-l 显示的属性包括:

  • 文件权限标志
  • 文件的链接个数
  • 文件所有者的用户名
  • 该用户所在到用户组名
  • 文件大小
  • 最后一次修改到日期
  • 最后一次修改到时间
  • 文件名

 4. 列出目录内容 dir / vdir

wangzhengchao@ubuntu:~$ pwd
/home/wangzhengchao
wangzhengchao@ubuntu:~$ dir Downloads/
employees_db                navicat121_mysql_en_x64.tar.gz
employees_db-full-1.0.6.tar.bz2        navicat.desktop
flash_player_npapi_linux.x86_64.tar.gz    navicat.png
LGPL                    readme.txt
libflashplayer.so            sogoupinyin_2.2.0.0108_amd64.deb
license.pdf                sublime_text_3_build_3176_x64.tar.bz2
navicat121_mysql_en_x64            usr
wangzhengchao@ubuntu:~$ vdir Downloads/
总用量 173856
drwxrwxr-x 2 wangzhengchao wangzhengchao     4096 9月  25 15:03 employees_db
-rw-rw-r-- 1 wangzhengchao wangzhengchao 26715056 3月  30  2009 employees_db-full-1.0.6.tar.bz2
-rw-rw-r-- 1 wangzhengchao wangzhengchao  9047129 9月  26 21:35 flash_player_npapi_linux.x86_64.tar.gz
drwxrwxr-x 2 wangzhengchao wangzhengchao     4096 8月  28 03:59 LGPL
-rw-rw-r-- 1 wangzhengchao wangzhengchao 16603112 8月  28 03:59 libflashplayer.so
-rw-rw-r-- 1 wangzhengchao wangzhengchao  2792147 8月  28 03:59 license.pdf
drwxr-xr-x 4 root          root              4096 9月  23 22:47 navicat121_mysql_en_x64
-rw-rw-r-- 1 wangzhengchao wangzhengchao 89593252 9月  23 22:19 navicat121_mysql_en_x64.tar.gz
-rwxrwxr-x 1 wangzhengchao wangzhengchao      320 9月  25 14:14 navicat.desktop
-rw-rw-r-- 1 wangzhengchao wangzhengchao     5399 9月  25 14:06 navicat.png
-rw-rw-r-- 1 wangzhengchao wangzhengchao     3617 8月  28 03:59 readme.txt
-rw-rw-r-- 1 wangzhengchao wangzhengchao 22915896 9月  26 21:32 sogoupinyin_2.2.0.0108_amd64.deb
-rw-rw-r-- 1 wangzhengchao wangzhengchao 10314226 9月  25 21:03 sublime_text_3_build_3176_x64.tar.bz2
drwxrwxr-x 6 wangzhengchao wangzhengchao     4096 8月  28 03:59 usr

dir 用于列出目录内容,vdir用于列出目录内容的详细信息 相当于ls -l

5. 查看文本文件  cat / more

wangzhengchao@ubuntu:~/Downloads$ cat test
A
B
C
D
E
F
G
wangzhengchao@ubuntu:~/Downloads$ cat -n test
     1    A
     2    B
     3    C
     4    D
     5    E
     6    F
     7    G

 -n 显示行号,如果文本内容很长,使用cat指令会全部打印常出来,linu提供more指令一页一页地显示出来,空格向下翻一页,enter向下一行,Q退出

 6. 阅读文件的开头和结尾 head / tail

wangzhengchao@ubuntu:~/Downloads$ head -n 2 test
A
B
wangzhengchao@ubuntu:~/Downloads$ tail -n 2 test
F
G

 7. 查找文件内容 grep

一般格式: grep [option] pattern [file...] ,返回所有含有指定pattern的行,同时可以使用-n 命令选项显示所在行号。

wangzhengchao@ubuntu:~/Downloads$ grep Sort sort1026.cpp
void QSort(vector<int> &array, int low, int high){
        QSort(array, low, pivot-1);
        QSort(array, pivot+1, high);
void QuickSort(vector<int> &array){
    QSort(array, 0, array.size()-1);
void HeapSort(std::vector<int> &array){
    // QuickSort(array);
    HeapSort(array);
    // BubbleSort(array);

对于查找含有空格的字符,需要使用‘’包含进去,如下:

wangzhengchao@ubuntu:~/Downloads$ grep 'vector<int> &array' sort1026.cpp
int Partition(vector<int> &array, int low, int high){
void QSort(vector<int> &array, int low, int high){
void QuickSort(vector<int> &array){
void HeapAdjust(std::vector<int> &array, int start, int end){
void HeapSort(std::vector<int> &array){

8. 查找文件 find

基本语法: find [option] [path...] [expression] 

find命令是根据文件的属性进行查找,如文件名,文件大小,所有者,所属组,是否为空,访问时间,修改时间等。

wangzhengchao@ubuntu:~$ find Downloads/ -name *.cpp
Downloads/sort1026.cpp

上述指令表示在Downloads目录下查找文件名含有.cpp的文件。根据其他属性查找文件的方法与上述指令类似。

9. 定位文件 locate

wangzhengchao@ubuntu:~$ locate *正则化.pdf
/home/wangzhengchao/Documents/机器学习集锦/机器学习算法/机器学习算法系列(28):L1、L2正则化.pdf

 10. 用户及版本信息查看

wangzhengchao@ubuntu:~$ who
wangzhengchao tty7         2018-11-04 14:09 (:0)
wangzhengchao@ubuntu:~$ whoami
wangzhengchao
wangzhengchao@ubuntu:~$ uname
Linux
wangzhengchao@ubuntu:~$ uname -a
Linux ubuntu 4.15.0-29-generic #31~16.04.1-Ubuntu SMP Wed Jul 18 08:54:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
wangzhengchao@ubuntu:~$ uname -r
4.15.0-29-generic
  • who 显示当前系统有哪些人登录,以及对应的工作台
  • whoami 显示当前用户名称
  • uname 显示当前系统的版本信息  -a 显示详细信息  -r显示内核信息

11. 寻求帮助 man

$ man grep
GREP(1)                                                       General Commands Manual                                                       GREP(1)

NAME
       grep, egrep, fgrep, rgrep - print lines matching a pattern

SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN]...  [-f FILE]...  [FILE...]

DESCRIPTION
       grep  searches  the  named  input FILEs for lines containing a match to the given PATTERN.  If no files are specified, or if the file “-” is
       given, grep searches standard input.  By default, grep prints the matching lines.

       In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively.   These  variants  are
       deprecated, but are provided for backward compatibility.

OPTIONS
...

12. 获取命令简介 whatis

man到内容显得有些罗嗦,whatis指令帮助用户了解指令的大致用途

wangzhengchao@ubuntu:~$ whatis grep
grep (1)             - print lines matching a pattern
原文地址:https://www.cnblogs.com/iwangzhengchao/p/9904909.html