1834104 Members
2185 Online
110063 Solutions
New Discussion

Shell Puzzle

 
SOLVED
Go to solution
Dan Am
Frequent Advisor

Shell Puzzle

Hi All,
in our Unix-Class here, s'one came up with this idea:

touch " "
touch " "

So, now we have two files, each with only whitespaces in their name. How can we:
1) access each file selectively
2) make the whitespaces visible

ls -b does not do the trick, as whitespaces are not "non-printing" as such.

Anyone ever run into this ?

Regards
Dan
do what you can. don't if you can't.
13 REPLIES 13
Peter Kloetgen
Esteemed Contributor

Re: Shell Puzzle

Hi,

if you call the files with the vi- command, it shows the file name in the left bottom corner marked with ""- characters.

filename " "

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
federico_3
Honored Contributor

Re: Shell Puzzle

You should use the inode number that can be found with ls -i



Ciao
Federico
Peter Kloetgen
Esteemed Contributor

Re: Shell Puzzle

Hi,

sorry, i forgot to mention how to access the files:

ls -l " " in the directory or:

ls -l /path_to_file/" "

rm /path_to_file/" "
rm ./" "

Allways stay on the bright site of life!

Peter
I'm learning here as well as helping
Dan Am
Frequent Advisor

Re: Shell Puzzle

Funny, the forum machine did not actually process this richt either:

The first filename is 4 Whitespaces.
The second is 1 Whitespace.

Which makes it kind of hard.
How do do I handle a file by Inode Number ?

Regrds
Dan
do what you can. don't if you can't.
Steve Steel
Honored Contributor

Re: Shell Puzzle

Hi


try


ls -b|while read line
do
echo filename \"$line\"
done

Then you have the names neatly between brackets
and can use the data


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Vasudevan MV
Frequent Advisor

Re: Shell Puzzle

Hi,

Do the following things & see its output. You can use backslash (\) for special characters like blank spaces etc.

touch " "

echo "hello" >> \

cat \ or vi \

Hope this helps

Vasu
Peter Kloetgen
Esteemed Contributor

Re: Shell Puzzle

Hi,

try this command:

cat/more /path_to_file/" "
cat/more /path_to_file/" "

or the same syntax with each command. You simply have to quote the space characters.
If you want to find out, how many space- characters are in the name, use the following command:

vi /path_to_file/" *"


Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
H.Merijn Brand (procura
Honored Contributor

Re: Shell Puzzle

perl -e 'opendir D,".";for(grep/^ /,readdir D){print"rm [$_] ? ";scalar=~m/^[YyJj]/ and unlink$_}'

if you'd put that in a shell, you can arg it, like

# prm \ *

(UNTESTED)
#!/opt/perl/bin/perl

opendir D, ".";
my @files = grep { -f } readdir D;

for (@ARGV){
foreach my $f (@files) {
$f =~ m/$_/ or next;
print "rm [$_] ? ";
scalar =~ m/^[YyJj]/ and unlink $_;
}
}
Enjoy, Have FUN! H.Merijn
Peter Kloetgen
Esteemed Contributor

Re: Shell Puzzle

Hi Dan,

sorry, typo in last posting of mine:

ls -l /path_to_files/" *"

--> lists all files beginnig with a space, you would get two entries

vi /path_to_files/" *"

--> would open all files beginning with a space, one after the other and show you the names with visible spaces

Then, when you know the number of spaces, you can quote the file name when using the needed commands:

cmd "number_of_spaces"
or
cmd " *"

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Dan Am
Frequent Advisor

Re: Shell Puzzle

hi guys,
thanks to you all. vi seems to be the only
"readymade" solution here. I have not tried the Perl thing. I'll let you know on that.
Thanks again.
Regards
Dan
do what you can. don't if you can't.
Rodney Hills
Honored Contributor
Solution

Re: Shell Puzzle

If you think you might have a filename with spaces, then you can do the following to see them-

ls -1 | sed -e 's/ /?/g'

This will do a ls command a replace any spaces with "?", thus making them visible.

-- Rod Hills
There be dragons...
Darrell Allen
Honored Contributor

Re: Shell Puzzle

Hi Dan,

To access the files, quote them: surround the appropriate number of spaces with "".

To list multiple files beginning with a space:
ls " "*

Note the asterik is following the quotes, not inside.

To make the spaces visible you could do:
ls " "* | tr " " .
The output would be:
.
....

To see what characters are in the file names:
ls | od -b
0000000 040 012 040 040 040 040 012
0000007

Note 012 is a newline which delimits filenames in this output.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Frank Slootweg
Honored Contributor

Re: Shell Puzzle

[I hope that this comes out OK. When posting, 'something' seems to convert multiple spaces to a single one.]

Adding to the other responses:

Let's first make some files *with* a content:

echo one >' ' # One space.
echo four >' ' # Four spaces.

> 1) access each file selectively

General:

command './ ' # One space.
command './ ' # Four spaces.

I.e. for *example*:

$ cat './ ' # One space.
one
$ cat './ ' # Four spaces.
four
$

> 2) make the whitespaces visible

ls -1 | cat -e
$
$
[other filenames]
$