LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-07-2008, 12:11 PM   #1
harshanair17
LQ Newbie
 
Registered: Aug 2008
Posts: 3

Rep: Reputation: 0
Cool location of cd


hi can anyone pls tell me where is the binary for cd command stored
which cd doesnt help all it returns is the command prompt again
 
Old 08-07-2008, 12:39 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
cd is a bash "built-in" function. (It's also, I believe, "built-in" to all other shell interpreters.) So there is no "binary" for cd to be found.

If you're looking for the source code for cd, consider looking at the source for busybox which is, in my opinion, easier to follow then the bash source.

If, for some reason, you want to change what cd does, you could, I suppose, write your own cd and "alias" it to replace the built-in functionality. (Actually, I don't know if you can over-ride a bulit in with an alias since I've never tried, but it might work.)
 
Old 08-07-2008, 01:06 PM   #3
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
Quote:
Originally Posted by harshanair17 View Post
hi can anyone pls tell me where is the binary for cd command stored
which cd doesnt help all it returns is the command prompt again
CD as a stand alone command is supposed to take you back to the command prompt in your home directory. "cd /etc" would take you to the command prompt in the /etc directory and cd /home/you/Documents or cd !/documents would take you to the command prompt in the Documents directory under your home directory
 
Old 08-07-2008, 10:15 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by john test View Post
CD as a stand alone command is supposed to take you back to the command prompt in your home directory. "cd /etc" would take you to the command prompt in the /etc directory and cd /home/you/Documents or cd !/documents would take you to the command prompt in the Documents directory under your home directory
I beg to differ:

In Linux - or, for that matter, any computer system, there is no such thing as a "stand alone" command. There are "executable files," but they all require, at a minimum, an operating system to provide a context within which they can be run, and most also require a "shell" that manages the process of introducing the "executable" to the operating system as a candidate for execution.

Specifically, as I said above, cd is a command that is built into the shell interpreter to permit you to specify the file system "base location" to be used when referencing entries in the fie system. And, as I also said above, it has no separate executable file.

When you type, e.g., cd ~/Scripts, the shell program parses the tokens in that string, looks up the command token (cd) in it's internal table of commandqs, and then passes the remaining tokens from the input to it's internal "cd processing subroutine." If it fails to find the command in it's internal table of command, it then looks for an executable file with the specified name in the directories listed in $PATH.

On the other hand, I was not aware that cd with no arguments defaulted to cd ~/, so for that tip, thank you "john test".

Last edited by PTrenholme; 08-07-2008 at 10:17 PM.
 
Old 08-08-2008, 12:08 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Easy there. I believe "john test" meant "without arguments" when he said "as a stand alone command". A simple nomenclature problem.

Let's be clear about why the cd command must be built into the shell. We'll prove by counter-example.

If cd was not a built-in, the shell would fork/exec the "cd program" into a new (child) process. That process would then execute the chdir(2) system call. So that process' working directory would change. Then the process would exit. But no child process can affect the working directory of its parent process, or any other process for that matter. The shell's working directory would be exactly where it was before the exec'd cd command. So, what a useless program that would be!

Thus, cd must be built into the shell, implemented by a simple chdir(2) command. All sub-processes inherit the working directory of their parents, so any newly-created process has the new working directory.

Last edited by Mr. C.; 08-08-2008 at 12:19 PM.
 
Old 08-08-2008, 11:48 AM   #6
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
Quote:
Originally Posted by PTrenholme View Post
I beg to differ:


On the other hand, I was not aware that cd with no arguments defaulted to cd ~/, so for that tip, thank you "john test".
Pleased to have been able to add to your store of knowledge. It is good that you were able to work your through my bad choice of words and get the the meat which was that CD with no arguments or parameters functions to take your home directory
 
Old 08-10-2008, 01:51 PM   #7
harshanair17
LQ Newbie
 
Registered: Aug 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Specifically, as I said above, cd is a command that is built into the shell interpreter to permit you to specify the file system "base location" to be used when referencing entries in the fie system.

hey cud pls xplain dis part?
 
Old 08-10-2008, 02:45 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
The description is a little loose, not using standard terms, but the idea is correct.

Each process has a working directory, which is inherited from its parent process. The cd command changes the shell's working directory, and therefore any process started by the shell has the same working directory as the shell.

The working directory is used when relative path names are specified to various commands and system calls. Thus, if the shell's working directory is /usr/local/bin, and you type ./top, the "./" relative path component means from the current working directory. This ./top would be referring to the full path /usr/local/bin/top. The same holds true for ".." where ../../bin/mail would refer to /usr/bin/mail.

See also: Week 2 Notes, "Files, Directories, Permissions and Ownership" in Coursework at http://cis68a.mikecappella.com/ for more explanation.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GD location tbl_123 Linux - Newbie 2 11-15-2005 06:00 PM
apache: location in location eantoranz Linux - Networking 2 08-08-2005 09:37 PM
CD to location discourse Linux - Newbie 4 12-08-2004 12:20 AM
location, location, location! mermxx LQ Suggestions & Feedback 9 09-25-2004 03:08 AM
ln location john8675309 Linux - General 3 03-12-2004 11:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:33 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