LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to shorten current path name (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-shorten-current-path-name-865779/)

cianoz 03-01-2011 12:34 PM

How to shorten current path name
 
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?

SL00b 03-01-2011 12:46 PM

You can use symbolic links. See the man pages for the ln command.

catkin 03-01-2011 01:13 PM

Does the PROMPT_DIRTRIM variable do what you want, for example export PROMPT_DIRTRIM=3

frieza 03-01-2011 01:17 PM

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

arizonagroovejet 03-01-2011 01:33 PM

Quote:

Originally Posted by frieza (Post 4275482)
yeah symbolic links are one of the more powerful features of Linux that are only half implimented in windows as 'shortcuts',

Not entirely true any more.

http://en.wikipedia.org/wiki/Symboli..._symbolic_link

I've never tried using them. They might suck.


I suspect PROMPT_DIRTRIM is the answer to the OP's query.

SL00b 03-01-2011 01:43 PM

I use symbolic links all the time. For instance, I might support a product that installs to a path like so:

/opt/company_name/product_brand/specific_product_name/version_number

And symlink it to: /productroot

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.

frieza 03-01-2011 01:53 PM

Quote:

Originally Posted by arizonagroovejet (Post 4275486)
Not entirely true any more.

http://en.wikipedia.org/wiki/Symboli..._symbolic_link

I've never tried using them. They might suck.


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 (Post 4275498)
I use symbolic links all the time. For instance, I might support a product that installs to a path like so:

/opt/company_name/product_brand/specific_product_name/version_number

And symlink it to: /productroot

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)

SvenJo 03-02-2011 01:03 AM

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.

cianoz 03-02-2011 10:03 AM

Thanks to everybody for your suggestions.
I'll give a try to your different solutions.

Sector11 03-02-2011 11:42 AM

Quote:

Originally Posted by SvenJo (Post 4275969)
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'm like you:
Code:

  14:39 /media/5
        $

Code:

export PS1="\n$grey  \$(date +%R) \w\n        $cyan\\$ $no_color"

turtlegeek 03-02-2011 09:13 PM

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.

padeen 03-11-2011 09:31 PM

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##*/} '


turtlegeek 03-12-2011 12:59 PM

I have also been frustrated by this feature.
 
Padeen, I was previously frustrated by this issue but resolved it differently.

Quote:

Originally Posted by padeen (Post 4287655)
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.
_

Fabio Paolini 03-12-2011 01:08 PM

Quote:

Originally Posted by catkin (Post 4275479)
Does the PROMPT_DIRTRIM variable do what you want, for example export PROMPT_DIRTRIM=3

I did not know that. It is really nice

SL00b 03-14-2011 07:50 AM

Quote:

Originally Posted by padeen (Post 4287655)
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

Honestly, I don't understand what's broken here. It looks like it's working as intended. I use the symlink in all my scripts, and they work just fine.

If you want to use the actual directory in your scripts instead of the symlink, then just use /var/www instead.


All times are GMT -5. The time now is 03:14 AM.