自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

針對大型文件系統(tǒng)可以試試此Bash腳本

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

[[319660]]

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

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

當然,你可以使用 find 命令:

  1. find . -maxdepth 1 -type f -print

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

  1. ls -F . | grep -v /

但是,這又有點笨拙。下面這個腳本提供了一種簡單的替代方法。

用法

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

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

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

  1. lsf -a

要以長格式列出目錄,請使用 lsd 命令:

  1. lsd -l

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

下面提供了當前目錄的父目錄和 /usr/bin 目錄中所有文件的長分類列表:

  1. lsf -F -l .. /usr/bin

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

  1. lsf -R

該腳本不會深入子目錄,這個不足有一天可能會進行修復。

內(nèi)部

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

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

總結(jié)

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

腳本

  1. #!/bin/bash
  2.  
  3. # Script to list:
  4. # directories (if called "lsd")
  5. # files (if called "lsf")
  6. # links (if called "lsl")
  7. # or executables (if called "lsx")
  8. # but not any other type of filesystem object.
  9. # FIXME: add lsp (list pipes)
  10. #
  11. # Usage:
  12. # <command_name> [switches valid for ls command] [dirname...]
  13. #
  14. # Works with names that includes spaces and that start with a hyphen.
  15. #
  16. # Created by Nick Clifton.
  17. # Version 1.4
  18. # Copyright (c) 2006, 2007 Red Hat.
  19. #
  20. # This is free software; you can redistribute it and/or modify it
  21. # under the terms of the GNU General Public License as published
  22. # by the Free Software Foundation; either version 3, or (at your
  23. # option) any later version.
  24.  
  25. # It is distributed in the hope that it will be useful, but
  26. # WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU General Public License for more details.
  29.  
  30. # ToDo:
  31. # Handle recursion, eg: lsl -R
  32. # Handle switches that take arguments, eg --block-size
  33. # Handle --almost-all, --ignore-backups, --format and --ignore
  34.  
  35. main ()
  36. {
  37. init
  38. parse_args ${1+"$@"}
  39.  
  40. list_objects
  41.  
  42. exit 0
  43. }
  44.  
  45. report ()
  46. {
  47. echo $prog": " ${1+"$@"}
  48. }
  49.  
  50. fail ()
  51. {
  52. report " Internal error: " ${1+"$@"}
  53. exit 1
  54. }
  55.  
  56. # Initialise global variables.
  57. init ()
  58. {
  59. # Default to listing things in the current directory.
  60. dirs[0]=".";
  61. # num_dirs is the number of directories to be listed minus one.
  62. # This is because we are indexing the dirs[] array from zero.
  63. num_dirs=0;
  64. # Default to ignoring things that start with a period.
  65. no_dots=1
  66. # Note - the global variables 'type' and 'opts' are initialised in
  67. # parse_args function.
  68. }
  69.  
  70. # Parse our command line
  71. parse_args ()
  72. {
  73. local no_more_args
  74.  
  75. no_more_args=0 ;
  76.  
  77. prog=`basename $0` ;
  78.  
  79. # Decide if we are listing files or directories.
  80. case $prog in
  81. lsf | lsf.sh)
  82. type=f
  83. opts="";
  84. ;;
  85. lsd | lsd.sh)
  86. type=d
  87. # The -d switch to "ls" is presumed when listing directories.
  88. opts="-d";
  89. ;;
  90. lsl | lsl.sh)
  91. type=l
  92. # Use -d to prevent the listed links from being followed.
  93. opts="-d";
  94. ;;
  95. lsx | lsx.sh)
  96. type=f
  97. find_extras="-perm /111"
  98. ;;
  99. *)
  100. fail "Unrecognised program name: '$prog', expected either 'lsd', 'lsf', 'lsl' or 'lsx'"
  101. ;;
  102. esac
  103.  
  104. # Locate any additional command line switches for ls and accumulate them.
  105. # Likewise accumulate non-switches to the directories list.
  106. while [ $# -gt 0 ]
  107. do
  108. case "$1" in
  109. # FIXME: Handle switches that take arguments, eg --block-size
  110. # FIXME: Properly handle --almost-all, --ignore-backups, --format
  111. # FIXME: and --ignore
  112. # FIXME: Properly handle --recursive
  113. -a | -A | --all | --almost-all)
  114. no_dots=0;
  115. ;;
  116. --version)
  117. report "version 1.2"
  118. exit 0
  119. ;;
  120. --help)
  121. case $type in
  122. d) report "a version of 'ls' that lists only directories" ;;
  123. l) report "a version of 'ls' that lists only links" ;;
  124. f) if [ "x$find_extras" = "x" ] ; then
  125. report "a version of 'ls' that lists only files" ;
  126. else
  127. report "a version of 'ls' that lists only executables";
  128. fi ;;
  129. esac
  130. exit 0
  131. ;;
  132. --)
  133. # A switch to say that all further items on the command line are
  134. # arguments and not switches.
  135. no_more_args=1 ;
  136. ;;
  137. -*)
  138. if [ "x$no_more_args" = "x1" ] ;
  139. then
  140. dirs[$num_dirs]="$1";
  141. let "num_dirs++"
  142. else
  143. # Check for a switch that just uses a single dash, not a double
  144. # dash. This could actually be multiple switches combined into
  145. # one word, eg "lsd -alF". In this case, scan for the -a switch.
  146. # XXX: FIXME: The use of =~ requires bash v3.0+.
  147. if [[ "x${1:1:1}" != "x-" && "x$1" =~ "x-.*a.*" ]] ;
  148. then
  149. no_dots=0;
  150. fi
  151. opts="$opts $1";
  152. fi
  153. ;;
  154. *)
  155. dirs[$num_dirs]="$1";
  156. let "num_dirs++"
  157. ;;
  158. esac
  159. shift
  160. done
  161.  
  162. # Remember that we are counting from zero not one.
  163. if [ $num_dirs -gt 0 ] ;
  164. then
  165. let "num_dirs--"
  166. fi
  167. }
  168.  
  169. list_things_in_dir ()
  170. {
  171. local dir
  172.  
  173. # Paranoia checks - the user should never encounter these.
  174. if test "x$1" = "x" ;
  175. then
  176. fail "list_things_in_dir called without an argument"
  177. fi
  178.  
  179. if test "x$2" != "x" ;
  180. then
  181. fail "list_things_in_dir called with too many arguments"
  182. fi
  183.  
  184. # Use quotes when accessing $dir in order to preserve
  185. # any spaces that might be in the directory name.
  186. dir="${dirs[$1]}";
  187.  
  188. # Catch directory names that start with a dash - they
  189. # confuse pushd.
  190. if test "x${dir:0:1}" = "x-" ;
  191. then
  192. dir="./$dir"
  193. fi
  194. if [ -d "$dir" ]
  195. then
  196. if [ $num_dirs -gt 0 ]
  197. then
  198. echo " $dir:"
  199. fi
  200.  
  201. # Use pushd rather passing the directory name to find so that the
  202. # names that find passes on to xargs do not have any paths prepended.
  203. pushd "$dir" > /dev/null
  204. if [ $no_dots -ne 0 ] ; then
  205. find . -maxdepth 1 -type $type $find_extras -not -name ".*" -printf "%f\000" \
  206. | xargs --null --no-run-if-empty ls $opts -- ;
  207. else
  208. find . -maxdepth 1 -type $type $find_extras -printf "%f\000" \
  209. | xargs --null --no-run-if-empty ls $opts -- ;
  210. fi
  211. popd > /dev/null
  212. else
  213. report "directory '$dir' could not be found"
  214. fi
  215. }
  216.  
  217. list_objects ()
  218. {
  219. local i
  220.  
  221. i=0;
  222. while [ $i -le $num_dirs ]
  223. do
  224. list_things_in_dir i
  225. let "i++"
  226. done
  227. }
  228.  
  229. # Invoke main
  230. main ${1+"$@"}

 

責任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2023-10-26 07:06:19

前端文件系統(tǒng)

2023-09-12 14:44:12

github前端

2020-07-22 14:53:06

Linux系統(tǒng)虛擬文件

2019-12-05 09:00:27

BashShellLinux

2011-01-13 14:10:30

Linux文件系統(tǒng)

2018-08-24 10:10:25

Linux文件系統(tǒng)技術(shù)

2019-09-20 10:04:45

Linux系統(tǒng)虛擬文件

2021-01-18 15:04:53

Linux內(nèi)核開發(fā)者刪除

2021-05-31 06:10:14

Btrfs文件系統(tǒng)Linux

2021-05-31 07:50:59

Linux文件系統(tǒng)

2013-05-27 14:46:06

文件系統(tǒng)分布式文件系統(tǒng)

2021-06-06 16:55:22

Linux文件系統(tǒng)

2021-04-12 05:44:44

Linux文件系統(tǒng)

2012-09-12 14:40:19

Lustre文件系統(tǒng)

2023-08-23 12:12:45

BashLinux

2010-04-29 10:11:17

Unix系統(tǒng)

2010-04-30 15:51:48

Unix系統(tǒng)

2011-03-23 14:00:44

2013-12-12 16:37:49

Shell腳本自動化部署MFS

2021-06-04 06:03:19

Python 3.6Python開發(fā)
點贊
收藏

51CTO技術(shù)棧公眾號