Linux test Command Explained: File Checks, Conditions & Scripting Tricks!

Published: 17 February 2025
on channel: TechOps Tutorials
62
0

🔹 Want to master the test command in Linux? In this tutorial, we’ll explore how to use test (and [ brackets) for checking file existence, permissions, string comparisons, and more! Perfect for shell scripting and automation! 🚀

✔️ What You’ll Learn:

How to check if a file or directory exists 📁
Testing if a file is readable, writable, or executable ✅
String comparisons and number conditions 🔢
Combining multiple conditions for better scripting ⚙️

📌 Commands Used in This Video:
Check if a directory exists
if [ -d /backup ]; then echo "Exists"; else echo "Not found"; fi

Check if a file exists
if [ -f "/etc/app/config.yaml" ]; then echo "File exists"; else echo "Missing"; fi

Check if a script is executable
if [ -x "/usr/local/bin/deploy.sh" ]; then echo "Can execute"; else echo "Cannot execute"; fi

Check if a string is empty
if [ -z "$username" ]; then echo "Empty"; else echo "Not empty"; fi

Compare numbers
if [ "$num1" -gt "$num2" ]; then echo "Greater"; else echo "Smaller or equal"; fi

Combine conditions (AND example)
if [ -r "/tmp/test_rw_file" -a -w "/tmp/test_rw_file" ]; then echo "Readable & Writable"; else echo "Not both"; fi

🔔 Want more Linux tips?
💬 Comment your favorite Linux command!
📌 Subscribe & hit the bell 🔔 for more Linux tutorials!
#Linux #BashScripting #LinuxCommands #ShellScripting #techtips


On this page of the site you can watch the video online Linux test Command Explained: File Checks, Conditions & Scripting Tricks! with a duration of hours minute second in good quality, which was uploaded by the user TechOps Tutorials 17 February 2025, share the link with friends and acquaintances, this video has already been watched 62 times on youtube and it was liked by 0 viewers. Enjoy your viewing!