In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use the Bash script to list files, directories, executables and links", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Bash scripts to list files, directories, executables and links" this article.
A simple script that lists files, directories, executables, and links.
Have you ever wanted to list all the files in the directory, but only the files and nothing else? What about listing only directories? If there is such a need, then the following script may be exactly what you have been looking for, which is open source under GPLv3.
Of course, you can use the find command:
Find. -maxdepth 1-type f-print
But this is cumbersome to type, the output is not friendly, and lacks some of the improvements that the ls command has. You can also use ls in combination with grep to achieve the same result:
Ls-F. | | grep-v / |
But it's a little clumsy. The following script provides a simple alternative.
Usage
The script provides four main functions, depending on the name you call it: lsf lists files, lsd lists directories, lsx lists executables, and lsl lists links.
There is no need to install multiple copies of the script through a symbolic link. This saves space and makes it easier to update the script.
The script searches by using the find command, and then runs ls on each project found. The advantage of this is that any arguments given to the script will be passed to the ls command. So, for example, this can list all files, even those that start with a dot:
Lsf-a
To list directories in a long format, use the lsd command:
Lsd-l
You can provide multiple parameters, as well as file and directory paths.
The following provides a long classified list of all files in the parent directory and / usr/bin directory of the current directory:
Lsf-F-l.. / usr/bin
Currently, the script does not handle recursion and lists only the files in the current directory.
Lsf-R
The script does not go deep into the subdirectory, and this deficiency may be fixed one day.
Inside
The script is written in a top-down manner, with the initialization function at the beginning of the script and the working body nearing the end. There are only two really important functions in the script. The function parse_args () carefully analyzes the command line, separates the options from the path name, and handles specific options in the ls command-line options in the script.
The list_things_in_dir () function takes the directory name as an argument and runs the find command on it. Each item found is passed to the ls command for display.
Summary
This is a simple script that can perform simple functions. It saves time and can be useful when working with large file systems.
Script #! / 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:# [switches valid for ls command] [dirname...] # # Works with names that includes spaces and that start with a Hyphen.## Created by Nick Clifton.# Version 1.99 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 ind) 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 -" & & "xcopyright 1" = ~ "xmur.rooma.*"]]; 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 item0 While [$I-le $num_dirs] do list_things_in_dir I let "iTunes +" done} # Invoke mainmain ${1 + "$@"} above is all the contents of the article "how to use Bash scripts to list files, directories, executables and links". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.