Linux常用命令-目录操作类

Linux 2024-08-17 37 次浏览 次点赞

常用命令

[root@fmujieserver bin]# ls -l /bin/ | grep sh  #列表形式展示    bin目录下所有带sh的项目
-rwxr-xr-x.   1 root root    964536 Apr  1  2020 bash
lrwxrwxrwx.   1 root root        10 Nov 17  2022 bashbug -> bashbug-64
-rwxr-xr-x.   1 root root      6964 Apr  1  2020 bashbug-64
-rws--x--x    1 root root     23880 Feb  3  2021 chsh
-rwxr-xr-x.   1 root root     11576 Aug  2  2017 fipshmac
-rwxr-xr-x.   1 root root      4629 Apr  1  2020 gettext.sh
-rwxr-xr-x    2 root root    735208 May 23  2023 git-shell
-rwxr-xr-x    1 root root     40929 Jun 10  2021 glib-genmarshal
-rwxr-xr-x    1 root root      4247 Jun 10  2014 instmodsh
lrwxrwxrwx    1 root root        53 Jun 25 21:57 jdcloudservice -> /usr/local/share/jcloud/jdcloudservice/jdcloudservice
-rwxr-xr-x.   1 root root     15864 Apr 13  2018 lchsh
-rwxr-xr-x.   1 root root      2291 Jul 31  2015 lesspipe.sh
-rwxr-xr-x    1 root root   3087504 Oct  2  2020 mysqlshow
lrwxrwxrwx.   1 root root        19 Nov 17  2022 setup-nsssysinit -> setup-nsssysinit.sh
-rwxr-xr-x.   1 root root      1539 Dec 11  2019 setup-nsssysinit.sh
lrwxrwxrwx.   1 root root         4 Nov 17  2022 sh -> bash
-rwxr-xr-x.   1 root root     37448 Aug 20  2019 sha1sum
-rwxr-xr-x.   1 root root     41608 Aug 20  2019 sha224sum
-rwxr-xr-x.   1 root root     41608 Aug 20  2019 sha256sum
-rwxr-xr-x.   1 root root     41624 Aug 20  2019 sha384sum
-rwxr-xr-x.   1 root root     41624 Aug 20  2019 sha512sum
-rwxr-xr-x.   1 root root     10371 May 13  2020 show-changed-rco
-rwxr-xr-x.   1 root root     15912 Oct 31  2018 showconsolefont
-rwxr-xr-x.   1 root root     16572 May 13  2020 show-installed
-rwxr-xr-x.   1 root root     15680 Oct 31  2018 showkey
-rwxr-xr-x    1 root root      7248 Aug 12  2017 showrgb
-rwxr-xr-x.   1 root root     54208 Aug 20  2019 shred
-rwxr-xr-x.   1 root root     50312 Aug 20  2019 shuf
lrwxrwxrwx.   1 root root         5 Nov 17  2022 slogin -> ./ssh
-rwxr-xr-x.   1 root root    774568 Aug  9  2019 ssh
-rwxr-xr-x.   1 root root    360920 Aug  9  2019 ssh-add
---x--s--x.   1 root nobody  382216 Aug  9  2019 ssh-agent
-rwxr-xr-x.   1 root root     10469 Aug  9  2019 ssh-copy-id
-rwxr-xr-x.   1 root root    419208 Aug  9  2019 ssh-keygen
-rwxr-xr-x.   1 root root    441024 Aug  9  2019 ssh-keyscan
lrwxrwxrwx    1 root root         8 Jun 25 22:29 tclsh -> tclsh8.5
-rwxr-xr-x    1 root root      7168 Nov 20  2015 tclsh8.5
-rwxr-xr-x    1 root root     15824 Feb  3  2021 unshare
lrwxrwxrwx    1 root root         7 Jun 25 22:29 wish -> wish8.5
-rwxr-xr-x    1 root root      7208 Nov 20  2015 wish8.5
-rwxr-xr-x    1 root root     11424 Aug 12  2017 xrefresh
-rwxr-xr-x.   1 root root    740480 Apr  7  2020 zsh

帮助命令

man获得帮助信息

man命令来自英文单词manual的缩写,中文译为“帮助手册”,其功能是查看命令、配置文件及服务的帮助信息。一般而言,网上搜索来的资料普遍不够准确,或者缺乏系统性,导致学习进度缓慢,而man命令作为权威的官方工具,很好地解决了上述两点弊病。一份完整的帮助信息包含以下信息。

查看指定命令的帮助信息:

[root@fmujieserver ~]# man ifconfig
IFCONFIG(8)                                                  Linux System Administrator's Manual                                                 IFCONFIG(8)

NAME
       ifconfig - configure a network interface

SYNOPSIS
       ifconfig [-v] [-a] [-s] [interface]
       ifconfig [-v] interface [aftype] options | address ...

NOTE
       This program is obsolete!  For replacement check ip addr and ip link.  For statistics use ip -s link.

DESCRIPTION
       Ifconfig  is used to configure the kernel-resident network interfaces.  It is used at boot time to set up interfaces as necessary.  After that, it is
       usually only needed when debugging or when system tuning is needed.

       If no arguments are given, ifconfig displays the status of the currently active interfaces.  If a single interface argument is given, it displays the
       status of the given interface only; if a single -a argument is given, it displays the status of all interfaces, even those that are down.  Otherwise,
       it configures an interface.

Address Families
       If the first argument after the interface name is recognized as the name of a supported address family, that address family is used for decoding  and
       displaying  all protocol addresses.  Currently supported address families include inet (TCP/IP, default), inet6 (IPv6), ax25 (AMPR Packet Radio), ddp
       (Appletalk Phase 2), ipx (Novell IPX) and netrom (AMPR Packet radio).  All numbers supplied as parts in IPv4 dotted decimal notation may be  decimal,
       octal,  or  hexadecimal, as specified in the ISO C standard (that is, a leading 0x or 0X implies hexadecimal; otherwise, a leading '0' implies octal;
       otherwise, the number is interpreted as decimal). Use of hexadecimal and octal numbers is not RFC-compliant and therefore its use is discouraged.

OPTIONS
       -a     display all interfaces which are currently available, even if down

       -s     display a short list (like netstat -i)

       -v     be more verbose for some error conditions

    ……
[root@fmujieserver ~]# man cd
cd(n)                                                               Tcl Built-In Commands                                                              cd(n)

____________________________________________________________________________________________________________________________________________________________

NAME
       cd - Change working directory

SYNOPSIS
       cd ?dirName?
_________________________________________________________________

DESCRIPTION
       Change  the  current  working directory to dirName, or to the home directory (as specified in the HOME environment variable) if dirName is not given.
       Returns an empty string.  Note that the current working directory is a per-process resource; the cd command changes the  working  directory  for  all
       interpreters and (in a threaded environment) all threads.

EXAMPLES
       Change to the home directory of the user fred:
              cd ~fred

       Change to the directory lib that is a sibling directory of the current one:
              cd ../lib

SEE ALSO
       filename(n), glob(n), pwd(n)

KEYWORDS
       working directory

Tcl
[root@fmujieserver ~]# help cd  # 只能获得shell内置命令的帮助信息(简洁)
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.
    
    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
    
    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.
    
    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.
    
    Options:
        -L    force symbolic links to be followed
        -P    use the physical directory structure without following symbolic
        links
        -e    if the -P option is supplied, and the current working directory
        cannot be determined successfully, exit with a non-zero status
    
    The default is to follow symbolic links, as if `-L' were specified.
    
    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.
[root@fmujieserver ~]# help ls
-bash: help: no help topics match `ls'.  Try `help help' or `man -k ls' or `info ls'
[root@fmujieserver ~]# ls --help  # 外部命令帮助信息这样显示
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
……
[root@fmujieserver ~]# type cd  # 表示内嵌在bash内的命令
cd is a shell builtin
[root@fmujieserver ~]# type exit  # 表示内嵌在bash内的命令
exit is a shell builtin
[root@fmujieserver ~]# type history  # 表示内嵌在bash内的命令
history is a shell builtin
[root@fmujieserver ~]# type ls  # 外部命令
ls is aliased to `ls --color=auto'
[root@fmujieserver ~]# type useradd  # 外部命令
useradd is /usr/sbin/useradd

常用快捷键

ctrl + C:停止进程

ctrl + L:清屏,相当于clear;彻底清屏:reset

Tab:补全,很好用

↑/↓:查看执行过的命令

文件目录类

pwdprint working directory显示当前工作目录的绝对路径(从根目录)(相对/绝对自行理解)

cd /XXX/ 绝对路径,以/开头

cd XXX/ 相对路径,不以/开头

[root@fmujieserver ~]# pwd
/root
[root@fmujieserver ~]# cd /etc/sysconfig
[root@fmujieserver sysconfig]# pwd
/etc/sysconfig
[root@fmujieserver sysconfig]# type pwd
pwd is a shell builtin
[root@fmujieserver sysconfig]# help pwd
pwd: pwd [-LP]
    Print the name of the current working directory.
    
    Options:
      -L    print the value of $PWD if it names the current working
        directory
      -P    print the physical directory, without any symbolic links
    
    By default, `pwd' behaves as if `-L' were specified.
    
    Exit Status:
    Returns 0 unless an invalid option is given or the current directory
    cannot be read.

cd -:返回上次的文件夹,而不是返回上一级,返回上一级cd ../

# 比如说,我刚才从/root直接cd /etc/sysconfig,现在cd -回到/root
[root@fmujieserver sysconfig]# cd -  
/root
[root@fmujieserver ~]# 
# 再次cd -,就会反复横跳
[root@fmujieserver ~]# cd -
/etc/sysconfig
[root@fmujieserver sysconfig]# cd # 回到当前用户主目录
[root@fmujieserver ~]# 

lslist简写,列出目录内容

  • ls -a:全部的文件,包括隐藏文件(.是当前目录对象,..是上一级目录对象,所以能够在调用cd .cd ..,以.xxx开头的文件或者文件夹,则是隐藏文件;.bashrc:当前用户的配置文件(环境变量等……))

    • [root@fmujieserver ~]# ls -a
      .   anaconda-ks.cfg  .bash_logout   .bashrc  .cshrc  igbinary.sh  .lesshst  original-ks.cfg  .pearrc  .pki  .rnd      .ssh     .viminfo
  • ls -l:长数据串列出,包含文件的属性与权限等数据

    • -开头,表示文件
      d开头表示文件夹
      [root@fmujieserver ~]# ls -l
      total 128
      -rw-------. 1 root root   7353 Nov 17  2022 anaconda-ks.cfg
      drwxr-xr-x  2 root root   4096 Jun 30 10:56 frp
      -rw-r--r--  1 root root   5140 May 24 17:15 igbinary.sh
      -rw-r--r--  1 root root  37929 Jun 20 09:09 install.sh
      -rw-------. 1 root root   6652 Nov 17  2022 original-ks.cfg
      -rw-r--r--  1 www  wheel 53551 Feb 16  2022 package.xml
      drwxr-xr-x  2 root root   4096 Jun 29 13:11 rustdesk
      [root@fmujieserver ~]# ll  # ll是list -l的别名
      total 128
      -rw-------. 1 root root   7353 Nov 17  2022 anaconda-ks.cfg
      drwxr-xr-x  2 root root   4096 Jun 30 10:56 frp
      -rw-r--r--  1 root root   5140 May 24 17:15 igbinary.sh
      -rw-r--r--  1 root root  37929 Jun 20 09:09 install.sh
      -rw-------. 1 root root   6652 Nov 17  2022 original-ks.cfg
      -rw-r--r--  1 www  wheel 53551 Feb 16  2022 package.xml
      drwxr-xr-x  2 root root   4096 Jun 29 13:11 rustdesk
      [root@fmujieserver ~]# type ll

mkdir(make directory):创建一个新的目录(也可以空格分隔,创建多个)

[root@fmujieserver ~]# mkdir test
[root@fmujieserver ~]# ls
anaconda-ks.cfg  frp  igbinary.sh  install.sh  original-ks.cfg  package.xml  rustdesk  test
[root@fmujieserver ~]# mkdir test1 test2 test3
[root@fmujieserver ~]# ls
anaconda-ks.cfg  frp  igbinary.sh  install.sh  original-ks.cfg  package.xml  rustdesk  test test1  test2  test3
  • -p:该参数用于创建好几级目录

    • [root@fmujieserver ~]# mkdir -p test4/test5/test6
      [root@fmujieserver ~]# ls
      anaconda-ks.cfg  frp  igbinary.sh  install.sh  original-ks.cfg  package.xml  rustdesk  test  test1  test2  test3  test4
      [root@fmujieserver ~]# cd test4
      [root@fmujieserver test4]# ls
      test5
      [root@fmujieserver test4]# cd test5
      [root@fmujieserver test5]# ls
      test6

      rmdir(remove directory):删除一个空的目录

      [root@fmujieserver ~]# rmdir test test1 test2 test3
      [root@fmujieserver ~]# ls
      anaconda-ks.cfg  frp  igbinary.sh  install.sh  original-ks.cfg  package.xml  rustdesk  test4
      [root@fmujieserver ~]# rmdir test4
      rmdir: failed to remove ‘test4’: Directory not empty
      # 若想一次性删除嵌套目录
      [root@fmujieserver ~]# rmdir test4/test5/test6/
      [root@fmujieserver ~]# ls

本文由 fmujie 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论

召唤看板娘