Count Executable Files in PATH Directories

LinuxLinuxIntermediate
Practice Now

Introduction

The $PATH environmental variable is used to specify the directories in which the shell looks for executable files. In this challenge, we will create a script pathCount.sh that will display the number of executable files in each directory listed in the $PATH environmental variable. We will use loops, arrays, and conditional statements to achieve this.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/FunctionsandScopeGroup(["`Functions and Scope`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/BasicFileOperationsGroup -.-> linux/wc("`Text Counting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/TextProcessingGroup -.-> linux/sed("`Stream Editing`") linux/TextProcessingGroup -.-> linux/tr("`Character Translating`") linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/arrays("`Arrays`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/FunctionsandScopeGroup -.-> shell/scope_vars("`Scope of Variables`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills linux/wc -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} linux/echo -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} linux/pipeline -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} linux/redirect -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} linux/sed -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} linux/tr -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} linux/find -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/shebang -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/comments -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/quoting -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/variables_decl -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/variables_usage -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/str_manipulation -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/arrays -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/param_expansion -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/for_loops -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/cond_expr -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/scope_vars -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/arith_expansion -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/cmd_substitution -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/subshells -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} shell/globbing_expansion -.-> lab-18305{{"`Count Executable Files in PATH Directories`"}} end

Path Count

In this challenge, you are required to create a script that displays the number of executable files in each directory listed in the $PATH environmental variable.

Tasks

You need to complete the following tasks:

  • Fetch each directory from the PATH variable.
  • Use the -x option in the if condition to check executable permission.
  • Print the directory and the number of executable files one by one.
  • Print the total number of executable files at the end.
  • Count only files with executable permission.
  • Verify the presence of the path every time.

Requirements

You must meet the following requirements:

  • The script should be named pathCount.sh.
  • The working directory for the script should be ~/project.
  • Utilize Bash environmental variables.
  • Understand the working of the tr command.
  • Use loops and arrays.
  • Check the permission of files in the script.

Example

current directory: /usr/lib/jvm/java-11-openjdk-amd64/bin
current count: 34

current directory: /usr/lib/jvm/java-11-openjdk-amd64/bin
current count: 34

current directory: /usr/lib/code-server/lib/vscode/bin/remote-cli
current count: 2

...

[OUTPUT] Total-Count: [2135] [OUTPUT]

Summary

After completing this challenge, we have learned how to work with environmental variables in bash scripts. We have also learned how to use loops and arrays to iterate through directories and files, and how to check file permissions using conditional statements. This challenge has helped us to understand the significance of the $PATH environmental variable and how it is used to locate executable files. By creating the pathCount.sh script, we have gained a better understanding of how to count the number of executable files in each directory listed in the $PATH environmental variable.

Other Linux Tutorials you may like