The Command Line

Chris Whitehouse
3 min readOct 22, 2020

--

First homework from Makers: The Command Line.

Post edit: I’ve created a simple command line command learner, if anyone would like to have a play: https://github.com/chriswhitehouse/command-line-practice

The command line is the window to the soul of your computer…kinda…

Photo by StellrWeb on Unsplash

First impressions of the command line: for coding newb like me, I can see the command line is the first stepping stone from the safe coding nurseries of edtech platforms into coding ‘for real’.

It is an incredibly powerful tool. Stripping away the graphical user interfaces we’re accustomed to as computer users, but empowering us with direct access to the underlying folder and file structures. Allow quick and direct access to navigate, manipulate, and automate folders and files.

Accessed through ‘bash’ (Bourne Again SHell), there are an array of short cut commands to learn.

Navigation

Navigating through directories quickly and easily with a few keyboard commands:

  • pwd: Print Working Directory
  • mkdir: Make a directory
  • ls: list contents of folder
  • cd: change directory
  • touch: create new file

Manipulation

Make amendments the directory and the files with another set of simple commands:

  • cp: copy files or directories
  • Command options: -l, -a, -t or in combination; -alt. List all files, in longform, in time stamp order
  • mv: to move a file form one directory to another
  • rm: the scary one(!) deletes a directory and all its contents.

Redirection

Redirect the ‘standard output’, ‘standard input’ and ‘standard error’ using the following commands:

  • >: redirects the standard output of a command to somewhere other than the terminal i.e. a file. When redirected to an existing file, the new output will overwrite the existing content.
  • >>: redirects the standard output of a command and appends it to the new output location i.e. to the bottom of an existing file.
  • |: ‘pipe’ will redirect output from a command to the input of another command.
  • cat: displays the content of a file in the terminal.
  • grep: searches folders and file names, and file contents for particular ‘regular expression’ string patterns. -i makes the search case insensitive. -R makes the search recursive i.e. looking through all sub directories, files, and file contents.

Configuration

Amend how the bash environment works, personalising for how you prefer:

  • env: returns a list of environment variables
  • alias: is used to assign a short cut a command i.e “p” = pwd would then allow p to print working directory.
  • ~/.bash_profile: stores the environment variable and commands. any edits must be enabled with source ~/.bash_profile in the command terminal.
  • history: prints a list of historic commands
  • clear: clears the terminal of all prior commands

Bash Scripting

Automate processes through bash scripts.

  • #!/bin/bash: is used at the top of a script (shebang) to inform the computer to use bash as the interpreter
  • Arguments: arguments in a bash command can be accessed using $1 for the first argument, $2 for second etc.
  • Variables: similarly, a defined bash variable can be accessed using the $ symbol i.e. greeting = “hello”, echo $greeting
  • echo: prints a string to the terminal
  • read: requests input from the terminal
  • alias: can be used to create a shortcut for a bash script, with predefined arguments.

--

--

No responses yet