Elektrine lite

← Feed

@MolochHorridus@piefed.social

Using alias of path in commands?

2026-05-17 15:56 UTC

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?

Replies (6)

  • @davel@lemmy.ml 2026-05-17 16:11

    alias is for aliasing commands. If you want to “alias“ arguments, use shell/environment variables. $ docs=/media/docs $ cd $docs

    Open ##2643825

  • @brandon@piefed.social 2026-05-17 16:21

    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.

    Open ##2644171

  • @TwistedTree@piefed.social 2026-05-17 16:49

    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.

    Open ##2644341

  • @rycee@lemmy.world 2026-05-17 18:50

    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.

    Open ##2674215

  • @DrunkAnRoot@sh.itjust.works 2026-05-18 20:54

    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

    Open ##2674387

  • @whatiswrongwithyou@lemmy.ml 2026-05-19 16:12

    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!).

    Open ##2695978