Skip to content

Instantly share code, notes, and snippets.

@lstellway
Last active February 22, 2022 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lstellway/66fdbd3b25cd214fbc4825ee7e9e9348 to your computer and use it in GitHub Desktop.
Save lstellway/66fdbd3b25cd214fbc4825ee7e9e9348 to your computer and use it in GitHub Desktop.
Distribute files in a directory to subfolders based on URL's in a specified file.
#!/bin/bash
# Loop through links
while IFS="" read -r LINE || [ -n "$LINE" ]; do
# Replace domain (everything before "/media")
PATH=${LINE//**\/media/media}
# Get file file name (everything after the last "/")
FILE_NAME=${PATH//*\//}
# Remove the last "/" and filename from the path
PATH=${PATH/\/$FILE_NAME/}
# Create the directory
mkdir -p $PATH
# Copy the file to the directory
cp $FILE_NAME $PATH
echo "Moved ${FILE_NAME} to ${PATH}."
done < $1
@lstellway
Copy link
Author

lstellway commented May 23, 2020

Example:

Data File (images.txt):

https://example.com/media/magpleasure/mpblog/post_thumbnail_file/1/8/188e919dbf4c23c7a3f14f98bbc0bd05.png
https://example.com/media/magpleasure/mpblog/post_thumbnail_file/d/7/d7f04f7e07b583c80d96a85810feaa89.png
https://example.com/media/magpleasure/mpblog/post_thumbnail_file/4/d/4d63fbf6dfa75c5019bdf88819a24c5b.png

Working Directory:

.
..
188e919dbf4c23c7a3f14f98bbc0bd05.png
d7f04f7e07b583c80d96a85810feaa89.png
4d63fbf6dfa75c5019bdf88819a24c5b.png

Command:

distribute images.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment