LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-15-2010, 05:25 AM   #1
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Rep: Reputation: 15
Path shortcuts in CentOS


Is there a way to set a short cut so that you don't have to type out the full path to directories in CentOS? For example instead of typing out /var/log to get to log files, can you just type logs and have the OS know to cd to /var/log automatically?

Thanks
 
Old 09-15-2010, 05:34 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
In your .bashrc or .bash_profile (whichever you use at the moment), define an alias:
Code:
alias logs='cd /var/log'
Then re-source your .bashrc to apply the changes
Code:
. ~/.bashrc
or log out and back in again. I'm assuming you're using bash.
 
Old 09-15-2010, 06:27 AM   #3
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
Thanks pwc, which should be used, bash_profile or bashrc? And what is the way for all users to use this shortcut? Also, can you please provide a solutions for the tcsh equivelant?

Many thanks!
 
Old 09-15-2010, 06:33 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
tcsh? *shudder*

I think the syntax is similar for tcsh:
Code:
alias logs 'cd /var/log'
If you're using tcsh, then you're not going to want to put these aliases in .bashrc or .bash_profile. For tcsh, the user-specific settings can be added to ~/.tcshrc or ~/.cshrc; global settings are made in /etc/csh.cshrc. See man tcsh:
Quote:
Originally Posted by man tcsh
Startup and shutdown
A login shell begins by executing commands from the system files
/etc/csh.cshrc and /etc/csh.login. It then executes commands from
files in the user's home directory: first ~/.tcshrc (+) or, if
~/.tcshrc is not found, ~/.cshrc, then ~/.history (or the value of the
histfile shell variable), then ~/.login, and finally ~/.cshdirs (or
the value of the dirsfile shell variable) (+). The shell may read
/etc/csh.login before instead of after /etc/csh.cshrc, and ~/.login
before instead of after ~/.tcshrc or ~/.cshrc and ~/.history, if so
compiled; see the version shell variable. (+)
 
Old 09-15-2010, 07:10 AM   #5
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by investmentbnker75 View Post
Is there a way to set a short cut so that you don't have to type out the full path to directories in CentOS? For example instead of typing out /var/log to get to log files, can you just type logs and have the OS know to cd to /var/log automatically?
Not in CentOS specifically, but in bash in general, this feature is controlled by CDPATH variable.

a CDPATH variable set as this:
Code:
CDPATH=.:~:/export/main/fileserver:/var/lib/samba:/var/lib/samba/netlogon:/opt:/var/log
will allow you do change to any directories that have the parents directories in the list above.

so, "cd Documents" will change to "/export/main/fileserver/Documents" (or any directory bellow of /export/main/fileserver)

"cd profiles/JohnDoe" will change directory to "/var/lib/samba/netlogon/profiles/JohnDoe"

This is much better than creating several aliases, one for each directory, specially if you have many of then.

PS: On tcsh the variable which has the same meaning is just "cdpath"

Last edited by marozsas; 09-15-2010 at 07:14 AM.
 
Old 09-15-2010, 07:31 AM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by marozsas View Post
This is much better than creating several aliases, one for each directory, specially if you have many of then.
Well, you learn something every day. This seems more elegant if you have many directories you'd like to easily cd to. Thanks marozsas.
 
Old 09-15-2010, 10:35 AM   #7
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
Thanks marozsas, but im a little lost about your helpful response. If i want to type cd logs and have it go to /var/log would be CDPATH look like this?

CDPATH=.:~:/logs:/var/log
 
Old 09-15-2010, 12:22 PM   #8
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
hi investmentbnker75,

For just a single (or a few) folder like /var/log, using CDPATH is not the better option. It is better to create an alias, just like pwc101 told you;

Even so, for this single case, in bash: "CDPATH=/var" (in tcsh: "set cdpath=/var") would do the trick, and "cd log" will change to "/var/log" and "cd named" will change to "/var/named" and "cd spool" to "/var/spool".

Things become more complicated as soon you start to create several aliases and at this point, CDPATH makes more sense.

Last edited by marozsas; 09-15-2010 at 12:25 PM.
 
  


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
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 05:48 PM
CentOS - gcc path question rheng Linux - Newbie 3 07-29-2008 01:13 PM
Cannot correctly set up the PATH environment variable Centos dralexpe Linux - Software 7 05-30-2008 11:23 PM
Getting target path of Windows shortcuts in Java or batch Slokunshialgo Programming 1 01-12-2008 03:58 PM
CentOS 4.4 saving PATH, CLASSPATH variables Thor Red Hat 2 11-25-2006 08:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:59 PM.

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