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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I just used locate to find a file "logdumptemp" and would immediately like to change directory to it. How do I pipe the output so my command acts like a google "I'm feeling lucky"?
This is what I have so far:
`locate logdumptemp` = tempvar && cd tempvar
Where tempvar is a temporary variable. Right now I'm getting a permission denied error for assigning the output to a variable, and an unknown command for the cd tempvar portion...
And finally... you can't cd into a text file... so I'm not sure how I will cross that bridge when I come to it either...
Or, if you're sure logdumptemp is a directory, not a file (which is unlikely since locate finds files, not directories), then cd $(locate logdumptemp) should work.
However, I be tempted to use something like this: Dir=$(locate logdumptemp);if [ -d $Dir];then cd $Dir;elseif [ -f $Dir];then cd $(dirname $Dir);else echo $Dir is neither a directory nor a file.;fi
Note: the test function requires a blank after the open square bracket and before the closing one. The required blanks seem to have been removed by the posting software.
It might be better to use locate -b '\logdumptemp' to restrict the search to only files with the exact name you specify. (The "\" overrides the default replacement of locate name by locate *name*)
Last edited by PTrenholme; 08-18-2009 at 09:52 AM.
Wow, I had no idea it was going to be that ridiculous. I'd rather just type in the path myself.
What I'll do now is write that script and incorporate it into the shell. Maybe submit it as an extra switch for the locate command?? It could be the newest and GREATEST feature of Red Hat 6, and they could pay me (errr... I mean us!) lots of money.
You could take either/whatever way that works, put it into a script somewhere in your $PATH (like /usr/bin/ ), then alias your own custom command to execute the script:
bash$ alias gimme=/usr/bin/your-script
..and now, when you type gimme you will be taken to the location of the file.
..And even better yet, to use the functionality to change to the directory of ANY file, grab the parameter passed to 'gimme' and substitute the variable for the 'logdumptemp' filename. Then you could do:
shell$ gimme somefile and it would take you to the directory of 'somefile'
Last edited by GrapefruiTgirl; 08-18-2009 at 11:02 AM.
there are an estimated quadrillion ways to do this. the easiest way would be:
$ cd `find /dir -name xxx -type d`
however, if there are more dir's in /dir named xxx this wouldn't work. you shouldn't use locate since you can't specify a search directory (or can you? i don't use locate often). also, the option "-type d" makes sure that find lists only directories. if you really need to use a temporary variable, for what reason ever, you could do:
$ VAR="`find /dir -name xxx -type d`" && cd $VAR
don't forget the '$' before VAR in the cd command to dereference the variable. beware, these command won't work if 'locate' of 'find' find more than one matches.
apart from this, your command make no sense to me:
`locate logdumptemp` = tempvar && cd tempvar
let's say locate returns your desired dir, e.g. /bla/logdumptemp then your command will be evaluated by the bash as
$ /bla/logdumptemp = tempvar && cd tempvar
as first argument bash expects a command /bla/logdumptemp is a directory and so bash will complain (bash: /bal/logdumptemp is a directory). secondly, the assign order is reversed. you assign a value to a variable like this:
and not the other way round. secondly, or thirdly, for those who keep track , you try to cd into a directory called tempvar. as said above, you need to dereference tempvar by letting it be preceded by '$'.
EDIT: forget the rest, piping doesn't work. see below
regarding the pipe. you can use a pipe: