LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Path shortcuts in CentOS (https://www.linuxquestions.org/questions/linux-server-73/path-shortcuts-in-centos-832278/)

investmentbnker75 09-15-2010 05:25 AM

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

pwc101 09-15-2010 05:34 AM

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.

investmentbnker75 09-15-2010 06:27 AM

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!

pwc101 09-15-2010 06:33 AM

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. (+)


marozsas 09-15-2010 07:10 AM

Quote:

Originally Posted by investmentbnker75 (Post 4098067)
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"

pwc101 09-15-2010 07:31 AM

Quote:

Originally Posted by marozsas (Post 4098147)
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.

investmentbnker75 09-15-2010 10:35 AM

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

marozsas 09-15-2010 12:22 PM

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.


All times are GMT -5. The time now is 06:11 AM.