I have a file which contains downloading links like this:


I want to download all of them using a script. If the name starts with 's' download this file to the s directory, if it's b, then move it to b directory...


Download all files, then move them using shell globs:

wget -i /path/to/download_list
mv s* ./s/
mv b* ./b/
  • -i: Read URLs from a local or external file.

You might get a warning:

mv: cannot move 's' to a subdirectory of itself.

That's fine, you can ignore it, or use find instead:

wget -i /path/to/download_list
find -maxdepth 1 -iname "s*" -type f -exec mv "{}" ./s \;
find -maxdepth 1 -iname "b*" -type f -exec mv "{}" ./b \;

And with a for loop you can run in on all alphabets, script name is script.sh:

wget -i /path/to/download_list
mkdir -p {a..z}
for l in {a..z};
    find -maxdepth 1 -type f -iname "${l}*" -not -iname script.sh -exec mv "{}" "./${l}" \;
  • i want with scripts – no name Jan 15 at 8:19
  • What you are seeing in my answer is a simple script, save it in a file, make it executable, then run it. You have to change it so it suits your needs. – Ravexina Jan 15 at 8:20
  • if file name starting c or x or k ? write all variants? – no name Jan 15 at 8:23
  • You didn't mention that in your question, then you have to use a loop, I'll update the answer. – Ravexina Jan 15 at 8:25
  • uea ,i want to use loop – no name Jan 15 at 8:30

An addition to @Ravexina's nice answer.

Solution without a loop:

wget -i /path/to/download_list
mkdir -p {a..z}
# mv the files with rename tool
rename 's/^((.).+)$/$2\/$1/' *
# clean up empty directories
find . -maxdepth 1 -name '[a-z]' -type d -empty -delete

A solution to download directly into the desired folder:

# expand file to list and iterate
for path in $(<"/path/to/download_list"); do
    # get file part of path
    name=$(basename "$path")
    # use first character of name as dir
    # create dir is not exist
    mkdir -p "$dir"
    # download path directly to dir
    wget "$path" -P "$dir"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.