Shell Scripting Tips - One liner to change to a given users directory - the hard way
https://www.zyxware.com/sites/default/files/styles/user_image/public/pictures/zyxlogo.png?itok=J9t-ZEoU
BY webmaster
11 years ago
Technical-Solution
1 comments comment
Recently we had to provide support on a Red Hat Server where there were hundreds of users and the user directories organized into hierarchies. Normally we change to a users directory using cd ~username. We had some extra time on our hands and wanted to play with the /etc/passwd file and tried to use awk to do the same thing. The following one liner will do this the hard way :-)

cd `(cat /etc/passwd|grep username|awk -F ":" '{print $6}')`

The script works as follows. The home folders of all users are listed in the 6th column of the /etc/passwd file. The columns in the file are separated by colons. The 'cat' part will print the contents of the file to stdout which is piped to grep to find the line corresponding to the user. The grep will print just this line to the stdout which is then piped to awk which takes the content of the 6th column and prints it out. This string is passed as the return value from the back-quoted string and cd uses this path to change the directory.

Using this served the purpose but the solution did not look very elegant. So we saved the following to a file /usr/local/bin/cduser

cd `(cat /etc/passwd|grep $1|awk -F ":" '{print $6}')`

and then added the following alias to ~/.bashrc

alias cduser='. /usr/local/bin/cduser'

The alias is required because a change directory command inside a script will only run in its own context and when control is returned to the calling shell the working directory would remain the same. The only way to work around is to source the script instead of executing it and this is done through the alias part. All is well that ends well. Now all we had to do to change to a users directory was to execute

cduser username

which is slightly more longer than cd ~username but you would be more wiser about shell scripting by figuring out the harder way :-).


RELATED ARTICLE

/themes/custom/zyxpro_light/images/placeholder.png
close

on 04th April 2007 / by webmaster
Linux is the epitome of the FLOSS model. Technical Solution Linux installation Leave a reply Your email address will not be published. Required fields are marked * Dewey J. Corl (not verified) access_time 09 Jul 2020 - 23:14 Linux is not ready for the general public! (I am a Ubuntu user). For proof, 1. try to simply print labels on a Dymo label printer. It takes a LOT of work to get to the ease of label printing already available in Windows and Mac. 2. try syncing a modern Palm based PDA. Yes, jpilot and others will sync the main data, but pictures and midi files do not get transferred without a lot of extra setup. These are examples of applications that are not ready for the general public. Since an operating system only supports applications, Linux is not ready to be a common desktop for the general public. While we are waiting for the applications to catch up, keep up the good work!! Anoop John (not verified) access_time 09 Jul 2020 - 23:14 Dewey Yes you may be right. But these are not applications that a common man would use. A common man would use one of these applications - Internet browser, Chat Client, Email Client, Document Writer, Presentation Creator, Spreadsheet Editor, Movie Viewer, Music Player, CD/DVD Writer. Both examples you cited are more specific applications that only a small percentage of the whole population uses. It will take time before those hardware vendors identify the need from their perspective to address the Linux community. Krishnadas (not verified) access_time 09 Jul 2020 - 23:14 Dear Mr Anoop, Thank you very much for the free installation of LINUX done in my PC. I am getting immersed in the LINUX and seeing the versatility. To my surprise, I am able to read one of my old backup CD(wherein lot of my valuable file exist) done in DIRECT CD wizard (a custom cd writing software of Easy Cd creator in WIN98) which could not be read in XP. Installation of old version of easycd creator/direct cd program was not possible in XP. I thought that possible i lost all data. Very pleasant start! Srikanth N. S. (not verified) access_time 09 Jul 2020 - 23:14 Hi Anoop, My friend is thrilled with Ubuntu. One of his grouses with windows was that after OS installation, everything else is to be installed separately which is a real headache. WHereas if u install Linux, everything is installed as a package and he is thrilled to bits! Let linux installation spread in Trivandrum and your service in this direction is highly appreciated. Kepp up the good work Regards. PDA Freak (not verified) access_time 09 Jul 2020 - 23:14 Is it possible to install linux on a packardbell notebook ? Pagination Current page 1 Page 2 Next page Next › Last page Last » Add new comment
Leave a reply
Your email address will not be published. Required fields are marked *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

author-image
cherry (not verified)
access_time 10 Jul 2020 - 04:24

alternatively:

cd `(cat /etc/passwd|grep username|cut -d ':' -f 6`

;-)