Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linux - Open Terminal to Any Directory

Status
Not open for further replies.

Kirsle

Programmer
Joined
Jan 21, 2006
Messages
1,179
Location
US
I got this idea from the Perlmonks "Cool Uses for Perl", where somebody posted a script for Windows which did this...

Basically, this script will open a terminal to whatever directory you want it to, so if you're like me and need the terminal often for testing Perl scripts, you find it's a pain in the butt to have to open the terminal, `cd` to the directory your Perl script is in, etc.

So, download this script, add a launcher to your panels, and then whenever you need to use it... copy the path you want to your clipboard, click the launcher, and voila--a terminal opened with `pwd` being the directory you wanted to go to!

Note: I used Tk for the sake of popping up an error box if the path isn't valid. You'll need xclip installed (you can probably get it through Yum or Apt, if not, Google it)

Code:
[gray]#!/usr/bin/perl -w[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]Tk[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$mw[/blue] = MainWindow->[maroon]new[/maroon][red];[/red]
[blue]$mw[/blue]->[maroon]withdraw[/maroon][red];[/red]

[gray][i]# Get the clipboard.[/i][/gray]
[black][b]my[/b][/black] [blue]$cmd[/blue] = [red]"[/red][purple]xclip -o -selection clipboard|[/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [black][b]my[/b][/black] [blue]$exe[/blue],[blue]$cmd[/blue] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Couldn't run [blue]$cmd[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]$clip[/blue] = [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]'[/red][purple][/purple][red]'[/red], <[blue]$exe[/blue]>[red];[/red]

[gray][i]# Does it look like a directory?[/i][/gray]
[olive][b]if[/b][/olive] [red]([/red][blue]$clip[/blue] =~ [red]/[/red][purple]^[purple][b]\/[/b][/purple][/purple][red]/[/red] && [url=http://perldoc.perl.org/functions/-X.html][black][b]-d[/b][/black][/url] [blue]$clip[/blue][red])[/red] [red]{[/red]
	[gray][i]# Change to this directory.[/i][/gray]
	[url=http://perldoc.perl.org/functions/chdir.html][black][b]chdir[/b][/black][/url] [red]([/red][blue]$clip[/blue][red])[/red][red];[/red]

	[gray][i]# Run Terminal.[/i][/gray]
	[url=http://perldoc.perl.org/functions/exec.html][black][b]exec[/b][/black][/url][red]([/red][red]"[/red][purple]gnome-terminal[/purple][red]"[/red][red])[/red][red];[/red]
[red]}[/red]
[olive][b]else[/b][/olive] [red]{[/red]
	[blue]$mw[/blue]->[maroon]messageBox[/maroon] [red]([/red]
		-[purple]title[/purple]   => [red]'[/red][purple]Error[/purple][red]'[/red],
		-[purple]message[/purple] => [red]'[/red][purple]Copy a directory to the clipboard first.[/purple][red]'[/red] . [red]"[/red][purple][purple][b]\"[/b][/purple][blue]$clip[/blue][purple][b]\"[/b][/purple][/purple][red]"[/red],
		-[purple]type[/purple]    => [red]'[/red][purple]Ok[/purple][red]'[/red],
		-[purple]icon[/purple]    => [red]'[/red][purple]error[/purple][red]'[/red],
	[red])[/red][red];[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/exit.html][black][b]exit[/b][/black][/url][red]([/red][fuchsia]0[/fuchsia][red])[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
Other Modules used :
[ul]
[li]Tk[/li]
[/ul]
[/tt]

-------------
Cuvou.com | The NEW Kirsle.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top