1

I have a long path to some of my files and I don't want to type and/or use the Tab-key as much when using it with commands in the Terminal.
So I want to add "shortcuts" to some of them (both files and directories), e.g.

replace
ls folder1/folder2/folder3/folder4/

with something like
ls folder1234

and

replace
cat folder1/folder2/folder3/folder4/my_super_very_long_amazing_filename.txt

with something like
cat long_path_and_filename.txt (but obviously shorter :))

or if it's not possible, then with two "replace commands" like
cat folder1234/short_filename

Are any of the above possible?
Can you use the ln command or maybe the alias command?

2 Answers 2

2

Something fulfilling what you believe would work best

One way to create "shortcuts" for pathnames would be to define environment variables:

export folder1234='folder1/folder2/folder3/folder4'
export short_filename='my_super_very_long_amazing_filename.txt'

This way, you could reduce your typing to

ls $folder1234
cat $folder1234/$short_filename

While that sounds like a magnificent idea, nobody appears to do that. It indeed requires you to set up and maintain static definitions, and remember all of these artifacts. These will work the period you work on a project, but will be largely obsolete once you move to a different project.

The better workflow using standard tools

The better way to avoid long path names, is to turn into a habbit of changing to the directory where you need to do your work. This way, it takes a single

cd folder1/folder2/folder3/folder4/

(where you use autocompletion and tab) to bring you there. Next day, you will get there more quickly: instead of attempting the command again, type Ctrl+r: this starts a reverse search through your history. Type folder4 and you will quickly see your cd command again. Select it and execute it. Done.

Once inside that folder, typing the long filename is a no-brainer using the tab autocompletion.

On steroids

Install fuzzy finder. sudo apt install fzf

At the empty prompt, hit Alt+c. Fuzzy finder will pop up. Type folder4: you will see the full folder name, select it, hit enter and you are there.

Type cat then Ctrl+t. my_super_very may already reveal your file only. Hit enter to add the full path to the command line.

Alternative to the above: type ** then Tab to trigger fussy finder.

0

Why not alias them?

In *buntu .bashrc is located in /home. I believe .bashrc looks for a file named .bash_aliases for your own custom aliases.

so for you to (in terminal) do:

nano .bash_aliases

Then you can add your custom aliases to the file(for example):

alias ls_mine='ls /Documents && ls /Downloads | cat'

Then save and exit with Ctrl+o then Ctrl+x.

Then run:

source .bashrc

And try your alias.

You can always edit your aliases anytime by:

nano .bash_aliases

Remember to save with Ctrl+o and exit with Ctrl+x then run:

source .bashrc

You must log in to answer this question.

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