0

I usually go through files using the following commands:

cd first
pwd
ls
cd second
ls

This a more generic way of my work. Can I execute all the commands one by one by itself by calling "some secret command"? . The point here is to reduce the typing, since I literally type all of the above lines at least 25 times a day.

  • 1
    I don't really understand what you're looking for. You can cd into nested directories: cd first/second. To input multiple commands in a single prompt, use a semicolon: cd first; pwd Or are you looking for an alias/bash function, which triggers ls automatically after changing directories? – Panki Mar 18 at 10:48
  • Yes I am looking to stack all of the above commands to some file or whatever means and use a simpler version of command that makes me type lesser whenever I want to run a series of commands. – Alagusankar Mar 18 at 11:09
  • (and is bash even your shell?) – Jeff Schaller Mar 18 at 11:47
0

Edit: I just realised that first, second are just placeholders for your real dirnames, right? In that case, instead of an alias you can create a function in your ~/.bashrc:

function pipe(){
    first=$1
    second=$2

    cd -- "$first"; pwd; ls; cd -- "$second"; ls
}

Source your ~/.bashrc and then you can call pipe with 2 input arguments (the names of your first and second dirs:

pipe first_dir_name second_dir_name   

=====

You can create an alias for this series of commands.

Add to your ~/.bashrc the following line:

# 'pipe' is the name of your alias, you can choose any name you want

alias pipe="cd first; pwd; ls; cd second; ls" 

Source your ~/.bashrc file:

. ~/.bashrc

You can now start executing pipe in your terminal that will be executing your series of commands. Of course, you have to ensure that you call pipe from the correct dir that allows you to cd to the first and second dirs.

  • I don't know what do you mean by Source your ~/.bashrc. Do I have to store the function in a file and then call it? Or do you pose some other solution. I am a novice here and I would like a step by step approach. And I will use the original directory and not just placeholders. Whole point is to run series of commands one by one using least typing work everytime – Alagusankar Mar 18 at 11:14
  • .bashrc is a file in your home directory, the full path is ~/.bashrc. Open it and add the lines I've written above: function pipe(){ ... }. Save and close your .bashrc and then type in your teminal: source ~/.bashrc. You're then set to start using pipe dir1 dir2 in your terminal.. – dvitsios Mar 18 at 11:21
  • I will definitely try it. Thanks for the contribution. – Alagusankar Mar 18 at 13:09
  • You can upvote and/or accept my answer if you find it useful. Thanks – dvitsios Mar 18 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.