Some of the most used UNIX commands are as follows:

  1. ls
  2. pwd
  3. cd
  4. mkdir
  5. touch
  6. mv
  7. cp
  8. | - to pass the output from one command as the input to another command

Pipes is an interesting one. For instance we can get the word count of two files with |

cat file1.txt file2.txt | wc -w

Redirection

Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. Redirection allows commands’ file handles to be duplicated, opened, closed, made to refer to different files, and can change the files the command reads from and writes to.

When bash starts it opens the three standard file descriptors: stdin (file descriptor 0), stdout (file descriptor 1), and stderr (file descriptor 2).

  1. Redirect the stdout to a file:
ls > fileName
  1. Redirect the stderr to a file:
cd notADirectory/ 2> fileName
  1. Redirect both stdout and stderr to a file:
cd notADirectory/ &> fileName

&> operator redirect bothstdout and stderr to the file. There are other ways to do it as well:

command > fileName 2>&1  

2>&1 duplicates the file descriptor 2 to be a copy of the file descriptor 1

Grep - Global Regular Expression Print

To put it simply, it does what regular expression does

# Case Sensetive
grep Sam names.txt
 
# Case insensetive
grep -i Sam names.txt
 
# Case exact
grep -w Sam names.txt
 
# Multiple Command with Pipe
ls -l /usr/bin/ | grep zip

Bash Cheat Sheet