LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 06-17-2007, 07:05 AM   #1
benchmarkman
LQ Newbie
 
Registered: Feb 2006
Posts: 18

Rep: Reputation: 0
Using ./ to execute a file in current directory


So I am new to Linux and a co-worker and I were discussing using ./ so that Linux knows to look in the current directory. My co-worker explained that you must use ./ to take away ambiguity and that is a better method than Windows. I feel this is pretty stupid because Linux should assume if I have navigated to a directory that it should know that I want to execute the file that is in that directory. Is there another reason for this? If not can you give me a good reason why I should have to use ./ ? What are your thoughts on this?
 
Old 06-17-2007, 07:26 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,398

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
without wishing to sound too ridiculous, about the best reason is that's the way it is. new users seem to have this salmon like urge to fight against anything that doesn't immediately strike them as obvious. this makes life very hard for them, whereas if they initially accepted these sorts of things, and questioned them later, that later stage either never needs to arise or is a lot more valid when it does.

as for more pratical reasons, executable programs live in /usr/bin, /bin etc... and a command should be generally predictable wherever you run it from. if you stick your current directory into the path variable, then running a command may be different if you were to have your own "versions" of a command. e.g. you have /home/user/rm which is a test script you're messing about with. you run "rm file.txt" whilst in /tmp and the file /tmp/file.txt is deleted. you run that in your home directory and file.txt is sent as an email attachment to your ex girlfriend and your boss... stupid example of course, but it's much better to have this consistency, even if it is more long winded.

as above... don't fight it. if nothing else, it's very cliched!
 
Old 06-17-2007, 08:25 AM   #3
ArcLinux
Member
 
Registered: Apr 2005
Location: Fargo, ND
Distribution: Slackware, CentOS
Posts: 87

Rep: Reputation: 20
Quote:
Originally Posted by benchmarkman
My co-worker explained that you must use ./ to take away ambiguity
You may want to have another listen to your co-worker. He/she is correct is that it helps with ambiguity.

example. You have "executable" located at (/usr/bin/executable).

In /tmp/testEXE dir you have a test "executable" file. You want to compare how they both react when used.
First you test the original:
:/tmp/testEXE>executable <input>
BLAH BLAH ... something happens
Next test your new file:
:/tmp/testEXE>./executable <input>
blah blah.. something else happens

Now isn't that easier then comparing it this way:
:/tmp/testEXE>/usr/bin/executable <input> {file in $PATH but local file would mask it if in current dir}
:/tmp/testEXE>executable <input> {local file}

Last edited by ArcLinux; 06-17-2007 at 08:26 AM.
 
Old 06-17-2007, 09:32 AM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
It's true that you should type ./cmd to access an executable in the current directory (as acid_kewpie said, the primary rationale is a slight security issue), but you don't *have* to. If you wish, you can append the current directory to your PATH (you can prepend it but this is even less wise). But what kind of a filesystem mess are you living in where you need that? Just create a personal bin directory and put *that* on your path and keep your shell scripts and whatnot there. Saves on './' and 'cd' typing.
 
Old 06-17-2007, 04:00 PM   #5
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
Quote:
Originally Posted by benchmarkman
I feel this is pretty stupid because Linux should assume if I have navigated to a directory that it should know that I want to execute the file that is in that directory.
So,

1. you think you know better than experts with 30 years of unix experience?

2. you expect a piece of software to be able to read your, ahem, "mind"?

Okaaaaaaaaaay.
 
Old 06-17-2007, 04:16 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by benchmarkman
I feel this is pretty stupid because Linux should assume if I have navigated to a directory that it should know that I want to execute the file that is in that directory. Is there another reason for this? If not can you give me a good reason why I should have to use ./ ? What are your thoughts on this?
It is really the opposite of your intuition....Linux looks in the PATH variable to see where executable files are located. You really do not want all executables located in your home directory. Look in /bin or /sbin or /usr/bin to see why.

When you DO have an executable in a directory not in PATH, then you have to disambiguate.
 
Old 06-17-2007, 05:09 PM   #7
benchmarkman
LQ Newbie
 
Registered: Feb 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dawkcid
So,

1. you think you know better than experts with 30 years of unix experience?

2. you expect a piece of software to be able to read your, ahem, "mind"?

Okaaaaaaaaaay.
I never said I knew better. Why are you taking such a smart @ss attitude?

To answer your question yes, I belive a computer should read my mind. Isn't it pretty obvious that if I navigate to a certain directory I want to run the file in that directory?? Also if people don't ask question and "accept" something just because it always been done that way then humans will never advance.

I do understand the security issue and that does make sense. Thanks for shedding some light on this.

Last edited by benchmarkman; 06-17-2007 at 05:17 PM.
 
Old 06-17-2007, 07:43 PM   #8
Lux Perpetua
LQ Newbie
 
Registered: Aug 2005
Location: Local Group
Distribution: Ubuntu Linux
Posts: 15

Rep: Reputation: 0
Quote:
Originally Posted by benchmarkman
To answer your question yes, I belive a computer should read my mind. Isn't it pretty obvious that if I navigate to a certain directory I want to run the file in that directory??
Why should that be obvious? Wanting to execute an executable located in a directory is not the most common reason for most users to cd into a directory, which is to operate on things in that directory using standard commands. Executing files in random directories is something developers do, and we know how to do it, either by prepending "./" to the command or by appending . to the path once and for all.
 
Old 06-17-2007, 08:55 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by Lux Perpetua
Why should that be obvious? Wanting to execute an executable located in a directory is not the most common reason for most users to cd into a directory, which is to operate on things in that directory using standard commands. Executing files in random directories is something developers do, and we know how to do it, either by prepending "./" to the command or by appending . to the path once and for all.
Amen....as a normal practice, you do NOT want all you executables to be in regularly used directories. That is why there is a PATH variable. Once that design decision is made, then there must be a means of executing a file in a directory that is not in PATH. I'm no a programmer, but I'd be willing to bet that tracking your movements thru the filesystem and somehow anticipating where you would want to run something----would be a bit of a chore....

Learning to type ./filename will surely be the easiest course of action.......
 
Old 06-17-2007, 10:49 PM   #10
benchmarkman
LQ Newbie
 
Registered: Feb 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany
Amen....as a normal practice, you do NOT want all you executables to be in regularly used directories. That is why there is a PATH variable. Once that design decision is made, then there must be a means of executing a file in a directory that is not in PATH. I'm no a programmer, but I'd be willing to bet that tracking your movements thru the filesystem and somehow anticipating where you would want to run something----would be a bit of a chore....

Learning to type ./filename will surely be the easiest course of action.......
I guess I'm kind of lost, let me give an example. I install program x and install it in directory y. If I go to the directory y and type x, why should it not assume I want to run the file x in the current directory? What would be the point of me navigating to directory y if I didn't want to use that file?
 
Old 06-17-2007, 11:31 PM   #11
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
You're asking the wrong question - reverse it: what is the point of navigating to the directory if it's already on your path?
 
Old 06-18-2007, 12:02 AM   #12
Lux Perpetua
LQ Newbie
 
Registered: Aug 2005
Location: Local Group
Distribution: Ubuntu Linux
Posts: 15

Rep: Reputation: 0
Quote:
Originally Posted by benchmarkman
I guess I'm kind of lost, let me give an example. I install program x and install it in directory y. If I go to the directory y and type x, why should it not assume I want to run the file x in the current directory? What would be the point of me navigating to directory y if I didn't want to use that file?
What you describe is not standard practice. When you install a program, it should normally go in one of the standard program directories, maybe like /usr/local/bin. The standard directories are probably part of your path by default, so you can then run the program simply by invoking its name. The point of working in a directory is usually to examine or modify files in that directory, not to execute programs contained therein.

An example of standard usage: I have a directory, ~/documents/pictures, with a bunch of pictures, say 1.jpg, 2.jpg, .... To display those pictures, I can switch to that directory (cd ~/documents/pictures) and display them with, for example, eog 1.jpg, eog 2.jpg, .... If I weren't in that directory, I would have to qualify the filenames, like eog ~/documents/pictures/1.jpg, so it's more convenient to be in the directory.
 
Old 06-18-2007, 05:44 AM   #13
benchmarkman
LQ Newbie
 
Registered: Feb 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Lux Perpetua
What you describe is not standard practice. When you install a program, it should normally go in one of the standard program directories, maybe like /usr/local/bin. The standard directories are probably part of your path by default, so you can then run the program simply by invoking its name. The point of working in a directory is usually to examine or modify files in that directory, not to execute programs contained therein.

An example of standard usage: I have a directory, ~/documents/pictures, with a bunch of pictures, say 1.jpg, 2.jpg, .... To display those pictures, I can switch to that directory (cd ~/documents/pictures) and display them with, for example, eog 1.jpg, eog 2.jpg, .... If I weren't in that directory, I would have to qualify the filenames, like eog ~/documents/pictures/1.jpg, so it's more convenient to be in the directory.
So if I understand you correctly, unlike Windows, all your installed executable files go into a single directory?

Also what is eog?
 
Old 06-18-2007, 06:13 AM   #14
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,398

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
not a single directory, but generally few, based on what their role is... /bin, /sbin, /usr/bin etc... the closest you'll get to "program files" is /opt/ where specific large programs will be more privately placed away from the generic infrastructure. most things are in the usual places though
 
Old 06-18-2007, 07:38 AM   #15
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by benchmarkman
So if I understand you correctly, unlike Windows, all your installed executable files go into a single directory?

Also what is eog?
Eye of Gnome--a picture viewer.

To the main topic:
No, as with Windows, you can put executable files where-ever you want. There are a few conventions, but they are not requirements.

The whole point of this discussion is that Linux has a convention in which most executables are in just a few directories. It has a standard mechanism for finding the executables---ie the PATH variable. This is why you don't type /usr/bin/myprogram every time you want to run something. (Or, more absurdly, /bin/ls every time you want to list the current directory.)

The beauty of Linux is you can set it up any way you want....e.g.:
1.Put ALL the executables in your home directory (get rid of PATH). Now just remember anytime you are NOT at home, type ~/program to run something.

2.Get rid of PATH and just remember where all executables are so you can type the right path.

3.Or some other equally problematic approach....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to untar a file in another directory other then current directory. dr_zayus69 Linux - Software 8 05-16-2007 11:01 AM
bash scripting help want to execute external function on current item. Benanzo Linux - Software 3 02-03-2007 02:45 PM
[SOLVED] Installind Slamd64-current. "cannot execute binary file" message about installpkg BrutalMusic Slackware - Installation 1 05-11-2006 09:35 AM
no such file or directory exists when trying to execute a exe ryedawg Linux - Software 5 12-05-2005 05:42 AM
"Cannot execute KDE: No such file or directory" abboq Linux - Software 1 09-27-2003 08:08 PM


All times are GMT -5. The time now is 05:48 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 Google+: linuxquestions
Open Source Consulting | Domain Registration