LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-01-2011, 12:34 PM   #1
cianoz
LQ Newbie
 
Registered: Jul 2009
Location: Italy
Posts: 19

Rep: Reputation: 0
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?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-01-2011, 12:46 PM   #2
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
You can use symbolic links. See the man pages for the ln command.
 
1 members found this post helpful.
Old 03-01-2011, 01:13 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Does the PROMPT_DIRTRIM variable do what you want, for example export PROMPT_DIRTRIM=3
 
2 members found this post helpful.
Old 03-01-2011, 01:17 PM   #4
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
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

Last edited by frieza; 03-01-2011 at 01:26 PM.
 
1 members found this post helpful.
Old 03-01-2011, 01:33 PM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
Quote:
Originally Posted by frieza View Post
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.
 
Old 03-01-2011, 01:43 PM   #6
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
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.
 
Old 03-01-2011, 01:53 PM   #7
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
Quote:
Originally Posted by arizonagroovejet View Post
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 View Post
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)

Last edited by frieza; 03-01-2011 at 01:55 PM.
 
Old 03-02-2011, 01:03 AM   #8
SvenJo
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 03-02-2011, 10:03 AM   #9
cianoz
LQ Newbie
 
Registered: Jul 2009
Location: Italy
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks to everybody for your suggestions.
I'll give a try to your different solutions.
 
Old 03-02-2011, 11:42 AM   #10
Sector11
Member
 
Registered: Feb 2010
Distribution: BunsenLabs (Debian Stable)
Posts: 132

Rep: Reputation: Disabled
Quote:
Originally Posted by SvenJo View Post
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"
 
Old 03-02-2011, 09:13 PM   #11
turtlegeek
LQ Newbie
 
Registered: Feb 2011
Location: Georgia, USA
Distribution: xubuntu, Mac OS X
Posts: 5

Rep: Reputation: 16
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.
 
Old 03-11-2011, 09:31 PM   #12
padeen
Member
 
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware, Debian, Gentoo, FreeBSD, OpenBSD
Posts: 208

Rep: Reputation: 41
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##*/} '
 
Old 03-12-2011, 12:59 PM   #13
turtlegeek
LQ Newbie
 
Registered: Feb 2011
Location: Georgia, USA
Distribution: xubuntu, Mac OS X
Posts: 5

Rep: Reputation: 16
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 View Post
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.
 
Old 03-12-2011, 01:08 PM   #14
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 52

Rep: Reputation: 17
Quote:
Originally Posted by catkin View Post
Does the PROMPT_DIRTRIM variable do what you want, for example export PROMPT_DIRTRIM=3
I did not know that. It is really nice
 
Old 03-14-2011, 07:50 AM   #15
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
Quote:
Originally Posted by padeen View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
doing a ls from a directory (not the current) giving the files without path Byenary Linux - Newbie 5 06-25-2008 11:35 AM
double current directory in path Saulo SUSE / openSUSE 5 04-20-2006 06:19 PM
how to get absolute path of current executing C/C++ prog? Thinking Programming 2 10-10-2005 10:27 AM
Placing current path in the prompt zepplin611 Linux - General 2 05-13-2004 12:07 PM
How to change the path of the current directory ? ndha Linux - General 7 12-03-2003 11:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:51 AM.

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