moving files that have spaces in variables -bash scripting
I seem to not be able to move or copy files that have spaces that are contained in variables.
for instance v1=$(ls *.html) mv $v1 /opt if files have spaces then i get an error trying to move them because it tries to break up file names. I have tried ls -Q for loops ls --quoting-style=shell mv "$v1" /opt ? |
mv "$v1" /opt
should work ... Cheers, Tink |
no that does not work either, i forgot to list it in the tried section.
|
What about a completely different approach?
find -maxdepth 1 -iname "*html" -exec mv "{}" /opt/. \; Cheers, Tink |
Yes that did work, but I need to work with the files stored in a variable before the move.
It is weird to me that the command doesn't work or are we just missing something? |
Quote:
Cheers, Tink |
mv "$var1" /opt
mv: cannot stat `index 1.out\nindex 2.out': No such file or directory |
This is the way I've always done it, but it doesn't seem to be extremely popular:
Code:
#!/bin/bash |
thank you, that does work
can you explaing IFS ' ' thing? or point me in the right direction thanks again |
IFS is an environment variable used by the shell to determine where to break a list of items into single items. In other words, the IFS variable contains a list of delimeters that mark the end of an item. (man bash and search for IFS if you want to know the gory details) By default, IFS is defined to include space, tab, and newline. Normally this is what people want. So, the magic hand-waving at the top of the script saves the existing value of IFS in a temporary variable, sets IFS to include only the newline character, and that forces bash to treat files with spaces as a single item. Then, it restores the IFS to the original value in case the script has anything else to do that relies on the original value of IFS.
|
ls | cpio -pd /opt
|
All times are GMT -5. The time now is 09:00 AM. |