Joduis
Joduis

Reputation: 11

linux alias with part of path

Lately I have been getting familiar with aliases in the linux shell. I find them particularly useful for replacing hdfs dfs commands like this:

alias hls="hdfs dfs -ls"

This alias can be used like

hls /my/directory/ 

which will list the contents in that folder on the hadoop file system.

I would like to add another hdfs dfs -ls alias that contains a root directory, something along the lines of:

alias hlsr="hdfs dfs -ls /my/root/directory/"

I would then want to use this command as follows:

hlsr rest/of/path

which would then result in listing the contents of the path /my/root/directory/rest/of/path.

The problem is combining the alias, that ends with a root path, with a string which represents the rest of that path.

Does anyone know how to do this? Would I have to write a function or could it be done with aliases as well?

Thanks, J

Upvotes: 1

Views: 1416

Answers (2)

Jerren Saunders
Jerren Saunders

Reputation: 1435

I landed at this SO question a few weeks ago since I was looking to do the something very similar with a few different commands and not having much luck. Defining functions could have worked for me, but there would have been many to manage.

I know this is an old question, but in case anybody else landed here looking to do something similar, I wanted to share the script that I wrote that supports what the OP was looking for and also a few other things.

The script is available at https://github.com/jerrens/MyCE. Once the script is installed, edit ~/.myCommand to add the following:

hlsr=hdfs dfs -ls /my/root/directory/$1

Then at the terminal, run my hlsr rest/of/path

I don't have Hadoop on my system, but tested with the following:

~$ cat ~/.myCommand
rells=ls -lha ~/$1

~$ my rells .ssh
total 28K
drwx------  2 jdoe jdoe 4.0K Oct 30 23:10 .
drwxr-x--- 19 jdoe jdoe 4.0K Nov 18 19:31 ..
-rw-r--r--  1 jdoe jdoe   72 Oct 30 22:37 config
-rw-------  1 jdoe jdoe 2.6K Sep  8 19:11 id_rsa
-rw-r--r--  1 jdoe jdoe  575 Sep  8 19:11 id_rsa.pub
-rw-------  1 jdoe jdoe 1.8K Oct 30 23:10 known_hosts
-rw-------  1 jdoe jdoe  948 Oct 30 23:10 known_hosts.old

More details can be found in the MyCE GitHub repo and also at https://jerren.medium.com/introduction-my-command-engine-myce-42e15028364a

Upvotes: 1

silgon
silgon

Reputation: 7191

You can use a function in a file to be sourced. Let's call a file with name myfunctions.sh

hls () 
{ 
    hdfs dfs -ls "$1";
}

The argument $1 is your first parameter and then you have to source the file with source myfunctions.sh or . myfunctions.sh.

Upvotes: 3

Related Questions