Skip to content
Advertisement

Create directories and download files by reading input from a file

cat paste_output.txt | while read -r file_name path_name file; 
do mkdir -p -- "$path_name"; 
wget "$file_name"; 
mv "$file" "$path_name"; 
done; 

Hi! I have this piece of code that reads field by field from the file specified. What I am trying to do here is I am creating a directory that is specified in second field and then I am downloading file specified in first field and then after having that file downloaded I am that file in the directory specified in second field.

Output: I am getting the desired directory structure and files downloaded however files are downloading in the directory I am executing the commands from.

How to move files in the desired directories?

Advertisement

Answer

You can use the -P flag of wget to put the file in the target directory. If the directory doesn’t exist, it will create it, so this also let’s you save on the mkdir.

while read -r file_name path_name file; do
    wget -P "$path_name" "$file_name"
done < paste_output.txt

I made some other improvements to the script:

  • The cat is useless, input redirection is better
  • The semicolons at end of lines are unnecessary
  • It’s good to indent the body of loops, for readability
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement