While I ran these commands in macos using iterm2, the tricks are valid for most of the terminals out there.
Quick side-note: My username is rishirajpurohit but it will be your username for below examples/outputs/images when you run any of these commands.
-
$PATH variable
- Your shell uses a variable called path to store all the directory and places separated by colon where it can look for a program to run.
- You can update the path dynamically to add or replace a directory when running a program
- It can also help in finding the directory of certain programs.
- Most programs, if not done automatically, will require you to add their executable to the path so that you can directly use them from shell.
-
Which program
- Sometimes you want to see which version or what program will run when you type a command. For example, I have few versions of php set up in my system and sometimes certain scripts require certain version of php.
- Using which I can quickly see the path of the php that will run my script and change it if needed using the $PATH variable mentioned above.
~ (tilde character) : Tilde is basically your home directory or a shorthand for
/Users/rishirajpurohit
. So to navigate toSites
folder in your home directory you can just use~/Sites
.
- (dash character) : A dash represents the last place you were before coming to current directory and is very useful when you are jumping back and forth between directories.
ls with path :
ls
takes a path as an argument and can list the contents of the path you provide, so you don’t have to cd to a folder to see its contents. By default the path is set to.
or your current directory.
Using < for Input : Not commonly used but sometimes a program might need large input and you may have it saved it in a file, you can use the < character to pass the contents of the file as input to the program.
Using > for Output : A lot of times we want to save the output of a program to a file instead of displaying it in a prompt. For example,
python fetch_results.py > ~/Sites/carz/sample_cars.json
.Using >> for Append : Continuing the example from above, lets says you want to run the command every few hours to fetch additional data, and want to just append the result to the same file, you can use >> character. For example,
python fetch_results.py >> ~/Sites/carz/sample_cars.json
.
-
| (pipe character) : One of the most commonly used tricks to connect or chain programs when you want the output of a program as an input of another program.
- To find all files containing .py :
ls | grep .py
. - To find a command you ran in the past:
history | grep docker
will return the command.
- To find all files containing .py :
Thank you for reading and comment below your tools and tricks that you learned over time and hope that you knew when starting.
Also give claps, shouts, unicorns or anything else if you liked the post.
Top comments (0)