- > redirects standard output of a command to a file, overwriting previous content.
- >> redirects standard output of a command to a file, appending new content to old content.
- < redirects standard input to a command.
- | redirects standard output of a command to another command.
- sort: sorts lines of text alphabetically.
- uniq: filters duplicate, adjacent lines of text.
- grep: searches for a text pattern and outputs it.
- sed : searches for a text pattern, modifies it and outputs it.
- export VARIABLE="Value" sets and exports an environment variable.
- USER is the name of the current user.
- PS1 is the command prompt.
- HOME is the home directory. It is usually not customised.
- PATH returns a colon separated list of file paths. It is customised in advanced cases.
- env returns a list of environment variables.
e.g., env | grep PATH
-
pwd command: pwd stands for 'print working directory'. It prints the current directory.
$ pwd
-
ls command: ls stands for 'list directory content'. Display all the files and directories in the current directory. '-' this is known as a flag, we can add more specifications and properties to our commands using these flags like in case of ls. e.g.,
$ ls -a -l -h -r
-
This will print all the files and directories in the current directory but -a flag will help us to display the hidden files and directories, -l helps us to show detailed format, -r shows in reverse order and -h for units of memory, B for Bytes etc.
- You can also do like:
$ ls -alhr
-
-
cd command: cd stands for 'change directory' and helps us to change the directory.
$ cd games/
-
clear command: clear is used to clear the console screen
$ clear
-
cp command: cp is used to copy single or multiple files in current or another directory.
$ cp add.c programs/
add.c file will be copied to programs directory which is present in current directory.
-
mv command: mv is used to move or rename single or multiple files.
$ mv add.c interger_add.c
renaming add.c to integer_add.c
$ mv add.c programs/
-
cat command: cat stands for 'concatenate and print files'. It displays all the content of a file.
$ cat add.c
It can be also used for copying and creating a new file and if the file exists and still we are using cat command to copy content then the content of the new file will be deleted and the copied content will be there. Below if the new_add.c exists before then it's content will be deleted and it'll have the copied content of add.c file.
$ cat add.c > new_add.c
If you want contents of both files then use >> operator instead of >
-
wc command: wc stands for words, character, and it prints the number of lines, words and characters and file name of a file.
$ wc script.py
-
sort command: sorts the content of a file in alphabetical order.
$ sort names.txt
-
Pipe (|): Pipe ‘|’ this takes the command as standard output on the left of it and pipes it as a standard input to the command on its right.
$ env | grep PATH
-
uniq command: uniq stands for "unique" and filters out only adjacent, duplicate lines in a file and hence it will not remove those repeating lines which are not adjacent and are coming after some lines so to overcome this we can first sort the lines and then use the uniq command if we don’t want even a single line repeated.
$ sort names.txt | uniq > sorted_names.txt
-
grep command: grep stands for "global regular expression print". It searches in a files for lines that match a pattern and returns the results. It is also case sensitive. Here, grep searches for "cout" in code.cpp file and print all lines which contains it.
$ grep cout code.cpp
Tip: grep -i flag will remove the case sensitivity of the pattern being searched.
-
sed command: sed stands for "stream editor". It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to "find and replace".
Let's look at the expression 's/snow/rain/':
- s: stands for "substitution". It is always used when using sed for substitution.
- snow: the search string, the text to find.
- rain: the replacement string, the text to add in place.
but note one thing it will only replace the first snow word with rain if you want to do it in the whole file then you have to use
$ sed 's/snow/rain/' forests.txt
$ sed 's/snow/rain/g' forests.txt
-
nano command: nano is a command line text editor. It works just like a desktop text editor like TextEdit or Notepad, except that it is accessible from the command line and only accepts keyboard input. It can edit or create a new file for you.
$ nano hello_world.js
1. Ctrl + O, saves a file, O stands for output 2. Ctrl + X, exits the nano program, X stands for exit 3. Ctrl + G, opens a help menu
Note: Similarly, there is vim which can be accessed by vi command but it is operated differently
bash_profile is the name of file used to store environment settings. It is commonly called the "bash profile". When a session starts, it will load the contents of the bash profile before executing commands.
$ cat ~/.bash_profile
$ nano ~/.bash_profile
- The ~ represents the user's home directory.
- The . indicates that this file hidden file. A dot is used for hidden file
- The name ~/.bash_profile is important, since this is how the command line recognises the bash profile.
- The command nano ~/.bash_profile opens up ~/.bash_profile in nano.
- The text echo "Welcome, Jane Doe" creates a greeting in the bash profile, which is saved. It tells the command line to echo the string "Welcome, Jane Doe" when a terminal session begins.
- The command source ~/.bash_profile activates the changes in ~/.bash_profile for the current session.
$ source ~/.bash_profile
- Instead of closing the terminal and needing to start a new session, source makes the changes available right away in the session we are in.
Note: Similarly, there is .bashrc file which is available in some computers. You can use anyone of them just make sure which file dominates.
You can create shortcuts using alias command for long and frequently used commands and then you can use the small keywords instead of writing a long command and of course you can separate multiple commands using semicolons(;) like in second example.
- I frequently use second one to reach my desktop easily by only pressing f
$ alias cls='clear'
$ alias f='cd ~/Desktop/'
$ alias hide='defaults write com.apple.finder CreateDesktop false; killall Finder'
$ alias show='defaults write com.apple.finder CreateDesktop true; killall Finder'
- Using second command you can hide all your Desktop folders and files and no body will be able to see your stuff. You can undo this using third command. But this is only valid for Apple Mac users.
Note:
- alias cls = 'clear' is wrong only cls='clear' will work.
- You can save all your aliases into bash_profile file if you want to make them permanent.
Each time we launch the terminal application, it creates a new session. The session immediately loads settings and preferences that make up the command line environment. We can configure the environment to support the commands and programs we create. This enables us to customise greetings and command aliases, and create variables to share across commands and programs.
env command: The env command stands for "environment", and returns a list of the environment variables for the current user. Here, the env command returns a number of variables, including PATH, PWD, PS1, and HOME.
$ env
You can also try
$ env | grep PATH
This will print the PATH environment vairable from the environment using grep command. I hope now you are able to use these commands simultaneously. Congratulate yourself!!.
Environment variables are variables that can be used across commands and programs and hold information about the environment. Some of the environment variables are given below, rest you can explore on your own.
-
USER variable: export USER="Farhan" sets the environment variable USER to a name "Farhan". Usually the USER variable is set to the name of the Computer's owner.
$ export USER="Farhan" $ echo $USER
- The line export makes the variable to be available to all child sessions initiated from the session you are in. This is a way to make the variable persist across programs.
- At the command line, the command echo $USER prints the value of the variable.
Note that $ is always used when we are using an environmental. Here, the command echo $USER prints the name set for the variable.
-
PS1 variable: PS1 is a variable that defines the makeup and style of the command prompt.
$ export PS1="I am developer >> " I am developer >> ls
export PS1="I am developer >> " will change the styling of your command prompt to this.
- HOME variable: The HOME variable is an environment variable that displays the path of the home directory.
$ echo $HOME
- PATH variable: PATH is an environment variable that stores a list of directories separated by colons(:). Each directory contains scripts for the command line to execute. The PATH variable simply lists those directories that contain scripts.
$ echo $PATH
For example, many commands we've learned are scripts stored in the /bin directory.
This is the script that is executed when you type the pwd command. /bin/pwd.
This is the script that is executed when you type the ls command. /bin/ls
- You can also use man command to know or gain information about any command including the flags that you can use with that command
$ man cat
- Single dot (.) in Unix or Linux is used for current directory
$ ls .
- Double dots (..) are used for one level up directory
$ ls ../