Renaming a file that starts with "-"

Hi

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

Try either quoting or escapeing the filename.

As in either one of:

mv ./"-.txt" $i.txt
mv ./\-.txt $i.txt

Also about the urls, id try with:

urls="url1 url2 ..."
for url in $urls
do
   wget -o $(basename $url).txt $url 
done

So you avoid to rename the files, alternativly you could insert at the proper places:

# Below: urls=
i=0

# Replace on wget line:
$i-$(basename $url).txt

# Below wget
i=$(($i + 1))

Hope this helps & happy holidays

EDIT:
Understand that it saved the file as -.txt because - was the default input as you did not define an output file.

Hi

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)

Thank you again

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:

  1. Play 20 questions and hope we get you where you need to be
  2. 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.

2 Likes

I do not see the need for extra quotes or escapes here. By using ./ the file name will not be interpreted as a command option...

It must be in the python part. To illustrate, if I use an equivalent bash script like this:

for i in {1..5}
do
  curl http://www.google.com > -.txt
  mv ./-.txt $i.txt
done

I get:

$ ls -- ?.txt
1.txt	2.txt	3.txt	4.txt	5.txt

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)

You have suggestions from sea, jim mcnamara, and Scrutinizer. When you said:

which suggestion were you talking about? Have you looked at the other suggestions?

What OS are you using?

Please show us the EXACT diagnostic output produced by the failing mv command.

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

How about sharing that script that downloads all the files?
--> As-is, in code tags, please.

So we could see why it names them like that in the first place.
Because what (of the code) you had posted so far would not work at all.

EDIT:
Ahh nevermind, glad it works now.
Happy holidays :slight_smile:

I would have happily shared the Python script, but it belongs to my work.

Good thing you got the bug resolved. Thanks for reporting back....