Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello
If i am using a terminal window (shell) and I am on a deep point of a folder tree it becomes difficult to read what i write, as well as the entire content of the window.
Is there a way to shorten the name of the current path in a shell / terminal?
I know that aliases can be used for commands, does it exist anything similar for paths?
Click here to see the post LQ members have rated as the most helpful post in this thread.
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233
Rep:
yeah symbolic links are one of the more powerful features of Linux that are only half implimented in windows as 'shortcuts', i've even used symbolic links to put modules for a web script on an nfs server (yes i could have mounted the nfs server as the modules directory, but with symlinks I could use the same module set for multiple instances of said script in multiple document roots)
another way would be to simply edit the PS1 value in .bashrc in your home directory
Quote:
Originally Posted by http://linuxers.org/howto/how-change-command-prompt-using-ps1-variable
\t - time
\d - date
\n - newline
\s - Shell name
\W - The current working directory
\w - The full path of the current working directory.
\u - The user name
\h - Hostname
\# - The command number of this command.
\! - The history number of the current command
shows a list of tokens (escape strings?) that can be used in a bash prompt, though they seem to have forgotten \$ which shows a # for superuser and a $ for regular users
eg
Code:
export PS1='[\u@\h \W]\$ '
will only show the top level directory
as opposed to
Code:
export PS1='[\u@\h \w]\$ '
which shows the whole path
though a symlink has the added bonus of making navigating to the directory quicker
Not only does it make it easier for me to navigate, but later on while I'm transitioning between two different versions, it means I don't have to change any of my automation scripts or configuration files. I simply manipulate the symlink.
I suspect PROMPT_DIRTRIM is the answer to the OP's query.
Quote:
Originally Posted by http://en.wikipedia.org/wiki/Symbolic_link#Windows_7_.26_Vista_symbolic_link
Microsoft aimed for Vista's symbolic links to "function just like UNIX links".[4] However, the implementation varies from Unix symbolic links in several ways. For example, Vista users must manually indicate when creating a symbolic link whether it is a file or a directory.[5] Vista has a limit of 31 symbolic links in a single path.[6] Only users with the new Create Symbolic Link privilege, which only administrators have by default, can create symbolic links.[7] If this is not the desired behavior, it must be changed in the Local Security Policy management console.
yep but still nowhere near as functional as UNIX style symlinks
Quote:
Originally Posted by SL00b
I use symbolic links all the time. For instance, I might support a product that installs to a path like so:
Not only does it make it easier for me to navigate, but later on while I'm transitioning between two different versions, it means I don't have to change any of my automation scripts or configuration files. I simply manipulate the symlink.
But to each their own.
yep, i believe this is the default behavior of firefox now, that is to put firefox plugins such as the flash player in a separate directory and then point to it with a symlink, that way the plugins survive a transition to newer versions of firefox (at least it does that in fedora and ubuntu)
I always end my PS1 with \n\$ '. This give me a full line for my command and can keep a full path in the prompt. It takes two lines, but this is better than having command lines split over two lines.
I always end my PS1 with \n\$ '. This give me a full line for my command and can keep a full path in the prompt. It takes two lines, but this is better than having command lines split over two lines.
All the solutions proposed above are very good and usable for a prompt and
some are also good for general use.
For those simply looking for a solution that means less to type (as opposed to shortening the prompt),
two other easily used possibilities remain:
1. If your execution path lies in the same volume, use a hard link:
Code:
ln /myvolume/my_very_long_path_including_all_intervening_directories_and_myfile /myvolume/myfile
cd /myvolume/myfile
This solution remains after logging out but could cause problems with some tree search algorithms.
2. Create and export a shell variable:
Code:
# export if necessary:
export MYFILE
MYFILE=/myvolume/my_very_long_path_including_all_intervening_directories_and_myfile
cd $MYFILE
Solution 2 would go away after logging out but could be made "permanent" by placing it in
a file included in a shell invocation. If placed in an invocation file, the name would be
better chosen as a unique name, unlikely to be chosen by other shells.
I don't like symlinks for this purpose. They don't change the PWD correctly:
Code:
ln -s /var/www /home/user/www
cd ~/www
echo $PWD
/home/user/www
which breaks scripts
This function shortens the path to display only the next 2 higher dirs. Put it in your .bashrc and change the PS1 to include any other things you want.
Code:
# generate shortened pwd entry similar to csh's, for the PS1 prompt
function PWD {
tmp=${PWD%/*/*};
[ ${#tmp} -gt 0 -a "$tmp" != "$PWD" ] && echo ".../${PWD:${#tmp}+1}" || echo $PWD;
}
PS1='${PWD##*/} '
Padeen, I was previously frustrated by this issue but resolved it differently.
Quote:
Originally Posted by padeen
I don't like symlinks for this purpose. They don't change the PWD correctly:
Code:
ln -s /var/www /home/user/www
cd ~/www
echo $PWD
/home/user/www
which breaks scripts
This function shortens the path to display only the next 2 higher dirs. Put it in your .bashrc and change the PS1 to include any other things you want.
Code:
# generate shortened pwd entry similar to csh's, for the PS1 prompt
function PWD {
tmp=${PWD%/*/*};
[ ${#tmp} -gt 0 -a "$tmp" != "$PWD" ] && echo ".../${PWD:${#tmp}+1}" || echo $PWD;
}
PS1='${PWD##*/} '
I understand why you don't like the symlink in this case. However, bash is designed to pay attention to symlinks in this manner. Bash does work correctly.
Had you entered
Code:
cd /var/www
the $PWD variable would have reflected that. The pwd command also reflects the symlink in this manner. My personal response to this feature has been to use absolute path names in my scripts (also a safety bonus) and regard most symlinks I create as permanent paths. I now consider this problem my short-sighted error. If I had thought there were advantages to your solution, I might have implemented it long ago.
I haven't tested your solution but prefer to keep my code simple. I don't like changing the meaning of $PWD to be different from what bash coders or programmers expect. If I were to use such a redefinition of $PWD, that, too, could break some scripts and if the script were lengthy and not written by me, I could spend a lot of time "fixing" it. By the way, if you insist on using your change to $PWD, you might also consider making an alias for the pwd command.
Some "bin" directory commands are actually scripts. On my Mac G5 running the Tiger (10.4.11) OS, many /usr/bin commands are actually recognized as "Bourne" scripts by the "file" command but begin with "#!/bin/sh" which will default to bash (Bourne-Again-SHell) anyway.
_
Last edited by turtlegeek; 03-12-2011 at 01:00 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.