Operating System - HP-UX
1838334 Members
3100 Online
110125 Solutions
New Discussion

Can you explain why it shows myfile twice in the last command?

 
Avinash Agarkar
Valued Contributor

Can you explain why it shows myfile twice in the last command?

test:cd /tmp

test:/tmp > mkdir test

testp > cd test

test:/tmp/test > touch .myfile

test:/tmp/test > ls -al
total 464
drwxr-x--- 2 lsapas6 lsapuser 512 24 Jun 06:44 .
drwxrwxrwt 44 bin bin 230912 24 Jun 06:44 ..
-rw-r----- 1 lsapas6 lsapuser 0 24 Jun 06:44 .myfile

test:/tmp/test > ls -l .* | grep myfile
-rw-r----- 1 lsapas6 lsapuser 0 24 Jun 06:44 .myfile

test:/tmp/test > ls -al .* | grep myfile
-rw-r----- 1 lsapas6 lsapuser 0 24 Jun 06:44 .myfile
-rw-r----- 1 lsapas6 lsapuser 0 24 Jun 06:44 .myfile

Thanks,
Avinash
Great Power Comes With Great Responsibility
11 REPLIES 11
OFC_EDM
Respected Contributor

Re: Can you explain why it shows myfile twice in the last command?

I think it's because it's listing the file ".myfile" and then listing the contents of "."
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: Can you explain why it shows myfile twice in the last command?

clicked submit by accident.

To continue my post.

"." is the current directory. So when it lists the contents of the current directory it will disply ".myfile" again when using ls -al .*

Cheers
The Devil is in the detail.
Torsten.
Acclaimed Contributor

Re: Can you explain why it shows myfile twice in the last command?

Try this:

test:/tmp/test > ls -al .*

(leave the "grep" away).

You will see that the system is doing the following:

# ls -al
# ls -al .
# ls -al ..


Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Kenan Erdey
Honored Contributor

Re: Can you explain why it shows myfile twice in the last command?

hi,

ls -al .[a-z]* |grep myfile works. is it possible to escape "." in .* ?
Computers have lots of memory but no imagination
Rasheed Tamton
Honored Contributor

Re: Can you explain why it shows myfile twice in the last command?

ls -a|grep ^.m
ll |grep \\.m


Regards,
Rasheed Tamton.
Rasheed Tamton
Honored Contributor

Re: Can you explain why it shows myfile twice in the last command?

With awk:

ll|awk '{print $9}'|grep ^\\.
Laurent Menase
Honored Contributor

Re: Can you explain why it shows myfile twice in the last command?

To avoid that type of thing use
ls -al .[[:alnum:]]*
Dennis Handly
Acclaimed Contributor

Re: Can you explain why it shows myfile twice in the last command?

>Kenan: ls -al .[a-z]* | grep myfile works. is it possible to escape "." in .*?

No need to escape "." in the Pattern Matching Notation that the shell uses. There "?" is the any char, character. You would have to escape it in grep's Basic Regular Expression.
Torsten.
Acclaimed Contributor

Re: Can you explain why it shows myfile twice in the last command?

Because of the "*" the shell will substitute the "*" and your "ls" command will run recursively as mentioned above.

this is the reason why you see the file twice.

It is a result of

# ls -al
followed by
# ls -al .

Both commands find the file.

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Trupti Mahadik
New Member

Re: Can you explain why it shows myfile twice in the last command?

It is very obvious to show .myfile twice for ur command.
Explanation as follows:

1)Ur command ls -al .* | grep myfile
Shell is executing command in following way.

ls - list
l - long list
a - hidden files

now ls -al .* will show all hidden files as well as hidden directories from current working directory.

1) So as per execution of command,shell will find â .â Directory (i.e. Current directory - /tmp/test)
U have issued log listing, so it will show files & directory tree inside â .â means current directory /tmp/test. There again u have greped for .myfile, so it will show u .myfile.

So thatâ s ur first o/p â -rw-r----- 1 lsapas6 lsapuser 0 24 Jun 06:44 .myfileâ

2) Now shell will go to next directory i.e. â ..â (Parent directory-/tmp in ur case)
But there is no .myfile in /tmp.

So, It will go to next dir or file.
In case of dir it will search for .myfile & goes to next dir/file
In case of file it will matches it with .myfile,it equals to .myfile,it will return result as

So thatâ s ur second o/p â -rw-r----- 1 lsapas6 lsapuser 0 24 Jun 06:44 .myfileâ

So this is the reason why it is showing .myfile twice as in Present working directory & â .â means Current working directory.
And Present working directory & Current working directory is same i.e.
/tmp/test

Peter Nikitka
Honored Contributor

Re: Can you explain why it shows myfile twice in the last command?

Hi,

the simpiliest way to overcome this IMHO:

ls -ld .* | ...
ls -ald .* | ... (but this won't tell anything different)

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"