# Writing Scripts

Instead of running a single command at a time, you can combine multiple commands into a file to create a script. Scripts can simplify multi-step processes into a single invocation, saving you time in the long-run.

Writing a script is as simple as writing a file. Usually, we make the first line of the script `#!/bin/bash` to tell the shell to use `bash` when running the script.

You can create variables in a bash script using the `=` operator. So to make a variable named `myname`, you'd write `myname="Allen"`. To use the variable later in the script, you'd prefix it with a `$`. For example, `$myname`.

You can also ask for a user's input and store that into a variable by using the `read` command. Preface it with `echo "question?"` to give context for what the user is inputting. For example:

```bash
echo "What is your name?"
read name

echo "Hello, $name"
```

You can run shell commands inside of a script and store their results in a variable. To do so, you wrap the command with `$()`. For example, to get the size of a file and store it in a variable, you'd do `file_size = $(du file)`.

Knowing this, let's create a script that looks through the files in a directory, and moves any files above a certain size to a new folder. Name it `sizewatcher.sh`

<pre class="language-bash"><code class="lang-bash">#!/bin/bash

# Make variables for directory to loop through
# and directory where files should be moved.
directory="/home/asoberan/unixclass"
new_directory="/home/asoberan/bigfiles"

# If the new directory doesn't exist, then
# create the directory
<strong>if [ ! -d $new_directory]; then
</strong><strong>    mkdir $new_directory;
</strong><strong>fi;
</strong>
# Loop through the contents in the directory.
# Only move files, skip directories.
# Store the size of the file (in KB), in
# file_size. If the file size is greater
# than 400000 KB, move the file to the
# new directory 
for file in $directory/*;
do
    if [ -f "$file" ]; then
      file_size=$(du "$file" | cut -f1)
      if [ "$file_size" -gt 400000 ]; then
          mv "$file" "$new_directory"
      fi;
    fi;
done;
</code></pre>

Once you've created and saved this file, make sure to modify its permissions to let it be executed. An easy way of doing this is running the following command:

```bash
chmod +x sizewatcher.sh
```

Then run the script:

```bash
./sizewatcher.sh
```

Let's create another script that asks a user for a directory, then sorts the files in that directory into folders that correspond to the year and month that the file was created. Name it `sorter.sh`:&#x20;

```bash
#!/bin/bash

# Ask for directory and store it in a variable
# called "directory"
echo "What directory to check?"
read directory

# Check if the provided argument is a directory
if [ ! -d "$directory" ]; then
    echo "Error: $directory is not a directory."
    exit 1
fi

# Iterate over files in the directory
for file in "$directory"/*; do
    # Check to see if the file is really file and
    # not a directory, etc.
    if [ -f "$file" ]; then
        # Get the year and month of creation for the file
        year=$(date -r "$file" +%Y)
        month=$(date -r "$file" +%m)

        # Create directory for the year and month if it doesn't exist
        mkdir -p "$directory/$year-$month"

        # Move the file to the corresponding directory
        mv "$file" "$directory/$year-$month"
        echo "Moved $file to $directory/$year-$month"
    fi
done

echo "File sorting complete."

```

Make it executable and then run it.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://igb.mit.edu/mini-courses/introduction-to-unix/writing-scripts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
