溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何使用Bash腳本列出文件、目錄、可執(zhí)行文件和鏈接

發(fā)布時(shí)間:2021-11-08 11:38:35 來(lái)源:億速云 閱讀:519 作者:小新 欄目:系統(tǒng)運(yùn)維

這篇文章主要為大家展示了“如何使用Bash腳本列出文件、目錄、可執(zhí)行文件和鏈接”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何使用Bash腳本列出文件、目錄、可執(zhí)行文件和鏈接”這篇文章吧。


一個(gè)可以列出文件、目錄、可執(zhí)行文件和鏈接的簡(jiǎn)單腳本。

你是否曾經(jīng)想列出目錄中的所有文件,但僅列出文件,而不列出其它的。僅列出目錄呢?如果有這種需求的話(huà),那么下面的腳本可能正是你一直在尋找的,它在 GPLv3 下開(kāi)源。

當(dāng)然,你可以使用 find 命令:

find . -maxdepth 1 -type f -print

但這鍵入起來(lái)很麻煩,輸出也不友好,并且缺少 ls 命令擁有的一些改進(jìn)。你還可以結(jié)合使用 lsgrep 來(lái)達(dá)到相同的結(jié)果:

ls -F . | grep -v /

但是,這又有點(diǎn)笨拙。下面這個(gè)腳本提供了一種簡(jiǎn)單的替代方法。

用法

該腳本提供了四個(gè)主要功能,具體取決于你調(diào)用它的名稱(chēng):lsf 列出文件,lsd 列出目錄,lsx 列出可執(zhí)行文件以及 lsl 列出鏈接。

通過(guò)符號(hào)鏈接無(wú)需安裝該腳本的多個(gè)副本。這樣可以節(jié)省空間并使腳本更新更容易。

該腳本通過(guò)使用 find 命令進(jìn)行搜索,然后在找到的每個(gè)項(xiàng)目上運(yùn)行 ls。這樣做的好處是,任何給腳本的參數(shù)都將傳遞給 ls 命令。因此,例如,這可以列出所有文件,甚至包括以點(diǎn)開(kāi)頭的文件:

lsf -a

要以長(zhǎng)格式列出目錄,請(qǐng)使用 lsd 命令:

lsd -l

你可以提供多個(gè)參數(shù),以及文件和目錄路徑。

下面提供了當(dāng)前目錄的父目錄和 /usr/bin 目錄中所有文件的長(zhǎng)分類(lèi)列表:

lsf -F -l .. /usr/bin

目前該腳本不處理遞歸,僅列出當(dāng)前目錄中的文件。

lsf -R

該腳本不會(huì)深入子目錄,這個(gè)不足有一天可能會(huì)進(jìn)行修復(fù)。

內(nèi)部

該腳本采用自上而下的方式編寫(xiě),其初始化功能位于腳本的開(kāi)頭,而工作主體則接近結(jié)尾。腳本中只有兩個(gè)真正重要的功能。函數(shù) parse_args() 會(huì)仔細(xì)分析命令行,將選項(xiàng)與路徑名分開(kāi),并處理腳本中的 ls 命令行選項(xiàng)中的特定選項(xiàng)。

list_things_in_dir() 函數(shù)以目錄名作為參數(shù)并在其上運(yùn)行 find 命令。找到的每個(gè)項(xiàng)目都傳遞給 ls 命令進(jìn)行顯示。

總結(jié)

這是一個(gè)可以完成簡(jiǎn)單功能的簡(jiǎn)單腳本。它節(jié)省了時(shí)間,并且在使用大型文件系統(tǒng)時(shí)可能會(huì)非常有用。

腳本

#!/bin/bash # Script to list:#      directories (if called "lsd")#      files       (if called "lsf")#      links       (if called "lsl")#  or  executables (if called "lsx")# but not any other type of filesystem object.# FIXME: add lsp   (list pipes)## Usage:#   <command_name> [switches valid for ls command] [dirname...]## Works with names that includes spaces and that start with a hyphen.## Created by Nick Clifton.# Version 1.4# Copyright (c) 2006, 2007 Red Hat.## This is free software; you can redistribute it and/or modify it# under the terms of the GNU General Public License as published# by the Free Software Foundation; either version 3, or (at your# option) any later version. # It is distributed in the hope that it will be useful, but# WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the# GNU General Public License for more details. # ToDo:#  Handle recursion, eg:  lsl -R#  Handle switches that take arguments, eg --block-size#  Handle --almost-all, --ignore-backups, --format and --ignore main (){  init   parse_args ${1+"$@"}   list_objects   exit 0} report (){  echo $prog": " ${1+"$@"}} fail (){  report " Internal error: " ${1+"$@"}  exit 1} # Initialise global variables.init (){  # Default to listing things in the current directory.  dirs[0]=".";   # num_dirs is the number of directories to be listed minus one.  # This is because we are indexing the dirs[] array from zero.  num_dirs=0;   # Default to ignoring things that start with a period.  no_dots=1   # Note - the global variables 'type' and 'opts' are initialised in  # parse_args function.} # Parse our command lineparse_args (){  local no_more_args   no_more_args=0 ;   prog=`basename $0` ;   # Decide if we are listing files or directories.  case $prog in    lsf | lsf.sh)      type=f      opts="";      ;;    lsd | lsd.sh)      type=d      # The -d switch to "ls" is presumed when listing directories.      opts="-d";      ;;    lsl | lsl.sh)      type=l      # Use -d to prevent the listed links from being followed.      opts="-d";      ;;    lsx | lsx.sh)      type=f      find_extras="-perm /111"      ;;        *)      fail "Unrecognised program name: '$prog', expected either 'lsd', 'lsf', 'lsl' or 'lsx'"      ;;  esac   # Locate any additional command line switches for ls and accumulate them.  # Likewise accumulate non-switches to the directories list.  while [ $# -gt 0 ]  do    case "$1" in      # FIXME: Handle switches that take arguments, eg --block-size      # FIXME: Properly handle --almost-all, --ignore-backups, --format      # FIXME:   and --ignore      # FIXME: Properly handle --recursive      -a | -A | --all | --almost-all)        no_dots=0;        ;;      --version)        report "version 1.2"        exit 0        ;;      --help)        case $type in          d) report "a version of 'ls' that lists only directories" ;;          l) report "a version of 'ls' that lists only links" ;;          f) if [ "x$find_extras" = "x" ] ; then               report "a version of 'ls' that lists only files" ;             else              report "a version of 'ls' that lists only executables";             fi ;;        esac        exit 0        ;;      --)        # A switch to say that all further items on the command line are        # arguments and not switches.        no_more_args=1 ;        ;;      -*)        if [ "x$no_more_args" = "x1" ] ;        then          dirs[$num_dirs]="$1";          let "num_dirs++"        else          # Check for a switch that just uses a single dash, not a double          # dash.  This could actually be multiple switches combined into          # one word, eg "lsd -alF".  In this case, scan for the -a switch.          # XXX: FIXME: The use of =~ requires bash v3.0+.          if [[ "x${1:1:1}" != "x-" && "x$1" =~ "x-.*a.*" ]] ;          then            no_dots=0;          fi          opts="$opts $1";        fi        ;;      *)        dirs[$num_dirs]="$1";        let "num_dirs++"        ;;    esac    shift  done   # Remember that we are counting from zero not one.  if [ $num_dirs -gt 0 ] ;  then    let "num_dirs--"  fi} list_things_in_dir (){  local dir   # Paranoia checks - the user should never encounter these.  if test "x$1" = "x" ;  then    fail "list_things_in_dir called without an argument"  fi   if test "x$2" != "x" ;  then    fail "list_things_in_dir called with too many arguments"  fi   # Use quotes when accessing $dir in order to preserve  # any spaces that might be in the directory name.  dir="${dirs[$1]}";   # Catch directory names that start with a dash - they  # confuse pushd.  if test "x${dir:0:1}" = "x-" ;  then    dir="./$dir"  fi   if [ -d "$dir" ]  then    if [ $num_dirs -gt 0 ]    then      echo "  $dir:"    fi     # Use pushd rather passing the directory name to find so that the    # names that find passes on to xargs do not have any paths prepended.    pushd "$dir" > /dev/null    if [ $no_dots -ne 0 ] ; then      find . -maxdepth 1 -type $type $find_extras -not -name ".*" -printf "%f\000" \        | xargs --null --no-run-if-empty ls $opts -- ;    else      find . -maxdepth 1 -type $type $find_extras -printf "%f\000" \        | xargs --null --no-run-if-empty ls $opts -- ;    fi    popd > /dev/null  else    report "directory '$dir' could not be found"  fi} list_objects (){  local i   i=0;  while [ $i -le $num_dirs ]  do    list_things_in_dir i    let "i++"  done} # Invoke mainmain ${1+"$@"}

以上是“如何使用Bash腳本列出文件、目錄、可執(zhí)行文件和鏈接”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI