Skip to content

Latest commit

 

History

History
90 lines (69 loc) · 2.98 KB

01-3-P-Terminal-Basics.adoc

File metadata and controls

90 lines (69 loc) · 2.98 KB

Command Line Basics

This section shows a few handy terminal commands.

Windows prerequisites

This step can be skipped if you are using MacOS or Linux. However, if you are using Windows, you need to have a terminal that supports the execution of basic Linux commands. Such programs are Git Bash or MinGW, for example. You can find below a few helper steps to get MinGW running on your system.

  1. Get the MinGW installer from here

  2. Install it to wherever you like, the default installation folder is C:\MinGW

  3. Once the setup finishes, open the MinGW Installation Manager

  4. Select the two packages for installation as shown in the figure below
    MinGW Base Packages

  5. Click on Installation/Apply Changes. This will take a few moments to fetch and install the required packages.

  6. You can open a terminal window by running the executable C:\MinGW\msys\1.0\bin\bash.exe

Basic file system operaions

  1. Open a terminal, and try the following commands:

    • pwd: prints the present working directory
      Example:

      $ pwd
      /home/ecse321
    • ls: lists the content of a given folder
      Example:

      $ ls /home
      ecse321 guest-user admin
    • cd: navigates the file system
      Example:

      $ cd ..
      $ pwd
      /home
      $ cd ecse321
      $ pwd
      /home/ecse321
      Note
      The following steps will include images that illustrate the commands and their output to prevent easy copy-paste. Sorry! :)
  2. Creating files and reading/writing their contents

    • touch: creates a file

    • mkdir: creates a directory

    • mv: moves a file (or directory) from its current location to a target location

    • echo: prints a string

    • cat: prints the contents of a file
      Example:

      terminal touch ls echo cat mkdir mv

Finding files

The versatile find command allows us to find files based on given criteria. Take look at its manual page with man find!

Example:

terminal find

Batch file operations

  • sed: stream editor; changes a given string to a replacement

Combining find with an additional command (e.g., sed) can greatly speed up your repetitive tasks.
Example:

terminal find sed mv

Note
The file helloworld.txt in the example is initially a copy of greeting.txt.

Some additional useful commands

  • rm: removes a file

  • cp -r: copies a directory recursively with its contents

  • rmdir: remove an empty directory

  • rm -rf: force to recursively delete a directory (or file) and all its contents

  • nano: an easy-to-use text editor (not available by default in MinGW)

  • grep: finds matches for a string in a given stream of characters

  • ag: takes a string as argument and searches through the contents of files recursively to find matches of the given string (this tool is included in the silversearcher-ag package)