📂
SEI 1019
  • Introduction
  • About These Notes
  • Syllabus
  • Development Workflow
    • Command Line
      • The Terminal
      • Filesystem Navigation
      • File Manipulation
      • Additional Topics
    • Intro to Git
      • Version Control
      • Local Git
      • Remote Git
      • Git Recipes
    • Group Collaboration
      • Git Workflows
      • Project Roles and Tools
    • VS Code Tips & Tricks
  • HTML/CSS
    • HTML
    • CSS Selectors
    • CSS Box Model and Positioning
      • Box Model
      • Display and Positioning
      • Flexbox
      • Grid
      • Flexbox & Grid Games
      • Floats and Clears
      • Additional Topics
    • Advanced CSS
      • Responsive Design
      • Pseudo-Classes/Elements
      • Vendor Prefixes
      • Custom Properties
      • Additional Topics
    • Bootstrap
    • CSS Frameworks
    • Accessibility
  • JavaScript
    • Primitives
    • Arrays
    • Objects
    • Control Flow
      • Boolean Expressions
      • Conditionals
      • Loops
      • Promises
    • Functions
      • Callbacks
      • Timing Functions
      • Iterators
    • DOM and Events
    • DOM Manipulation
    • HTML5 Canvas
    • How To Reduce Redundancy
    • (2019) JavaScript OOP
    • (2016) OOP with Classes
    • (1995) OOP with Prototypes
      • Constructors
      • Prototypes
    • Intro to TDD
    • Scoping
    • Inheritance
      • Prototypal Inheritance
      • Call, Apply, and other Functions
      • ES6 Inheritance
      • Resources
    • Custom Node Modules
    • Additional Topics
      • AJAX, Fetch, and Async/Await
      • AJAX w/JSON and Localstorage
        • AJAX w/JSON
        • Local Storage
      • Async module
      • Data Scraping
  • jQuery
    • Intro
      • DOM Manipulation
      • Reddit Practice
      • Styling
      • Events
    • Plugins
    • AJAX
  • APIs
    • Fetch
    • AJAX w/jQuery
    • AJAX w/Fetch
  • Databases
    • Intro to SQL
    • Advanced SQL
    • MongoDB
      • Intro to NoSQL
      • CRUD in MongoDB
      • Data Modeling
      • Intermediate Mongo
  • Node/Express
    • Node
      • Intro to Node
      • Node Modules
      • Node Package Manager (NPM)
    • Express
      • Intro to Express
        • Routes
        • Views
        • Templates
        • Layouts and Controllers
        • CRUD & REST
          • Get and Post
          • Put and Delete
      • APIs with Express (request)
      • APIs with Express (axios)
    • Sequelize
      • Terminology
      • Setup
      • Using Models
      • Seeding Data
      • Validations and Migrations
      • Resources
      • 1:M Relationships
      • N:M Relationships
    • Express Authentication
      • Research Components
      • Code Components
      • Auth in Theory
        • Sessions
        • Passwords
        • Middleware
        • Hooks
      • Auth in Practice
        • Create the User
        • User Signup
        • Sessions
        • User Login
        • Authorization and Flash messages
    • Testing with Mocha and Chai
    • Mongoose
      • Mongoose Associations
    • JSON Web Tokens
      • Codealong
    • Additional Topics
      • oAuth
      • Geocoding with Mapbox
      • Geocoding and Google Maps
      • Cloudinary
      • Websockets with Socket.io
      • SASS
  • Ruby
    • Intro to Ruby
    • Ruby Exercises
    • Ruby Classes
    • Ruby Testing with Rspec
    • Ruby Inheritance
    • Ruby Data Scraping
  • Ruby on Rails
    • Intro to Rails
    • APIs with Rails
    • Asset Pipeline
    • Rails Auth and 1-M
      • Auth Components
    • Rails N:M
    • ActiveRecord Polymorphism
    • Additional Topics
      • oAuth
      • SASS
      • Rails Mailers
      • Cloudinary
      • Jekyll
  • React (Updated 2019)
    • ES6+/ESNext
      • Const and Let
      • Arrow Functions
      • Object Literals and String Interpolation
      • ES6 Recap
      • ES6 Activity
    • Intro to React
      • Create React App
      • Components and JSX
      • Virtual DOM
      • Props
      • Dino Blog Activity
      • Nested Components
      • Lab: LotR
    • React State
      • Code-Along: Mood Points
      • Code-Along: Edit Dino Blog
      • Lab: Simple Calc
      • Lifting State
    • React Router
      • Browser History/SPAs
      • React Router (lesson and full codealong)
      • Router Lab
    • Fetch and APIs
      • APIs with Fetch and Axios
      • Fetch the Weather
    • React Hooks
    • React LifeCycle
      • Lab: Component LifeCycle
    • React Deployment
    • Additional Topics
      • React Frameworks
        • Material UI Theming
      • Typescript
        • More Types and Syntax
        • Tsconfig and Declaration Files
        • Generics with Linked List
      • Redux
      • TypeScript
      • Context API
      • React Native
  • Meteor
  • Deployment and Config
    • Installfest
      • Mac OSX
      • Linux
      • Git Configuration
      • Sublime Packages
    • Deploy - Github Pages
    • Deploy - Node/Sequelize
    • Deploy - Node/MongoDB
    • Deploy React
    • Deploy - Rails
      • Foreman (Environment Variables)
    • Deploy - AWS Elastic Beanstalk
    • Deploy - S3 Static Sites
    • Deploy - Django
    • Deploy - Flask
  • Data Structures and Algorithms
    • Recursion
    • Problem Solving - Array Flatten
    • Binary Search
    • Algorithm Complexity
    • Stacks and Queues
    • Bracket Matching
    • Ruby Linked Lists
      • Sample Code
      • Beginner Exercises
      • Advanced Exercises
    • JS Linked Lists
      • Sample Code
      • Beginner Exercises
      • Beginner Solutions
    • Hash Tables
    • Intro to Sorting
    • Insertion Sort
    • Bucket Sort
    • Bubble Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort
    • Sorting Wrapup
    • Hashmaps
    • Trees and Other Topics
  • Python
    • Python Installation
    • Intro to Python
    • Python Lists
    • Python Loops
    • Python Dictionaries
    • Python Sets and Tuples
    • Python Cheatsheet
    • Python Functions
    • Python Classes
    • Python Class Inheritance
    • Intro to Flask
    • Intro to SQLAlchemy
      • Flask and SQLAlchemy
    • Using PyMongo
    • Intro to Django
    • CatCollector CodeAlong
      • URLs, Views, Templates
      • Models, Migrations
      • Model Form CRUD
      • One-to-Many Relations
      • Many-to-Many Relations
      • Django Auth
    • Django Cheatsheet
    • Django Auth
    • Django Polls App Tutorial
    • Django School Tool Tutorial
    • Django 1:M Relationships
    • Custom Admin Views
    • Data Structures and Algorithms
      • Recursion
      • Binary Search
      • Stacks and Queues
      • Linked Lists
      • Binary Trees
      • Bubble Sort
      • TensorFlow & Neural Networks
    • Adjacent Topics
      • Raspberry Pi
      • Scripting
  • Assorted Topics
    • History of Computer Science
    • Regular Expressions
    • Intro to WDI (Course Info)
    • Being Successful in WDI
    • Internet Fundamentals
      • Internet Lab
    • User Stories and Wireframing
      • Wireframing Exercise: Build an Idea
    • Post WDI
      • Learning Resources
      • Deliverables -> Portfolio
      • FAQ
  • Projects
    • Project 1
    • Project 2
    • Project 3
      • Project 3 Pitch Guidelines
    • Project 4
    • Past Projects
      • Project 1
      • Project 2
      • Project 3
      • Project 4
      • Portfolios
    • Post Project 2
    • MEAN Hackathon
      • Part 1: APIs
      • Part 2: Angular
    • Portfolio
  • Web Development Trends
  • Resources
    • APIs and Data
    • Tech Websites
    • PostgreSQL Cheat Sheet
    • Sequelize Cheat Sheet
    • Database Administration
  • Archived Section
    • (Archived) ReactJS
      • Intro to React
        • Todo List Codealong
        • Additional Topics
      • Deploy React
      • React with Gulp and Browserify
        • Setting up Gulp
        • Additional Gulp Tasks
      • React Router
        • OMDB Router
        • OMDB Search
        • Additional Resources
      • React Animations
        • CSS Animations
    • AngularJS
      • Intro to AngularJS
        • Components and SPA
        • Create an Angular App
      • Angular Directives and Filters
      • Angular Animation
      • Angular Bootstrap Directives
        • Bootstrap Modals
      • Angular $http
      • Angular Services
        • Service Recipes
        • ngResource
        • Star Wars Codealong
      • Angular Routing
      • Angular + Express
      • Angular Authentication
        • Additional Topics
      • Angular Components
      • Angular Custom Filters
      • Angular Custom Directives
Powered by GitBook
On this page
  • Common File Manipulation Commands
  • mkdir - Make a directory
  • touch - Create a file
  • cat - Reading and concatenating files
  • echo - Writing text to standard output
  • > and >> - File Redirection
  • | - Piping
  • grep - Searching files
  • mv - Moving files
  • cp - Copying files
  • rm - Removing files
  • Learning More About Commands

Was this helpful?

  1. Development Workflow
  2. Command Line

File Manipulation

Common File Manipulation Commands

  • mkdir - make a directory

  • touch - make a file

  • mv - move a file/directory

  • cp - copy a file/directory

  • rm - remove a file/directory

  • sort - sort text files

  • grep - search text files

  • echo - write text to standard output

  • cat - read and concatenate files

  • > - redirect output to a file

  • >> - redirect and append output to a file

  • | - pipe output to another command

mkdir - Make a directory

Make a directory using mkdir, which accepts the name of the new directory as an argument. Note that when naming directories, using hyphens or underscores is recommended when separating words (don't use spaces).

mkdir living_room

Let's cd into our new living_room Look around with ls, and ls -la. What do you see?

touch - Create a file

Creating a file can be done by using the touch command. Then, the file can be opened in VS Code for editing.

touch books.txt

Now you can open that up in your editor (VS Code):

code books.txt

Add a few books, copy and paste the section below so we all have some books in common, and save the file. Make sure the books you add are in the same format: <author_given_name>, <author_last_name>:<title>.

Carroll, Lewis:Through the Looking-Glass
Shakespeare, William:Hamlet
Bartlett, John:Familiar Quotations
Mill, John :On Nature
London, Jack:John Barleycorn
Bunyan, John:Pilgrim's Progress, The
Defoe, Daniel:Robinson Crusoe
Mill, John Stuart:System of Logic, A
Milton, John:Paradise Lost
Johnson, Samuel:Lives of the Poets
Shakespeare, William:Julius Caesar
Mill, John Stuart:On Liberty
Bunyan, John:Saved by Grace

Now try ls -la again. Do you see the books.txt file?

cat - Reading and concatenating files

A quick way to read files without opening your editor is by using cat.

cat books.txt

If we had another file, we could provide additional filenames as arguments in order to concatenate files together.

cat books.txt schedule.txt

echo - Writing text to standard output

echo is a command that echoes (outputs) what we give to it as arguments.

echo "This bookshelf flexes under the weight of the books it holds."

At first glance, it seems too simple. Why would we need this command? Well every command that we run in the terminal has an input, an output, an error output, and arguments/options. Since echo produces output, we can change where this output will go!

> and >> - File Redirection

Let's try redirecting the output from echo to a file.

echo "This bookshelf flexes under the weight of the books it holds" > bookshelf.txt

Using the closing angle bracket > in this way is called redirection. We are saying:

  • Run echo with this string as an argument

  • Take the output, and put it in a new file called bookshelf.txt.

Try running ls again, and cat our new file.

Two angle brackets >> works similarly, but it appends the string to the end of the file.

echo "It does not break, it does its job admirably" >> bookshelf.txt

Try cat bookshelf.txt to see the result

| - Piping

Let's look back at books.txt. Look at the file contents. Notice that the list of books is unsorted. We need to organize this using the sort command.

We can use the | character to pipe output into another command, specifically the sort command. This is different from file redirection because we're directing output to the input of a command, not a file.

cat books.txt | sort

Note that if we look at books.txt, nothing changed. We read the contents of books.txt and piped the contents into sort, but the output was never saved. Luckily, we can combine piping with file redirection.

cat books.txt | sort > sorted_books.txt

Look around again to see how the room has changed.

grep - Searching files

There are dozens of powerful tools we can leverage using pipes. One of the ones you'll be using the most is grep.

cat books.txt | grep Mil

See how we filtered out just the lines that contain Mil? Try grepping for something else. There are also additional options that can be passed to both sort and grep.

mv - Moving files

Now that we have our books sorted, we really don't need our unsorted list of books. mv stands for move, and that's how we move files and folders from place to place.

mv sorted_books.txt books.txt

cp - Copying files

To copy files, we use the cp command. Let's try adding a second bookshelf.

cp bookshelf.txt second_bookshelf.txt

Note that if we copy a folder, we'll need to use an additional option, -r.

rm - Removing files

To remove files, we use the rm command. Let's go back and remove that second bookshelf.

rm second_bookshelf.txt

Note that if we remove a folder, we'll need to use an additional option, -r and sometimes -f.

IMPORTANT NOTE: This does not send files to the trash can or recycle bin. Your files are gone forever, so be careful when using this command!

Learning More About Commands

Happily, the manual for every standard shell command is embedded in every Unix-derived system. It is as easy to access as typing man name-of-command. The word man is short for 'manual' and typing that will bring up the documentation for all the commands you will need to know.

Let's see if we can learn a bit more about the grep command. In your terminal, type the following:

man grep

This is a Unix manual page. You can exit it at any time by typing the letter 'q'. You can use the mouse pad to scroll or the arrow keys. Look in the section labeled 'SYNOPSIS' and you will see a listing of every command line option that you can use with grep. A bit further down in the 'DESCRIPTION' section you will find an explanation of every option.

Take 5 minutes and see if you can find the answers to the following questions:

  1. How can I print the line number in the file where the match is found? (For example, if I grepped for Jack in the list of books, I want it to show that it is found on line 5.)

  2. How can I also print the 3 lines after the line where the match is found?

  3. What if I wanted to print the 2 lines before the match as well as the 3 lines after the match? (Hint: There are actually two ways to do this. What would be the cleanest way?)

PreviousFilesystem NavigationNextAdditional Topics

Last updated 4 years ago

Was this helpful?

Adapted from

http://en.flossmanuals.net/command-line/piping/