[Ilugc] how to copy a file with spaces in the name?

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Thu Jan 7 07:25:44 2010

On Thu, Jan 7, 2010 at 7:02 AM, Kenneth Gonsalves
<lawgon@xxxxxxxxxxxxxxx> wrote:

hi,

I had a file 'filename 04 april v3.pdf' - I tried to copy it to another
directory, but cp refused to oblige. I managed to generate the command by
typing 'filename\' and pressing tab key. That is as far as I could go. I 
finally
had to go back to kmail and save the file in the correct directory.
--

Very typical of Windoze/Winblows generated files. Unfortunately even mp3
players and handhelds seem to have similar issues.

It is so hard to deal with spaces in file names in UNIX but other OSes happily
create such files to make our lives hard.

In fact your question is not quite clear to me.

You can use backslash before every space. For instance, you can do

filename\ 04\ april\ v3.pdf

In case you want to replace all filenames in toto please use the mmv
command. I ran a tip on this very long ago. Please search the
archives.

http://ae.iitm.ac.in/pipermail/ilugc

$ touch 'filename 04 apr v3.pdf'
$ touch 'foo 05 nov v4.pdf'
$ mmv -v "*[ ]*[ ]*[ ]*.pdf" "#1-#5-#7.pdf"
filename 04 apr v3.pdf -> filename-apr-v3.pdf : done
foo 05 nov v4.pdf -> foo-nov-v4.pdf : done

Or you could use tr ' ' '-' if you wish to replace with '-'. You can
also choose
to do it with some other char like underscore or even a letter.

for i in *
do
newname=`echo $i | tr ' ' '-' `
echo $newname
mv "$i" $newname
done

I tried it and this works.

-Girish
-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com

Other related posts: