一個可以列出檔案、目錄、可執行檔案和連結的簡單指令碼。
你是否曾經想列出目錄中的所有檔案,但僅列出檔案,而不列出其它的。僅列出目錄呢?如果有這種需求的話,那麼下面的指令碼可能正是你一直在尋找的,它在 GPLv3 下開源。
當然,你可以使用 find
命令:
find . -maxdepth 1 -type f -print
但這鍵入起來很麻煩,輸出也不友好,並且缺少 ls
命令擁有的一些改進。你還可以結合使用 ls
和 grep
來達到相同的結果:
ls -F . | grep -v /
但是,這又有點笨拙。下面這個指令碼提供了一種簡單的替代方法。
該指令碼提供了四個主要功能,具體取決於你呼叫它的名稱:lsf
列出檔案,lsd
列出目錄,lsx
列出可執行檔案以及 lsl
列出連結。
通過符號連結無需安裝該指令碼的多個副本。這樣可以節省空間並使指令碼更新更容易。
該指令碼通過使用 find
命令進行搜尋,然後在找到的每個專案上執行 ls
。這樣做的好處是,任何給指令碼的引數都將傳遞給 ls
命令。因此,例如,這可以列出所有檔案,甚至包括以點開頭的檔案:
lsf -a
要以長格式列出目錄,請使用 lsd
命令:
lsd -l
你可以提供多個引數,以及檔案和目錄路徑。
下面提供了當前目錄的父目錄和 /usr/bin
目錄中所有檔案的長分類列表:
lsf -F -l .. /usr/bin
目前該指令碼不處理遞回,僅列出當前目錄中的檔案。
lsf -R
該指令碼不會深入子目錄,這個不足有一天可能會進行修復。
該指令碼採用自上而下的方式編寫,其初始化功能位於指令碼的開頭,而工作主體則接近結尾。指令碼中只有兩個真正重要的功能。函數 parse_args()
會仔細分析命令列,將選項與路徑名分開,並處理指令碼中的 ls
命令列選項中的特定選項。
list_things_in_dir()
函數以目錄名作為引數並在其上執行 find
命令。找到的每個專案都傳遞給 ls
命令進行顯示。
這是一個可以完成簡單功能的簡單指令碼。它節省了時間,並且在使用大型檔案系統時可能會非常有用。
#!/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 --ignoremain (){ 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+"$@"}