0

I'm trying to find a way to access a directory without specifying the whole path each time. For example, i download a lot of videos and I've set up a folder to store all those videos, since I'll be accessing and moving/copying stuff to and from that same folder many times and in many different occasions, I was looking for a way to define an alias (or something similar) that would allow me to refer to that folder quickly in the terminal, something on the likes of

mv video.mp4 VIDFOLDER

where

VIDFOLDER=/path/to/my/folder

As of now i tried only setting up an env variable, but it doesn't seem to work. I tried looking for other way of doing it but haven't been able to find much. Any ideas?

2 Answers 2

2
vidmv () { command mv "$@" /path/to/my/folder; }

mv would handle any missing or unreadable file errors as it normally does. This also has the advantage of supporting all of the existing mv options (except -T which wouldn't work). e.g. -v for verbose, -n for no-clobber, -u for update-only, etc.

run as, for example:

vidmv -v -u *.mp4

Note: the command in the function runs mv as found in the PATH, ignoring any aliases or functions named mv. For example, it's a common practice to alias mv='mv -i' -- this function would ignore that and run mv without -i unless you specified it as a vidmv option.

Or

vidmv () { command mv "$@" "$VIDFOLDER"; } 

This version allows changing the destination without redefining the function. Of course, VIDFOLDER would have to be pre-defined in the current shell or .bash_profile, .bashrc/etc

2
  • Thanks for introducing me to the "$@" placeholder! So many possibilities! Anyhow, thanks for the alternate solution but in this particular case i wasn't looking for a customized command. I might use it to quickly access configuration files, like cfgvim or something like that! Commented Sep 13, 2019 at 14:46
  • you're welcome. it's a bit of an eye-opener when you first start to realise the possibilities of writing your own little scripts and functions. i think the appropriate phrase is "first taste is free" :) Commented Sep 13, 2019 at 15:01
1

You are almost there:

Instead of mv video.mp4 VIDFOLDER you need to mv video.mp4 "$VIDFOLDER". The $ is needed to use the variable. It must NOT be used during assignement of the variable.

export VIDFOLDER=/path/to/my/folder should go into your .bashrc-file.

As an alternative, you could set up function for that (in .bashrc or .bash_aliases), see the function provided by cas

7
  • 1
    To set the variable as environment variable (make it available to child processes), you may want to export it like export VIDFOLDER=/path/to/my/folder. Commented Sep 13, 2019 at 13:47
  • Uhmmm, i might have found the problem. I defined the environmental variable using quotes as in export VIDFOLDER="path/to/folder" problem is i have to escape a space in the path, does it work even without quotes? Commented Sep 13, 2019 at 13:52
  • 1
    why not just vidmv () { command mv "$@" /path/to/my/folder; } and let mv handle any missing or unreadable file errors? That also has the advantage of supporting all of the existing mv options (except -T which wouldn't work). Or vidmv () { command mv "$@" "$VIDFOLDER"; } allowing the OP to change the destination without redefining the function. Of course, VIDFOLDER would have to be pre-defined in the shell or .bashrc. Commented Sep 13, 2019 at 13:56
  • @WhiteEyeTree You need to escape the spaces: VIDFOLDER=temp/path\ with\ spaces/ and then you can ls -l "$VIDFOLDER" Commented Sep 13, 2019 at 14:10
  • 1
    or vidls () { ls "$@" "$VIDFOLDER" ; } :-) (I didn't use command here because people often have ls aliased to suit their output preferences. The "$@" in the function allows adding more ls options to those prefs.) Commented Sep 13, 2019 at 14:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.