Code 10: Add a script to PATH

Run a script without specifying its path
coding
linux
Author

Tony Phung

Published

September 21, 2024

1. Create Bash Script

Put all the bash code below into a new bash script (e.g. do_symlinks.sh)

#!/bin/bash

# Check if a prefix is supplied
if [ -z "$1" ]; then
          echo "Usage: $0 <prefix>"
            exit 1
fi

# Define the source directory and the prefix
SOURCE_DIR=~/og_files
PREFIX=$1

# Loop through files with the specified prefix and create symlinks
for file in "$SOURCE_DIR"/"$PREFIX"*; do
        if [[ $(basename "$file") != "$PREFIX"d* ]]; then
                ln -s "$file" .
                echo "Created symlink for $(basename "$file")"
        fi
done

2. Copy or Move /usr/local/bin and Source .bashrc

sudo cp do_symlinks.sh /usr/local/bin/do_symlinks && . ~/.bashrc

3. Create and change directory into test1 folder

mkdir test1 && cd test1

5. Confirm results ll

ll

6. Terminal Code

sudo cp do_symlinks.sh /usr/local/bin/do_symlinks && . ~/.bashrc
mkdir test1 && cd test1
do_symlinks tony_
ll

7. Screenshots of Code