0

I would like to be able to replace

cp x.x ~/some/long/directory/that/is/deeply/nested/

with

cp x.x s1

where s1 is an alias that points to /some/long/directory/that/is/deeply/nested/

However the destination frequently changes.
Today it is

/some/long/directory/that/is/deeply/nested/

next day it is

/some/long/directory/less/deep

next day it is

/some/other/directory/less/deep

Given that it is changing all the time is there an alias (or more likely function? as I think about it), is there a way I can do a cp with the destination being an 'interpreted' alias

something like

cp x.x `s`

The 'source' file was also be many different values. I don't want an alias that just points to one (or a list of a few) location or is for 1 (or a list of a few) source file. I want it to be whatever the alias points to at that time and the filename would be supplied when invoking it.

Source and destination could be any of thousands of values.

5
  • You need a function here, alias wouldn't help.
    – devnull
    May 3, 2014 at 12:27
  • How is s1 being changed? Or are you asking what s1 should be and how it should be changed? If the latter, a symlink seems the obvious answer.
    – goldilocks
    May 3, 2014 at 12:28
  • Any reason why not make a link s in your home directory to the 'directory-of-the-day' and just use cp filename ~/s? Then you can have a script creates/updates the link, you don't have to resource any alias, function or variable in all open terminals?
    – Anthon
    May 3, 2014 at 12:35
  • there are dozens of given values on a given day May 3, 2014 at 12:42
  • @MichaelDurrant As long as at any given time things are unique you can have one or more (symbolic) links pointing to the right direction. Update the links through some script based on a cron job as often as circumstances require. The links will point to the right location, without any need to update variables or functions.
    – Anthon
    May 3, 2014 at 13:40

2 Answers 2

4

Shell variable

You could accomplish this by adding the directories that you want to put in s1 in variables instead like so:

s1=/some/long/directory/that/is/deeply/nested/

You can then access these like this:

$ cp x.x $s1

Symbolic link

You could maintain a link in your home directory that merely points to today's long directory.

$ ln /some/long/directory/that/is/deeply/nested/ $HOME/shortlink
$ cp x.x $HOME/shortlink

Directory Bookmarking Tools

Take a look at this Q & A titled: Quick directory navigation in the terminal. Tools such as autojump or xd - eXtra fast Directory changer, can be used as well to "bookmark" frequently used directories so that you can easily change to them without having to type long paths.

References

5
  • Disadvantages of the variable approach is IMHO that you need to remember to refresh the value when the target-directory-of-the-day changes. I.e. in every open terminal/shell.
    – Anthon
    May 3, 2014 at 12:37
  • @Anthon - very good point!
    – slm
    May 3, 2014 at 12:39
  • it's actually changing throughout the day. May 3, 2014 at 12:45
  • 1
    @MichaelDurrant we can't really help unless you explain how it is changing. How can your system determine what the value should be?
    – terdon
    May 3, 2014 at 12:54
  • Sure. Thanks tendon! :) ok, so the source file is only chosen at the time that the cp is done. So would probably be STDIN. The output location would be whatever the alias points to that that moment. I want the same benefits from an alias that it provides at the command line, i.e. it's a shortcut and you can change what it is at will and the shortcut ('left side') remains the same May 3, 2014 at 13:22
0

How about something like:

#!/bin/bash
mdhf="/home/MichaelDurrant/linq
params="$@"
[[ $params = *-d* ]] && nufile=${params##*\-f}
[[ $nufile ]] && [ -L "$mdhf" ] && rm $mdhf 
[[ $nufile ]] && ln -s $nufile $mdhf 
for i in "$@"; do
[[ $i = '-d' ]] && break 
cp -v $i $mdhf
done

Usage would be mdcp filename -d directory. You'd only use the -d parameter if you were changing the current softlinked directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .