This is probably a very simple thing but I can’t find an answer, possibly because I don’t know what terms to use in search.
How do I use an alias of a path with mv or cp? Or even cd?
In /etc/bash.bashrc I have:
alias docs=‘/media/docs
cd docs
Gives “No such file or directory”
Yet:
docs
Gives “Is directory”
With alias docs=‘cd /media/docs’ and by typing docs I get into the directory. Obviously I can’t use that alias with mv or cp though.
Maybe this isn’t even an intended use of alias but still. Why doesn’t it work?
26 Comments
davel@lemmy.ml · 34 pts · 84d
aliasis for aliasing commands. If you want to “alias“ arguments, use shell/environment variables.teawrecks@sopuli.xyz · 9 pts · 84d
It is worth acknowledging that this probably seems unintuitive to a new user. Makes it look like the shell has two different aliasing systems.
It makes sense the more familiar you are with bash, though. If you ever tried to
cd /some/other/path-with-docs/in/the/stringyou'd end up accidentally runningcd /some/other/path-with-/media/docs/in/the/string.Which would be confusing at best, or a security issue at worst. Better to see that
$in the cmd and know you're injecting a var's value.clmbmb@lemmy.dbzer0.com · -11 pts · 84d
New users should try to take a little time to read the basics, not suppose things then ask strangers in the internet.
teawrecks@sopuli.xyz · 4 pts · 83d
This was an unknown unknown for OP. Again, it's completely fair for a new user to see the
aliasfeature, think "ah, that's built for aliasing one thing to another, let me try it for this directory name", and be confused when it doesn't work. OP can't know what they don't know.And the open source community is just that, a community. Asking questions in forums is the accepted practice. And "basic" is hard to define. What is basic for you isn't basic for someone else, in the same way that what is basic for someone else isn't basic to you.
Black616Angel@discuss.tchncs.de · 4 pts · 84d
Wow, you are unable to even read the post you reply under.
They said that they don't know, what terms to use for their search.
clmbmb@lemmy.dbzer0.com · -2 pts · 83d
Wow, I replied to a reply, not to the original poster. Anyway, I still stand by my idea.
MimicJar@lemmy.world · 10 pts · 84d
You might find a symbolic link useful. For example
Now you'll have a "folder" in your home directory named "docs" that points to "/media/docs". You can use that path with commands like mv or cp.
These commands now both move "myfile" to the same location
MolochHorridus@piefed.social · 2 pts · 84d
Thanks, but the problem with this is that I’ll type /media/docs/whatever much faster than hunt the ~ from my keyboard because I have to move my whole hand to do so.
It's altGr + ^ on my keyboard.
teawrecks@sopuli.xyz · 4 pts · 84d
Highly recommend remapping common characters to easy-to-access hand movements. The keyboard is a tool to make things easier. I never use caps-lock, but I use esc all the time, so I regularly swap them (or just have a second esc bind).
False@lemmy.world · 2 pts · 84d
That sounds inconvenient. I use ~ all the time. $HOME should point to the same dir in most cases though
MolochHorridus@piefed.social · 1 pts · 84d
Unfortunately $ also requires pressing the damned AltGr.
adarza@lemmy.ca · 4 pts · 84d
have you looked at remapping a couple keys?
MolochHorridus@piefed.social · 1 pts · 84d
I bound ~/ to one of my keyboards G-keys. Still not optimal placement wise but way better than the AltGr-route.
False@lemmy.world · 0 pts · 84d
I'd have to buy a new keyboard at that point
rycee@lemmy.world · 8 pts · 84d
It might be excessive for your purposes but an alternative may be to use zoxide. It learns the directories you use regularly and you can then cd to those directories through the
zcommand. E.g.z docs.ranzispa@mander.xyz · 1 pts · 83d
I have setup
alias cd=zsome day and never looked back.darklamer@feddit.org · 7 pts · 84d
As already pointed out in other comments here,
aliasis used specifically to define a new shell command and in order to define some arbitrary text substitution to be used anywhere in a commandline you'll need to use a variable instead, but for the specific case ofcdthere's also a feature calledCDPATHthat you might be interested in learning about:https://writesoftwarewell.com/cdpath-easily-navigate-directories-in-the-terminal
TwistedTree@piefed.social · 6 pts · 84d
You should be using a variable not an alias.
Variables in bash have a few sharp edges; one of which is that spaces act as a delimiter and turn the variable into a list.
The other being that sometimes escaping and unescaping the contents of a variable can be stupidly tricky. This is why a lot of people who use bash do not like spaces in directory or file names.
MolochHorridus@piefed.social · 1 pts · 84d
Thanks for the tip, but escaping and unescaping sounds tedious, since I use spaces in both directory and file names.
What I like about aliases here is that I have one central location to set them up and change them. If I ever were to forget what aliases I have it's just about opening the file and looking.
TwistedTree@piefed.social · 1 pts · 84d
eh, it's mostly automagic but the first few times you encounter it makes for some fun debugging.
Play around with the
var=$(ls -A)construction a bit and see what is happening with your files.brandon@piefed.social · 5 pts · 84d
By default bash will only expand an alias if it's the first argument of the command (that is, the command itself).
It's probably not intended to use aliases this way, and there are probably better options for you.
However, there is a little trick you can do. If the alias command ends in a space, then bash will also check the next argument:
alias cd='cd 'Notice the trailing space after 'cd' in the alias definition.
alias docs='/media/docs'then,
cd docsshould work the way you expect.Another method would be to:
alias docs='echo /media/docs'Then you can do
The backticks will cause the shell to replace that portion with the output of the docs shell command, which will be expanded via the alias.
All that said, it's probably easiest just to use a link, like another commenter suggested.
kibiz0r@midwest.social · 2 pts · 84d
Cursed knowledge.
just_another_person@lemmy.world · 4 pts · 84d
This is a bad idea for a number of reasons. Instead, if you move files to a specific location frequently, you want to make a symbolic link to that location instead. It acts as a circuit breaker in case something about your environment changes or breaks.
alastel@lemmy.ml · 2 pts · 84d
You might consider looking into other shells. Fish has things such as nor requiring escaping variables with spaces and defining abbreviations that can work anywhere in a command line.
whatiswrongwithyou@lemmy.ml · 1 pts · 82d
May I interest you in the “ln” command?
Usually if you wanna access a file (or a directory, that’s a file too!) from some place other than where it is in the filesystem you make a link using ln like “ln /mnt/target link_name”. Which would give you a link type file (that shows up as a link when you give -l to ls) called “link_name” which references “/mnt/target” when you try to do something to it (Like ls!).
DrunkAnRoot@sh.itjust.works · 1 pts · 83d
as others pointed out alias is not right for this but also /media would be its own root dir idk if this is on purpose or if you meant ~/media
MonkderVierte@lemmy.zip · 0 pts · 84d