LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Blogs > druuna
User Name
Password

Notices

Rate this Entry

Bash moves in mysterious ways

Posted 12-04-2011 at 01:52 PM by druuna
Updated 12-23-2011 at 03:35 PM by druuna

Playing with the mv command I ran into this:

From a safe location create 2 Directories, create some files in one of the directories and enter that directory:
Code:
$ mkdir Temp_0{1,2}
$ ls -la
total 40
drwxr-x---  4 druuna internet  4096 Dec  4 20:16 .
drwxr-x--- 38 druuna internet 28672 Dec  4 20:16 ..
drwxr-x---  2 druuna internet  4096 Dec  4 20:16 Temp_01
drwxr-x---  2 druuna internet  4096 Dec  4 20:16 Temp_02

$ touch Temp_01/{foo,bar}
$ ls -la *
Temp_01:
total 8
drwxr-x--- 2 druuna internet 4096 Dec  4 20:17 .
drwxr-x--- 4 druuna internet 4096 Dec  4 20:16 ..
-rw-r----- 1 druuna internet    0 Dec  4 20:17 bar
-rw-r----- 1 druuna internet    0 Dec  4 20:17 foo

Temp_02:
total 8
drwxr-x--- 2 druuna internet 4096 Dec  4 20:16 .
drwxr-x--- 4 druuna internet 4096 Dec  4 20:16 ..

$ cd Temp_01/
$ pwd ; /bin/pwd
/home/druuna/Temp/Temp_01
/home/druuna/Temp/Temp_01

~/Temp/Temp_01 $ ls -la
total 8
drwxr-x--- 2 druuna internet 4096 Dec  4 20:17 .
drwxr-x--- 4 druuna internet 4096 Dec  4 20:16 ..
-rw-r----- 1 druuna internet    0 Dec  4 20:17 bar
-rw-r----- 1 druuna internet    0 Dec  4 20:17 foo
Nothing special this far all is where you expect it to be. Now the fun part.

Move the directory you are standing in to the other directory:
Code:
$ pwd ; /bin/pwd
/home/druuna/Temp/Temp_01
/home/druuna/Temp/Temp_01

$ mv ../Temp_01 ../Temp_02
No errors. But which directory are you presently in?
Code:
 $ pwd ; /bin/pwd
/home/druuna/Temp/Temp_01
/home/druuna/Temp/Temp_02/Temp_01
The build-in places you at /home/druuna/Temp/Temp_01. Sounds reasonable, that is where you cd-ed into.
But the binary version places you in home/druuna/Temp/Temp_02/Temp_01. Also sounds reasonable, that is where you moved yourself to.... Hmmmm.

You are actually standing in /home/druuna/Temp/Temp_02/Temp_01:
Code:
$ cd ..
$ pwd ; /bin/pwd
/home/druuna/Temp/Temp_02
/home/druuna/Temp/Temp_02

$ ls -la
total 12
drwxr-x--- 3 druuna internet 4096 Dec  4 20:19 .
drwxr-x--- 3 druuna internet 4096 Dec  4 20:19 ..
drwxr-x--- 2 druuna internet 4096 Dec  4 20:17 Temp_01

$ ls -l Temp_01/
total 0
-rw-r----- 1 druuna internet 0 Dec  4 20:17 bar
-rw-r----- 1 druuna internet 0 Dec  4 20:17 foo
Like I said: Bash moves in mysterious ways

Here's another example where pwd (the build-in) is "wrong":
Code:
$ mkdir Temp
$ cd Temp/
$ touch foo
$ ls -l
total 0
-rw-r----- 1 druuna internet 0 Dec  4 20:49 foo

$ rm -rf ../Temp/

$ pwd
/home/druuna/Temp/Temp

$ /bin/pwd
/bin/pwd: couldn't find directory entry in `..' with matching i-node

$ ls -l
total 0

$ cd ..
$ ls -la
total 32
drwxr-x---  2 druuna internet  4096 Dec  4 20:49 .
drwxr-x--- 38 druuna internet 28672 Dec  4 20:16 ..
The build-in pwd can be wrong and I would advise you to use /bin/pwd instead.
Posted in General
Views 697 Comments 0
« Prev     Main     Next »

  



All times are GMT -5. The time now is 02:57 AM.

Main Menu

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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration