Script to Print the Path of a File
This script will work only when you provide one argument
Here's a script that takes a filename as an argument and prints the full path to that file:
Script :
get_file_path.sh
#!/bin/bash
# This script prints the full path of a given file
if [ "$#" -ne 1 ]; then
echo "Usage: $0 filename"
exit 1
fi
FILE="$1"
if [ -f "$FILE" ]; then
echo "Full path: $(realpath "$FILE")"
else
echo "File not found: $FILE"
fi
chmod +x get_file_path.sh
./get_file_path.sh filename
Replace filename with the name of the file you want to find the path for. This script will print the full path of the file if it exists, or a "File not found" message if it doesn't.
This script will work for multiple arguments
Script:
get_full_paths.sh
#!/bin/bash
# Check if at least one argument is provided
if [ "$#" -eq 0 ]; then
echo "Usage: $0 <filename1> <filename2> ..."
exit 1
fi
# Iterate through all provided arguments
for FILE in "$@"; do
if [ -f "$FILE" ]; then
echo "Full path of '$FILE': $(realpath "$FILE")"
else
echo "File not found: $FILE"
fi
done
Steps to Use:
get_full_paths.sh
chmod +x get_full_paths.sh
./get_full_paths.sh file1.txt file2.txt file3.txt
Kindly Follow for more updates: