Operating System - HP-UX
1833867 Members
2023 Online
110063 Solutions
New Discussion

chown on filenames with spaces in them

 
SOLVED
Go to solution
Jonathan Taylor_3
Occasional Advisor

chown on filenames with spaces in them

I have several hundred files that I need to change ownership. The problem is that there is a space in the filename. The only way I can get it to succeed is to double quote "" the filename. However this only works for one file at a time. I can't find a way to get it to work for a list of filenames. I have the filenames in a text file and I added " to the start and end of each line, then:

cat filenames.txt | while read line
do
chown newname:newgroup $line
done

and it is parsing the spaces as separate lines and all I get is a bunch of "file not found" errors. Does anyone have a workaround for this situation?
7 REPLIES 7
Rodney Hills
Honored Contributor

Re: chown on filenames with spaces in them

You could do a
IFS=""

So that spaces don't effect the "read" statement.

Or you could use xargs-
cat filenames.txt | xargs chown newname:newgroup

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: chown on filenames with spaces in them

PS- For the xargs exmample, remove the " from the text file. If you don't chown will think they are part of the filename...

HTH

-- Rod Hills
There be dragons...
Jonathan Taylor_3
Occasional Advisor

Re: chown on filenames with spaces in them

Thanks for the suggestions. I tried both of these methods and both still come back with:
No such file or directory

A. Clay Stephenson
Acclaimed Contributor
Solution

Re: chown on filenames with spaces in them

You were so close. Firest recreate your textfile or remove the quotes; then:

cat filenames.txt | while read line
do
chown newname:newgroup "${line}"
done

Whitespaces are perfectly legal (though discouraged) characters in pathnames. That is why disciplined shell programmers always quote their variables.
If it ain't broke, I can fix that.
DCE
Honored Contributor

Re: chown on filenames with spaces in them

If the blank is in the same location within the file you could use the ?

i.e.

ls t?t

results in

tet
tst
ttt
TwoProc
Honored Contributor

Re: chown on filenames with spaces in them

How about fixing the input file...
cat filenames.txt | sed "s/\ /\\ /g" > newfilenames.txt
(what the above *should* do is put a backslash in front of each space found in the filenames file - but you should check it out first as I've not done that).

Then, your script from your original posting could work.
We are the people our parents warned us about --Jimmy Buffett
Jonathan Taylor_3
Occasional Advisor

Re: chown on filenames with spaces in them

Mr. Stephenson, your solution worked perfectly. That's why I love this forum. Thanks for the help everyone.