LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-05-2010, 09:47 AM   #1
talla
LQ Newbie
 
Registered: Jan 2010
Location: Berlin
Posts: 16

Rep: Reputation: 1
quick directory change (bash)


Hey guys,

I often use cp/mv to copy/move a file to a different directory. My directory structure is relatively deep so commands most often look like:

cp foo.bar ../../../../dir1/dir2/dir3/foo2.bar

after copying/moving I need to change directory to the target dir. Is there an easy way to change the directory to the destination of the last action / to the "last parameter" or something like that?

(of course, one can use the bash history.. but is there a more convenient way?)

Cheers,

talla
 
Old 02-05-2010, 09:58 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
edit of edit:

cd !$

(note you'd have to stop typing the destination file name)

Code:
c@wb:/tmp/a/b/c/d$ cp e /tmp
c@wb:/tmp/a/b/c/d$ cd !$
cd /tmp
c@wb:/tmp$

Last edited by AlucardZero; 02-05-2010 at 10:02 AM.
 
Old 02-05-2010, 10:09 AM   #3
talla
LQ Newbie
 
Registered: Jan 2010
Location: Berlin
Posts: 16

Original Poster
Rep: Reputation: 1
hey!

thanks, perfectly working!

does !$ always return the last parameter?
 
Old 02-05-2010, 10:11 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
If you have certain directories (deep in the directory tree) that you access REGULARLY, you could create symlinks to them in your home directory or create a series of custom variables in your .bashrc

eg. comp="/home/user/data/programs/compiled/"
torcom="/home/user/downloads/torrents/torrents_completed/"

cd $comp, cd $torcom, etc.

just a thought...There will probably be a better way of handling such situations, though.

Last edited by sycamorex; 02-05-2010 at 10:24 AM.
 
Old 02-05-2010, 10:13 AM   #5
JimBrewster
Member
 
Registered: Feb 2010
Location: usa:/dev/random
Distribution: Slackware-15.0; -current
Posts: 245

Rep: Reputation: 60
A few ways to do it, some more elegant than others:

If in X, work in two different terminals (or tabs if you're using a tabbable term program), one where pwd is the source, the other in the destination directory.

Do the same thing in emacs, each directory being a dired-d buffer.

Use shell variables, i.e.:
Code:
export OD=/path/to/your/orginal/files
export DD=/path/to/your/destination/files # optional if you just stay here...
cd $DD
cp $OD/foo.bar ./foo2.bar
# Do whatever you need to do...
 
Old 02-05-2010, 10:17 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

If you have to do a lot of directory changing you could also use pushd and popd instead of cd. So, rather than typing
Code:
cd destinationdir
use
Code:
pushd destinationdir
. Pushd remembers the directory where you were, then cds into destinationdir. You can run this on all sorts of different directories, and Bash will remember your entire trail. When you want to step backwards, just type popd to go to the previous directory.

Kind regards,

Eric
 
Old 02-05-2010, 10:51 AM   #7
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
Quote:
Originally Posted by talla View Post
does !$ always return the last parameter?
Yes it does. Turns out it is a Bash "History Expansion" "Word Designator". Something new every day! I'm going to have to drop that one in the conversation at tea break
 
1 members found this post helpful.
Old 02-05-2010, 09:54 PM   #8
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by catkin
Yes it does. Turns out it is a Bash "History Expansion" "Word Designator". Something new every day! I'm going to have to drop that one in the conversation at tea break
Well thanks, that link was really nice, till now I was unaware of the power of bash
 
Old 02-06-2010, 06:43 AM   #9
talla
LQ Newbie
 
Registered: Jan 2010
Location: Berlin
Posts: 16

Original Poster
Rep: Reputation: 1
thank you very much for your answers!

this forum and the cooperativeness of its members is awesome!!

Cheers,

talla
 
  


Reply

Tags
directory, navigation, path, shell



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
change default vsftp anonymous accout directory /var/ftp to other directory? hocheetiong Linux - Newbie 2 01-24-2010 06:33 PM
change default bash directory in gnome-terminal ming0 Linux - Software 5 10-01-2009 06:54 PM
is there a linux 'change' prog (like old dos 1) for quick change of text in files? johnemac General 2 06-15-2009 11:22 AM
how to change directory using bash or other simple language packets Programming 3 05-08-2009 04:28 AM
Unable to change directory on terminal using bash script gregorian Linux - Newbie 11 02-05-2007 03:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:47 PM.

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