LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-07-2013, 06:13 PM   #1
Radikll
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Rep: Reputation: 0
'ls' displays different results when in directory


How can this ever happen?

Code:
jon@ben:~/Development/www$ ls -l ../random-checkout/
total 28
drwxrwxr-x 15 jon jon 4096 May  7 17:02 application
-rwxrwxr-x  1 jon jon 6357 May  7 17:02 index.php
-rwxrwxr-x  1 jon jon 2496 May  7 17:02 license.txt
-rw-rw-r--  1 jon jon  398 May  7 17:02 README.md
drwxrwxr-x  8 jon jon 4096 May  7 17:02 system
drwxrwxr-x 11 jon jon 4096 May  7 17:02 user_guide
jon@ben:~/Development/www$ cd ../random-checkout/
jon@ben:~/Development/random-checkout$ ls -l
total 20
drwxr-xr-x  6 jon jon 4096 May  7 17:45 auth_example
drwxr-xr-x 12 jon jon 4096 May  7 15:32 CodeIgniter-Ion-Auth
drwxrwxr-x 10 jon jon 4096 Mar 27 16:24 m3-stream-read-only
drwxrwxr-x  4 jon jon 4096 Apr  2 18:03 trunk
drwxrwxr-x  6 jon jon 4096 Mar 27 18:07 xuggle-read-only
jon@ben:~/Development/random-checkout$
I could not believe my eyes when I saw this! The directory somehow contains two different sets of files and folders at the same time!!

It happened when I ran this command.
Code:
jon@ben:~/Development/www$ mv auth_example ../random-checkout/
mv: cannot move `auth_example' to `../random-checkout/': Permission denied
jon@ben:~/Development/www$ sudo mv auth_example ../random-checkout/
[sudo] password for jon:
Trouble shooting info:
Code:
jon@ben:~/Development/random-checkout$ df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/sda1                 70G   34G   33G  51% /
udev                     3.9G  4.0K  3.9G   1% /dev
tmpfs                    1.6G  1.4M  1.6G   1% /run
none                     5.0M     0  5.0M   0% /run/lock
none                     3.9G  5.6M  3.9G   1% /run/shm
none                     100M     0  100M   0% /run/user
/home/jon/.Private        70G   34G   33G  51% /home/jon
...
jon@ben:~/Development/random-checkout$ sudo parted -l
Model: ATA ST9750420AS (scsi)
Disk /dev/sda: 750GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  76.0GB  76.0GB  primary  ext4         boot
...
jon@ben:~/Development/random-checkout$ uname -a
Linux ben 3.5.0-27-generic #46-Ubuntu SMP Mon Mar 25 19:58:17 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
I know I can just remove the directory and the problem will be solved, but I just really want to know why and how this happened. If you can't tell I know my way around linux and the command-line.
 
Old 05-07-2013, 06:22 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Sounds like your PWD is inside a symlink.

Code:
ls ..
will print the results from the parent directory of the target of the symlink

Code:
cd ..; ls
will print the results from the parent directory of the symlink itself.

For example, take the following
Code:
$ mkdir -p dir1/dir2/dir3/dir4
$ cd dir1/dir2/dir3/dir4
$ ln -s ../../../dir2/ dir5
$ cd dir5
$ pwd
/home/user/dir1/dir2/dir3/dir4/dir5
$ ls -l ..
total 4
drwxrwxr-x 3 user user 4096 2013-05-07 17:19 dir2
$ cd ..; pwd; ls -l
/home/user/dir1/dir2/dir3/dir4
total 0
lrwxrwxrwx 1 user user 14 2013-05-07 17:20 dir5 -> ../../../dir2/
$
The contents of "ls -l .." is dir2, the contents of "cd ..; ls -l" is the dir5 symlink. Just a difference in how ls and cd handle ".." from inside a symlink.

Of course this begs the question, "why do cd and ls treat ".." differently when inside a symlink?" Which then begs the more philosophical question, "what is ".." when you're inside a symlink"?

Last edited by suicidaleggroll; 05-07-2013 at 06:39 PM.
 
1 members found this post helpful.
Old 05-07-2013, 07:31 PM   #3
Radikll
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
OH! yep that was it. Pretty odd (and confusing) that cd handles ".." differently from ls and mv. I'll have to keep that in mind.

Code:
jon@ben:~/Development/www$ ls /var
backups  cache  lib  local  lock  log  mail  opt  random-checkout  run  spool  tmp  www
jon@ben:~/Development/www$ ls ..
backups  cache  lib  local  lock  log  mail  opt  random-checkout  run  spool  tmp  www
Thanks for the solution!

edit:
They probably designed cd to be a special case, since it'd be really annoying to cd into a symlink and then have to use the absolute path to get back.

Code:
jon@ben:~/Development/www$ rm -rf ../random-checkout/
rm: cannot remove `../random-checkout': Permission denied
jon@ben:~/Development/www$ sudo rm -rf ../random-checkout/
[sudo] password for jon: 
jon@ben:~/Development/www$ ls ..
backups  cache  lib  local  lock  log  mail  opt  run  spool  tmp  www
rm functions like ls and mv.

Last edited by Radikll; 05-07-2013 at 07:42 PM.
 
Old 05-07-2013, 08:58 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
It is only the shell's cd builtin that, by default, follows back along any symlinks that were used to get to the CWD. Let's say that "/etc/somewhere" is actually a symlink to a directory "/opt/somepkg/etc/somewhere". If you had no reason to suspect that a symlink was involved, then doing "cd /etc/somewhere" and then "cd ..", you would expect to end up in "/etc", and that is what the cd builtin would do. Finding yourself instead in "/opt/somepkg/etc" would be quite a surprise.

You can make the shell's cd follow the physical directory structure by adding the "-P" option to the cd command. You can make that effective for future cd commands by setting the shell's "-P" option.

External commands, like rm and mv, have no way to know how you got to the CWD, so they have no choice but to follow the physical structure.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kerberos displays username in cleartext while logging to Active Directory, is it ok? patmut Linux - Security 2 12-03-2012 09:42 AM
gprof results displays always 0.0 for function call time udaykumar84 Programming 1 04-26-2012 03:53 AM
[SOLVED] su results in Cannot execute bash: No such file or directory gujedan Linux - Newbie 1 02-13-2011 01:24 PM
Dual monitors, separate X displays; want firefox in both displays bforbes Linux - Desktop 7 10-15-2008 09:26 PM
find command displays unexpected results helptonewbie Linux - Newbie 5 08-12-2008 02:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration