LinuxQuestions.org
Visit Jeremy's Blog.
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 08-18-2009, 10:13 AM   #1
ECRocker
Member
 
Registered: May 2007
Distribution: RHEL 5, Ubuntu 9.10, Fedora 12
Posts: 85

Rep: Reputation: 17
Pipe the Output into "cd"


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...

Thanks for pondering on this one.
 
Old 08-18-2009, 10:26 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
bash-3.1$ cd $(echo "`locate logdumptemp | gawk -v FS=/ '{gsub ($NF,"",$0); print}'`")
Maybe try that?

Sasha
 
Old 08-18-2009, 10:26 AM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Oops, a dupe..

PS - there's probably a less convoluted way, but my knowledge of ALL the cool ways of doing things is limited but this works for me.

S

Last edited by GrapefruiTgirl; 08-18-2009 at 10:29 AM.
 
Old 08-18-2009, 10:34 AM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Try tempvar=$(locate logdumptemp).

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

<edit>
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.
</edit>

<edit 2>
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*)
<edit 2>

Last edited by PTrenholme; 08-18-2009 at 10:52 AM.
 
Old 08-18-2009, 11:46 AM   #5
ECRocker
Member
 
Registered: May 2007
Distribution: RHEL 5, Ubuntu 9.10, Fedora 12
Posts: 85

Original Poster
Rep: Reputation: 17
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.
 
Old 08-18-2009, 11:59 AM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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'

Sasha

Last edited by GrapefruiTgirl; 08-18-2009 at 12:02 PM.
 
Old 08-18-2009, 03:01 PM   #7
ECRocker
Member
 
Registered: May 2007
Distribution: RHEL 5, Ubuntu 9.10, Fedora 12
Posts: 85

Original Poster
Rep: Reputation: 17
You're amazing
 
Old 08-18-2009, 03:19 PM   #8
:::
Member
 
Registered: Aug 2009
Distribution: slackware 12.2
Posts: 51

Rep: Reputation: 17
there are an estimated quadrillion ways to do this. the easiest way would be:
Code:
$ 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:
Code:
$ 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:
Code:
`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
Code:
$ /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:
Code:
VAR="value"
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:
Code:
$ find /dir -name logdumptemp -type d | cd
is possible because 'cd' expects an input.

cheers :::

Last edited by :::; 08-18-2009 at 04:55 PM.
 
Old 08-18-2009, 03:28 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by ::: View Post
regarding the pipe. you can use a pipe:
Code:
$ find /dir -name logdumptemp -type d | cd
is possible because 'cd' expects an input.
Intriguing

Doesn't work on ubuntu 8.04 with bash 3.2.39:
Code:
c@CW8:~$ echo /tmp | cd
bash: echo: write error: Broken pipe
 
Old 08-18-2009, 04:30 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
How about this in your ~/.bashrc ?

Code:
function gimme
{
  cd $(locate $1 |head -n 1| xargs -i dirname  {} )
}


Cheers,
Tink
 
Old 08-18-2009, 04:48 PM   #11
:::
Member
 
Registered: Aug 2009
Distribution: slackware 12.2
Posts: 51

Rep: Reputation: 17
Quote:
Originally Posted by catkin View Post
Intriguing
Doesn't work on ubuntu 8.04 with bash 3.2.39
sorry, this was my fault. my bash (3.1.17(2)) didn't complain about the command so i assumed it was ok. i forgot to issue a pwd to check if it succeeded. thanks for the correction

:::
 
Old 08-18-2009, 06:01 PM   #12
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
I really (REALLY) need to learn about that 'xargs' thing ....

Anyhow, there you have it OP -- lots of ways!

Sasha
 
Old 08-18-2009, 06:06 PM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by GrapefruiTgirl View Post
I really (REALLY) need to learn about that 'xargs' thing ....

Anyhow, there you have it OP -- lots of ways!

Sasha
Heh ... could be done w/o xargs, too.

Code:
 cd $( dirname $( locate $1 |head -n 1 ))
Actually prettier, I feel ;}
 
Old 08-18-2009, 06:22 PM   #14
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
*makes mental note to bookmark this thread until me learns/remembers this stuff*

Thanks Tink,

Sasha
 
Old 08-18-2009, 06:48 PM   #15
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
regarding the pipe. you can use a pipe:
Code:

$ find /dir -name logdumptemp -type d | cd

is possible because 'cd' expects an input.
Nope!!...

1. This does not work
2. cd does NOT expect an input

Otherwise, all is well.....

Seriously, I've been curious about this whole issue---when do pipes work and when not. One tentative conclusion is that they work when the receiving command will not work without an input.

I just ran this test:
Code:
grep a
   <hangs waiting for data>
echo father | grep a
   father
echo father | grep a filename
   <lines in filename containing "a">
Conclusion: if grep is not given the 2nd argument, then it goes looking. With the pipe in the 2nd example it finds father. In the third example, it never sees father because it reads from filename.

Last edited by pixellany; 08-18-2009 at 06:49 PM.
 
  


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
square brackets in output of "ps aux" not matching output of "ps -ejH" alirezan1 Linux - Newbie 14 07-14-2010 05:17 AM
Courious "unknown" entries in "netstat" output Sheridan Linux - Networking 5 09-01-2009 10:09 AM
"failed to execute child process" "Input/output error" fl.bratu Fedora 4 12-15-2008 05:03 AM
SFTP - CyberDuck doesn't work, but WinSCP does? Server exited on signal "PIPE"? dragos19 Other *NIX 1 10-04-2008 05:52 PM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 08:27 AM


All times are GMT -5. The time now is 10:53 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration