Linux
From Wikipedia, the free encyclopedia
Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).
Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.
Rules
- Posts must be relevant to operating systems running the Linux kernel. GNU/Linux or otherwise.
- No misinformation
- No NSFW content
- No hate speech, bigotry, etc
Related Communities
Community icon by Alpár-Etele Méder, licensed under CC BY 3.0
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 z command. E.g. z docs.
alias is for aliasing commands. If you want to “alias“ arguments, use shell/environment variables.
$ docs=/media/docs
$ cd $docs
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/string you'd end up accidentally running cd /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.
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.
You might find a symbolic link useful. For example
ln -s /media/docs ~/docs
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
mv myfile ~/docs
mv myfule /media/docs
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.
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).
That sounds inconvenient. I use ~ all the time. $HOME should point to the same dir in most cases though
Unfortunately $ also requires pressing the damned AltGr.
have you looked at remapping a couple keys?
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 docs should work the way you expect.
Another method would be to:
alias docs='echo /media/docs'
Then you can do
cd `docs`
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.
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.
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.
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.
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.