Execute file without ./

showbix used Ask the Experts™
Question 1
I have a directory called MYPROJ. Underneath it are 10 sub-directories. My problem is everytime I want to execute a file in any of the directory under MYPROJ, I have to type ./filename. How do I avoid typing ./ for all the 10 directories under MYPROJ.

Question 2
I have a file called amc.log. I want to delete the content of the file automatically every 3 days. How do I do that? Is there cron for that purpose?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
  add MYPROJ  and MYPROJ/sub1 and ... MYPROJ/sub10 to your PATH environment variable, like:

   PATH="$PATH":MYPROJ:MYPROJ/sub1:MYPROJ/sub2: ... :MYPROJ/sub1
or in csh:

   setenv PATH "$PATH":MYPROJ:MYPROJ/sub1:MYPROJ/sub2: ... :MYPROJ/sub1


Hi ahoffmann.

Question 1 : Any short way instead of defining one directory after another?

Question 2 : Any idea?


for file in *
   if [ -d $file ]
       export PATH=$PATH:$file

You may need to also test that the directory is not "." or ".."

if you put this in a script you would need to execute it like this :-

. ./env_script to include the definition of PATH in the current shell.

2) Yes cron could be used for this.  THe only problem is you want to delete every 3 days I can't think of an easy way of doing this other than within a script.  Cron defines five time fields for controlling when the job is run :-

day of month
day of week

so if you ignored Sunday for example you could execute your delete on monday and thursday using

0 0 * * 1,4 rm -f /path/to/log/amc.log.

This would remove the file at midnight on monday and thursday.  If it has to be strictly every three days then you would need to add the day logic to a script and execute the script every day.  The script can then decide itself whether or not to delete the file.  The crontab enrty would then be :-

0 0 * * * /path/to/my_delete_script.sh

Hope this helps
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

> Question 1 : Any short way instead of defining one directory after another?

put your programs in one directory, or use a loop as suggested

> Q1:
 cron, see example above

You can add a "." to your PATH environment variable.

try this test:
export PATH=$PATH:.

cd some-sub-dir

if both executions of filename worked fine so you can put the line "export PATH=$PATH:." into your .profile

good luck
if you have to type ./.... to execute your file, it means you are in the directory contening your executable, so you must include the current directory into your PATH variable, as said by santunes;
but it's not necessary to put the point: an empty path means also here.
so, PATH=${PATH}:
will include as last path to search for executable, the current path you are when you type the command

2°: cron is capable to execute tasks at hour, day, month, year as said garreth
the most simple is to program with cron a task every day to execute a program deleting your file if the file il older than 3 days
see the find command to see how to find a file older than... and delete it,
write this command into a batch file and execute it each day by cron
2) Instead of deleting the log, use cron to "age" it on a daily basis, so you always have a couple of days history, e.g.

cp amc.log.old amv.log.older
cp amc.log amc.log.old  # This may barf if amc.log is constantly changing
cat /dev/null > amc.log

Plus this is much simpler than doing date calculations...
No comment has been added lately, so it's time to clean up this Topic Area.
I will leave a recommendation for this question in the Cleanup topic area as follows:

- Answered by bedot

Please leave any comments here within the next 7 days


Cleanup Volunteer
per recommendation

Community Support Moderator @Experts Exchange

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial