I am downloading some files from the Internet. They are from different sources and they are all called
-.txt
that is: a dash "-" followed by a dot "." and then the file extension.
So i wrote a small script that hoped will downloads the first file and then rename them once they are completely downloaded. Here is the script I wrote:
#!/bin/sh
for i in `seq 1 1 30`
do
wget url
mv ./-.txt $i.txt
done
but what I get is that the script (above) downloads the first file "-.txt" and does not rename it to 1.txt as I wanted, and so keeps giving me the message that file "-.txt" has already been downloaded.
What is wrong in my script? (I have little experience with scripting in bash)
Thank you for your quick response.
Unfortunately, the files I download are contained in a python script (they are numerous) so I am left with the first answer of yours. I used the wget example to illustrate my problem. But it is fine since I just replace it with the python script.
I tried both escaping the dash and quoting it, but they didn't work. I still download the first file as "-.txt" and it never gets renamed to "1.txt".
Numbering these files is important, and I do use the first 3000 files of the collection I am trying to download (they are scientific txt files with lots of numbers)
When you invoke a "command line" in python, you can use the equivalent of exec or system. In either case, the child process (commands like mv and wget) are being executed by /bin/sh, which may or may not be the shell you think you are using.
There are two ways to approach this:
Play 20 questions and hope we get you where you need to be
OR make a suggestion for a quick workaround.
I vote for number two:
Use a separate script to download and rename files. Do not mess with doing that in python unless what I said about /bin/sh is very clear to you.
So your python code gets a few lines commented out - the ones doing downloads and renames. You run a five line shell script like what sea provided. Then run the abbreviated python. One advantage: you can see the filenames. Before you mess with running the python code.
I am assuming you want to complete a long task, so this will get you there the fastest.
I tried your suggestion, still I get the same results. The first files is downloaded but not renamed (moved), and then the scripts keeps running giving the message that the files exists. If I keep it running, it will produce the message 3000 times (2999 times) so I have to stop it (^C)
Sorry, I meant to answer Scrutinizer first suggestion.
I use Ubuntu Linux (14.04.0x) and I am using bash.
The problem is this:
I am using a script to download files that all have the same name "-.txt" (without the quotes) and I am trying to write a script that once it downloads the first file (-.txt) it will rename it to "1.txt" (again, without quotes) and so the second downloaded file ( also named -.txt) will be renamed 2.txt.
Since these files are large they do take some time to be downloaded (each files is ~ 120 MB)
Thank you all again
---------- Post updated at 05:55 PM ---------- Previous update was at 05:23 PM ----------
OK, problem solved. It was a bug in the downloading python script.
The script was made to download "ALL" files once launched, and therefore, it will download all files (all with name "-.txt") before the loop moves to the second command which is to rename the files.
I am to fix the bug, and then run the script.
How did I figure it out?
Out of frustration, I wrote this script:
for i in {1..5}
do
touch ./-.txt
mv ./-txt $i.txt
done
The result was, as I expected (and hoped) 1.txt, ..., 5.txt
so I directed my attention to understand how the python script works (I did not go through the script, though) and watched it, trying to figure out why does it pass the mv command?
I then added a couple of sleep 10 commands after the downloading command and the mv command to make sure how does each step run... and this is how I figured it out.
Thank you all again for your help and suggestions....
Happy Holidays