Harness the Combinatoric Power of Command-Line Tools and Utilities

Bash Basics

Lesson 4 - Working with Files

Bash

The command line excels at file manipulation. In this lesson you’ll learn various methods for reading and creating files.

Listing Files

In the previous lesson, you discovered that the ls command lists files in the current directory:

ls

This listing only shows filenames. However, the following command shows more details:

ls -la

The -l flag shows detailed information, and -a shows hidden files.

Creating and Viewing Files

In the first lesson you used echo to print text to the screen. You can use echo to create files by redirecting output from the screen into a file:

cd /tmp
echo "Hello from a file" > greeting.txt
cat greeting.txt

The > operator redirects output to a file, creating it if it doesn’t exist.

Your Turn

Create a file called notes.txt with some content, then display it:

cd /tmp

Useful File Commands

Here’s a quick reference:

CommandPurpose
lsList files
catDisplay file contents
touchCreate empty file
rmRemove file
cpCopy file
mvMove/rename file

What You Learned

  • ls lists directory contents
  • > redirects output to a file
  • > redirects output and appends it to a file
  • cat displays file contents

You’ve completed the Bash Basics course.