10

I often need to make a small change to a file nested several directories below my current working directory. Since it's only one file, and globbing/scripting would be overkill, I make the change manually.

For example, I need to rename the file blaz.txt in ./foo/bar/blee to foobar.txt:

. 
|--foo
  |--bar
     | blee
       |-- blaz.txt

Normally, I do this by cd'ing to blee and running mv blaz.txt foobar.txt for single files. I know I could type out the full paths (resting on some handy tab completions to speed things up), but I would prefer something quicker.

Is there a better way to do this?

5 Answers5

20

With brace expansion:

mv foo/bar/blee/{blaz,foobar}.txt
don_crissti
  • 82,805
5
> touch tmp/foo/bar/baz
> rename baz boo tmp/foo/bar/baz
> ls -l tmp/foo/bar/
total 0
-rw-r--r-- 1 hl hauke 0 May 29 23:08 boo
Hauke Laging
  • 90,279
  • 1
    This is a really interesting alternative to brace expansion, but unfortunately rename is not included in some of the *nix flavors I use. – Nick Tomlin May 29 '13 at 21:43
  • 1
    Note that this depends on the version of rename your distro ships - Debian and derivatives use perl-rename as their rename (and that has an entirely different, perl-regex-based, syntax). – evilsoup May 29 '13 at 21:44
4

You can use a subshell.

( cd foo/bar/blee ; mv blaz.txt boo.txt )

But I had to +1 the braces example. That is excellent.

unxnut
  • 6,008
3

You can save the path foo=/tmp/foo/bar in your environment and use it as shortcut.

 mv $foo/blaz.txt $foo/foobar.txt 

or

 mv $foo/{blaz,foobar}.txt
Raza
  • 4,109
0

If it doesn't end up getting too messy you should be able to create a symlink and pass the commands to it itself.

A guide to creating symlinks: http://www.cyberciti.biz/faq/creating-soft-link-or-symbolic-link/

Update from comment below: ln -s /directory/thats/super/long/and/a/pita/ /etc/goats/ and then mv /etc/goats/foo.txt /etc/goats/bar.txt Worth a try.