Renaming a file that starts with "-"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming a file that starts with "-"
# 1  
Old 12-28-2014
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
Code:
-.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:
Code:
#!/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
# 2  
Old 12-28-2014
Try either quoting or escapeing the filename.

As in either one of:
Code:
mv ./"-.txt" $i.txt
mv ./\-.txt $i.txt

Also about the urls, id try with:
Code:
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:
Code:
# 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.

Last edited by sea; 12-29-2014 at 12:34 AM.. Reason: Code & text fix
# 3  
Old 12-28-2014
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
# 4  
Old 12-29-2014
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.
These 2 Users Gave Thanks to jim mcnamara For This Post:
# 5  
Old 12-29-2014
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:

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

I get:
Code:
$ ls -- ?.txt
1.txt	2.txt	3.txt	4.txt	5.txt

# 6  
Old 12-29-2014
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)
# 7  
Old 12-29-2014
Quote:
Originally Posted by faizlo
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:
Quote:
I tried your suggestion
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.

Last edited by Don Cragun; 12-29-2014 at 05:04 AM.. Reason: Add request for diagnostic output.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

SFTP batch not renaming file with "put"

I have a .ksh script that creates an sftp batch file and runs it through sftp. It works except for one thing. If I try to "put" to a different name, it doesn't use the specified remote name...it still "puts" the original local name. I've tried both of these, and neither work...it will always... (4 Replies)
Discussion started by: dbiggied
4 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

How to cat a file , name starts with "-" ?

I have a question - beginning user, just learning unix. I have a file that name starts with a hypen -, such as a name like : -myfile (yes, I know shold not start files names like this, but this is the question I have to figure out, and its driving me nuts) I know that this hyphen is like... (9 Replies)
Discussion started by: HikerLT
9 Replies
Login or Register to Ask a Question