(Reference) A Handy Tutorial on Bash Unix shell

A Handy tutorial on Bash Unix shell

A short intro about Bash and Most commonly used Bash commands

  • Bash is the shell, or command language interpreter, for the GNU operating system. The name is an acronym for the 'Bourne-Again SHell'.

  • Bash is largely compatible with sh and incorporates useful features from the Korn shell ksh and the C shell csh.

  • Bash is ultimately intended to be a faithful implementation of the IEEE Posix Shell and Tools specification (IEEE Working Group 1003.2).

  • Bash contains features that appear in other popular shells and some features that only appear in Bash. Some of the shells that Bash has borrowed concepts from are the Bourne Shell (`sh'), the Korn Shell (`ksh'), and the C-shell (`csh' and its successor, `tcsh').

  • While the GNU operating system provides other shells, including a version of csh, Bash is the default shell.

Commonly used Bash commands - A quick reference

[ cp ]  [ mv ] [ rm ] [ mkdir ] [ cd ] [ rmdir ] [ file ] [ pwd ]

[ ls ] [ cat ]  [ find ] [ chmod ]

click here : Command Line Editing Tips - Quick Reference

  cp Make an exact copy of the contents of a file.
  Syntax   cp <oldfile> <newfile>
  Description  Here <oldfile> is the name of the existing file you want to copy. A <newfile> will be created which is an exact copy of the <oldfile>.
 cp file1 file2 Makes a copy of a file.
 cp file1 file2 Prompts the user before overwriting an existing file.
 cp file directory To copy a file into a different directory.

 

  mv Moves an existing file or directory to a different directory.
  Syntax 

 mv <oldfile> <newfile>

 mv <olddir> <newdir>

  Description  A <newfile> will replace the existing <oldfile>.
 mv file directory Makes a copy of a file.
 mv [-i] file1 file2 To rename file1 as file2. Optional -i will prompt the user before renaming an existing file.
 mv dir1 dir2 To rename the directory dir1 as dir2  Or  Moves a directory to a different directory.

 

  rm Removes an existing file.
  Syntax 

 rm <file>

  Description  Remove or delete a file
 rm [-i] file Permanently deletes a file. Optional -i flag will verify prior to the removal of the specified file.
 rm -rf directory Recursively removes all the files and sub-directories in the specified directory without any warnings.
 rm file1 file2 file3 Single command removes multiple files file1, file2 and file3 without any warnings.

 

  mkdir Creates a new directory.
  Syntax 

 mkdir <directory>

  Description  Creates a new directory.
 mkdir directory Creates a new directory.
 mkdir dir1 dir2 dir3 Creates multiple directories at the same level.

 

  cd Change to a directory.
  Syntax 

 cd <directory>

  Description  Change to an existing directory.
 cd directory Changes current directory to a different directory
 cd .. Changes to a parent directory.
 cd ~   or   cd $HOME Changes to a HOME directory.

 

  rmdir Removes an empty directory.
  Syntax 

 rmdir <directory>

  Description  Removes or deletes an existing empty directory.

Note: To remove a non-empty directory use 'rm -rf directory' command. For more info, Refer : rm command.

 rmdir directory Deletes an existing empty directory.
 rmdir [-i] dir1 dir2 Verifies before removing the multiple empty directories at the same level.

 

  file Determines the type of file.
  Syntax 

 file <filename>

  Description  Determines the type of file like ASCII Text, executable,etc.
 file a.out Determines that the type of file a.out is an executable.
 file readme.txt Determines that the readme.txt is an ASCII Text file.

 

  pwd Present working directory
  Syntax 

 pwd

  Description  Shows you which directory you are currently in.
 cd /tmp

 pwd

Displays absolute pathname of current directory. Here pwd displays /tmp as the present working directory.

 

 
  ls Lists the files and sub-directories.
  Syntax 

 ls [flags] [<directory>]

  Description  Lists the files and sub-directories of the current directory or specified directory (Optional). Most frequently used [flags] are discussed below.
 ls Lists the files and sub-directories in your current directory.
 ls directory Lists the files and sub-directories of the specified directory.
 ls -la Long listing format (-l flag) of all the files and sub-directories in your current directory including the hidden or dot(.) files (-a flag).

 ls -t

Sorts file by modification time (-t flag).

 ls -r

Lists in reverse order (-r flag).

 ls -F

 Lists file type with special character (-F flag).

 

  cat Displays a specified file contents at once.
  Syntax 

 cat <filename>

  Description  Displays the contents of a specified file onto the console.
 cat readme.txt Displays the contents of a 'readme.txt' file.
 cat file1 file2 > file3 Concatenates the file1 and file2 into file3.
 cat abc.txt | more

OR

more abc.txt

Displays the contents of 'abc.txt' file, one screen at a time. Note: Use Spacebar to scroll forward and q to quit.

 

  find Finds the files and directories
  Syntax 

 find <directory> [flags]

  Description  Finds the files and directories.
 find . -name "abc.txt" -print Search for file with a specific name (abc.txt) in a set of files using -name flag. The -print flag will print out the path of any file that is found with that name.
 find . -name *.log -print Finds and displays only the log files in the current directory and its sub-directories.
 find . -name *.log -exec rm {} \; Finds and deletes only the log files in the current directory and its sub-directories. Note: To apply a unix command to a set of file, Use -exec flag.
find /tmp -name *.log -exec grep "ERROR" {} \; -print Finds log files in the /tmp directory and all its sub directories. All files that contain the string will have their path printed to standard output. Note: To search for a string in a selection of files using the flag -exec grep "your string goes here".

 

  chmod Changes the file or directory priveleges.
  Syntax 

 chmod statuscode <filename>

 chmod statuscode <directory>

  Description  Changes permission status for a file or directory.
 chmod 444 dir

or

chmod +r dir

To give READ permission to everyone to read the specified directory and its contents..
 chmod 777 a.out To give FULL access to everyone i.e read, write and execute.
 chmod r+w prg.cpp To give both READ and WRITE privilege to everyone for prg.cpp file.

 


Bash Command Line Editing Tips - A quick reference

  • The text C-k is read as `Control-K' and describes the character produced when the k key is pressed while the Control key is depressed.

  • The text M-k is read as `Meta-K' and describes the character produced when the Meta key (Left ALT or ESC key) is depressed first, and then typing k. This process is known as metafying the k key.

  • The text M-C-k is read as `Meta-Control-k' and describes the character produced by metafying C-k.

  C-b Move back one character.
  C-f Move forward one character.
  DEL or Backspace Delete the character to the left of the cursor.
  C-d Delete the character underneath the cursor.
  Printing characters Insert the character into the line at the cursor.
  C-_   or   C-x C-u Undo the last editing command. You can undo all the way back to an empty line.
  C-a Move to the start of the line.
  C-e Move to the end of the line.
  M-f Move forward a word, where a word is composed of letters and digits.
  M-b Move backward a word.
  C-l Clear the screen, reprinting the current line at the top.
  C-k Kill the text from the current cursor position to the end of the line.
  M-d Kill from the cursor to the end of the current word, or, if between words, to the end of the next word.
  M-DEL Kill from the cursor the start of the current word, or, if between words, to the start of the previous word.
  C-w Kill from the cursor to the previous whitespace. This is different than M-DEL because the word boundaries differ.
  C-y Yank the most recently killed text back into the buffer at the cursor.

FYI,Yanking means to copy the most-recently-killed text from the kill buffer.

  M-y Rotate the kill-ring, and yank the new top. You can only do this if the prior command is C-y or M-y.
  C-r To search backward in the history for a particular string
  C-s To search forward in the history for a particular string
  previous-history (C-p) Move `back' through the history list, fetching the previous command.
  next-history (C-n) Move `forward' through the history list, fetching the next command.
  beginning-of-history (M-<) Move to the first line in the history.
  end-of-history (M->) Move to the end of the input history, i.e., the line currently being entered.
  reverse-search-history (C-r) Search backward starting at the current line and moving `up' through the history as necessary. This is an incremental search.
  forward-search-history (C-s) Search forward starting at the current line and moving `down' through the the history as necessary. This is an incremental search.
  non-incremental-reverse-search-history (M-p) Search backward starting at the current line and moving `up' through the history as necessary using a non-incremental search for a string supplied by the user.
  non-incremental-forward-search-history (M-n) Search forward starting at the current line and moving `down' through the the history as necessary using a non-incremental search for a string supplied by the user.
  yank-nth-arg (M-C-y) Insert the first argument to the previous command (usually the second word on the previous line) at point. With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0). A negative argument inserts the nth word from the end of the previous command.
  yank-last-arg (M-. or M-_) Insert last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn.
  upcase-word (M-u) Uppercase the current (or following) word. With a negative argument, uppercase the previous word, but do not move the cursor.
  downcase-word (M-l) Lowercase the current (or following) word. With a negative argument, lowercase the previous word, but do not move the cursor.
  capitalize-word (M-c) Capitalize the current (or following) word. With a negative argument, capitalize the previous word, but do not move the cursor.
  complete (TAB) Attempt to perform completion on the text before point. The actual completion performed is application-specific. Bash attempts completion treating the text as a variable (if the text begins with `$'), username (if the text begins with `~'), hostname (if the text begins with `@'), or command (including aliases and functions) in turn. If none of these produces a match, filename completion is attempted.
  possible-completions (M-?) List the possible completions of the text before point.
  insert-completions (M-*) Insert all completions of the text before point that would have been generated by possible-completions.
  complete-filename (M-/) Attempt filename completion on the text before point.
  possible-filename-completions (C-x /) List the possible completions of the text before point, treating it as a filename.
  complete-username (M-~) Attempt completion on the text before point, treating it as a username.
  possible-username-completions (C-x ~) List the possible completions of the text before point, treating it as a username.
  complete-variable (M-$) Attempt completion on the text before point, treating it as a shell variable.
  possible-variable-completions (C-x $) List the possible completions of the text before point, treating it as a shell variable.
  complete-command (M-!) Attempt completion on the text before point, treating it as a hostname. 

 

Online References