How to Download and Execute a Script Using wget in Ubuntu: A Step-by-Step Guide


2 views

The most efficient way to download and run a script in Ubuntu is by combining wget, chmod, and direct execution. Here's the complete workflow:


# Download script (save with custom name if needed)
wget https://example.com/script.sh -O custom_script.sh

# Make it executable
chmod +x custom_script.sh

# Execute the script
./custom_script.sh

For various script formats, you might need different execution methods:


# Python script example
wget https://example.com/script.py
chmod +x script.py
python3 script.py

# Perl script example
wget https://example.com/script.pl
chmod +x script.pl
perl script.pl

For quick execution without saving the file permanently:


wget -qO- https://example.com/script.sh | bash

Note: This approach skips the file saving step and executes directly, but use with caution as you can't inspect the script first.

Always verify scripts before execution:


# Download and inspect first
wget https://example.com/script.sh
less script.sh  # Review contents
chmod +x script.sh
./script.sh

For more complex scenarios:


# Download to specific directory
wget https://example.com/script.sh -P ~/scripts/

# Download and run with arguments
wget https://example.com/script.sh
chmod +x script.sh
./script.sh arg1 arg2

# Download and run as root
wget https://example.com/script.sh
chmod +x script.sh
sudo ./script.sh

When you need to download and immediately execute a script on Ubuntu, the process typically involves three key steps:

  1. Downloading the file using wget
  2. Making it executable with chmod
  3. Running the script

Here's the fundamental approach:

wget https://example.com/script.sh -O my_script.sh
chmod +x my_script.sh
./my_script.sh

Downloading and Executing in One Line

For quick execution without saving the file:

wget -qO- https://example.com/script.sh | bash

Warning: This method skips the opportunity to review the script before execution, which could be a security risk.

Handling Different Script Types

For Python scripts:

wget https://example.com/script.py
chmod +x script.py
python3 script.py

Adding Verification Steps

A safer approach that includes checksum verification:

wget https://example.com/script.sh
wget https://example.com/script.sh.sha256
sha256sum -c script.sh.sha256
chmod +x script.sh
./script.sh

1. Permission denied errors: Always remember to chmod +x

2. Script not found: Verify your current directory matches where you downloaded the file

3. Missing interpreter: Ensure you have the correct runtime (bash, python, etc.) installed

Before running any script you download:

  • Check the source's reputation
  • Review the script contents if possible
  • Consider running in a container or VM first
  • Avoid using sudo unless absolutely necessary

For GitHub repositories, you might prefer:

curl -sSL https://raw.githubusercontent.com/user/repo/branch/script.sh | bash

Or using git clone for entire projects:

git clone https://github.com/user/repo.git
cd repo
chmod +x install.sh
./install.sh