Josh Unwin

joshua unwin

FRONT END DEVELOPER

  • Tree

    Climate activist

  • Clapperboard

    Background in film workflow

React | JS | Rails | Python
my blog & more
Back

Bash copy paths

Josh Unwin

Joshua Unwin •

Edit


Copies a list of paths to a destination. Checks whether each path is a file or a directory and issues the correct cp command.

copy_paths.sh

#!/bin/sh

#!/bin/bash

# Set the source and destination directories
dst_dir="/Users/joshunwin/Desktop/test_output"

# Set the list of files to copy, either strings or direct paths
paths=(path1 path2 path3)

# Iterate over the list of files and copy them
for path in "${paths[@]}"; do
  # Check if the path exists
  if test -e "$path"; then
  # Check if the path is a file
    if test -f "$path"; then
      echo "$path is a file, copying..."
      cp "$path" "$dst_dir"
  # Check if the path is a directory
    elif test -d "$path"; then
      echo "$path is a directory, copying..."
      cp -r "$path" "$dst_dir"
    fi
  # If the path does not exist, print an error message
  else
    echo "ERROR: $path does not exist"
  fi
done