cp files containing \ from a script

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cp files containing \ from a script
# 1  
cp files containing \ from a script

I am using a sh shell to try and create a script to remove backslashes (and other characters) from filenames.

At the end of the script I need to copy the original filename (with the backslash) to the new file name.

So far I have not been able to figure out how to do that from inside a for loop.


On the command line or inside the script, I can use this "exact" command to copy the file junk/junk.txt to junkjunk.txt

cp junk\\junk.txt junkjunk.txt

and it works perfectly. Smilie

But I need to use a variable or perhaps command substitution to represent the original filename inside the script, like this:

cp "$oldfilename" $newfilename

Even though I can create a variable, that echo "$oldfilename" shows contains junk\\junk.txt -- I can not get the cp command to work.

I have tried many variations of this using multiple backslashes with no success. Smilie

Is this even possible? Smilie

Thanks in advance.....
# 2  
cp $oldname $newname

# 3  
Thanks for the reply amsct. For a while I thougt this was the answer to my problem

But your code below, is just a variation of typing the text of the file name into the script by hand. It does work for turning the hand typed text in to a variable that cp can read and I think it will be useful in the future for things that are defaults that need to be run the same as they would be from the command line.... but, it does not solve my problem. I did not know that single quotes used like that, made a diifference in the output and I will play with it in the future and look for other uses, so thanks for the information.

Originally Posted by amsct
cp $oldname $newname

The processing flow of the "problem" is this:

1 - I read the contents of the directory into a variable.
2 - That variable is read by the for loop.
3 - The individual file names are provided as a variable by the for loop one at a time.
4 - I use that variable to cp the old file name to the new.

In other words my starting point for accessing the original file name, is always a variable returned by the for loop.

Somehow I need to turn that variable ( or a processed version of it ) into a version that will work with cp from inside the script.

Perhaps there are some changes I can make in the loop or there are other ways of reading in the file names to the loop.

But note that a for * type of loop, directly accesing the directory, will not work because the file names contain illegal characters and spaces.

Any ideas?
# 4  
Use parameter expansion and escape the backslash:
cp ${oldname/\\/\\\\} $newname

# 5  
Results of testing the two suggestions presented

This is the results of testing the two suggestions presented so far.

(I hope this is not a double post. I posted this earlier but it never appeared in the forum...)

As long as the exact block is entered into the script, all of the methods below will copy the old file (with the backslash) to the new file.

NONE of them will work when the oldname variable is read from the directory or passed from the "for loop" -- even when oldname is EXACTLY the same as any of the text assigned to oldname variables below.

cp $oldname $newname

The the above works WITHOUT quotes OR with
double quotes ONLY, on either or both variables

cp ${oldname/\/\\} $newname

cp $oldname $newname

In the TWO examples above, oldname MUST be quoted, with single OR double quotes,
AND newname MAY or MAY NOT be quoted with single OR double quotes.


Does this make any sense to anyone. I can not see how the bash shell could distinguish between a variable created inside the script, from data inside the script AND a variable created inside the script from identical text passed in from outside the script.

The only thing I can think of is that I am reading in the filenames to the variable through a pipe and that starts a sub-process which might fold, spindle or mutilate the variable in some manner that causes this problem.

I am leaving the country for a week in about 36 hours. If I have time I will rework the script to where the file list is imported without using a pipe and see if that changes how the variables react.

If anyone has any other ideas I would appreciate hearing them.

# 6  
Problem's solution

I never did truly understand "exactly" what it was about how I was using variables that was the problem but I found a work around, so to speak.

First if you want to use a variable to hold a "bad" file name, or one with unusual characters in it, to be used with the cp command -- then process the filename the minimum amount possible. I was processing the file name to create a new name, then I had to process it some more to undo the first processing so I could get the original name back. Bad idea, something happened to the filename while doing all that processing and I could never get it back to exactly what it was before... So I set out to minimize the processing of the filename to be used in the variable used to copy the old filename to the new filename.

Second, bash does its own processing of special characters and spaces when the cp command is run. Do not try to help it by adding escape characters (backslashes) to the variable. You have to deliver the name to the variable "exactly" as it appears when you use ls to display it. Then if the variable is quoted with double quotes, then bash can properly process it -- no matter what characters are in the file name.

Even though you can type the filename in by hand and add backslashes to the name and when you run the cp command from the script, it will work -- that ability will not transfer to a variable. Putting that same text with backslashes into a variable does not work because the variable does some processing "before or as" it expands the variable when the cp command is run. That "extra" processing produces something that is different than what was put in the variable, usually it adds backslashes and when it does, it does not match the "real" filename and can not copy it to the new filename.

Here is how I was originally processing the filenames. I was creating a list of filenames and storing them as a variable. Because the variable "fed" a for loop, I had to remove any spaces before I used the variable (for loops will not work with spaces in the filenames). I created the "filename list" variable and converted the spaces to % at the same time. I then separated the filenames into the basename and extension and stored them in variables. When I finished I reassembled this to get the original filename's basename and extension and removed the %s that I had inserted previously

This was too much processing. I ignored the KISS rule. The final cure was use the entire original filename with the % added -- as provided by the for loop -- instead of breaking it into basename and extension and having to reassemble all that back into the original file name. This allowed me to use the original filename with no processing other than removing the % symbol which I did at the same time I reassigned it to the final variable used by the cp command. Then all I had to do was quote the variable and everything worked.

My filename repair script works perfectly now and can repair filenames with leading and trailing spaces, backslashes, single quotes and any other characters that I have tried so far except the % symbol. There are two problems with % in the filename. When they are assigned to the variable storing the filenames bash turned % into %%, then the script turned all spaces into % and things went downhill from there. I decided to leave things alone and not try to process files with % in them, (files with % in them are very rare anyway).

I hope this makes sense, and can help someone in the future.

Thanks for everyone's help.
# 7  
Hi! you can try this:

find 'pwd' -name "*.*" | awk -F'\' -v var="" '{while(i==NF){var= print $var$i;} print "cp "$0" "$var""}'

This is just an idea to say you can do something like this, Please feel free to edit it if it is not working as per your requirement.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #602
Difficulty: Medium
MySQL 8.0 incorporates a transactional data dictionary that stores information about database objects.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for field wise record count for different Files .csv files

Hi, Very good wishes to all! Please help to provide the shell script for generating the record counts in filed wise from the .csv file My question: Source file: Field1 Field2 Field3 abc 12f sLm 1234 hjd 12d Hyd 34 Chn My target file should generate the .csv file with the... (14 Replies)
Discussion started by: Kirands
14 Replies

2. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

3. Shell Programming and Scripting

How to create or convert to pdf files from csv files using shell script?

Hi, Can anyone help me how to convert a .csv file to a .pdf file using shell script Thanks (2 Replies)
Discussion started by: ssk250
2 Replies

4. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

5. Shell Programming and Scripting

Read files in shell script code and run a C program on those files

HI, I am trying to implement a simple shell script program that does not make use of ls or find commands as they are quite expensive on very large sets of files. So, I am trying to generate the file list myself. What I am trying to do is this: 1. Generate a file name using shell script, for... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

6. Shell Programming and Scripting

Need to exclude .NFSxxx files in clear old files batch script

I am new to Shell Scripting and need some help. The following batch job has been failing for me due to the .nfsxxx files in use. I need to know how to modify the following script to exclude the .nfsxxx files so this batch job will not fail on me. I have done lots of googling and keep coming back... (2 Replies)
Discussion started by: kimberlyg2007
2 Replies

7. Shell Programming and Scripting

need a shell script to extract the files from source file and check whether those files existonserve

Hi, I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is # echo $0 -sh My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
Discussion started by: muraliinfy04
13 Replies

8. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

9. Shell Programming and Scripting

Help - Bug: A script to compile two types of data files into two temporary files

Dear other forum members, I'm writing a script for my homework, but I'm scratching all over my head and still can't figure out what I did wrong. Please help me. I just started to learn about bash scripting, and I appreciate if anyone of you can point out my errors. I thank you in advance. ... (3 Replies)
Discussion started by: ilove2smoke
3 Replies

10. Shell Programming and Scripting

How to retrieve all the linked script files/ctl files/sql files?

Hi I am going to migrate our datawarehouse system from HP Tru 64 Unix to the Red Hat Linux. Inside the box, it is running around 40 cron jobs; inside each cron job, it is calling other shell script files, and the shell script files may again call other shell script files or ctl files(for... (1 Reply)
Discussion started by: franksubramania
1 Replies

Featured Tech Videos