Lesson 4 - Working with Files
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:
lsThis listing only shows filenames. However, the following command shows more details:
ls -laThe -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.txtThe > 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 /tmpUseful File Commands
Here’s a quick reference:
| Command | Purpose |
|---|---|
ls | List files |
cat | Display file contents |
touch | Create empty file |
rm | Remove file |
cp | Copy file |
mv | Move/rename file |
What You Learned
lslists directory contents>redirects output to a file>redirects output and appends it to a filecatdisplays file contents
You’ve completed the Bash Basics course.