Skip to content
Home » META » Meta Front-End Developer Professional Certificate » Version Control » Week 2: Command Line

Week 2: Command Line

In this module you will learn how to use the command line to execute commands in Linux. You will be introduced to some of most commonly used commands that traverse, create, rename, and delete files on your hard drive. You will learn how easy it is to use piping and redirection to create powerful workflows that will automate your work, saving you time and effort.

Learning Objectives

  • Describe how the command line is and how it is used.
  • Practice traversing your hard drive via the command line.
  • Create, rename and delete files and folders on your hard drive using Unix commands.
  • Use pipes and redirection.

Unix Commands


Video: The Command line

Summary: Human-Computer Interaction

Key points:

  • Human-computer interaction (HCI) involves exchanging information with computers via various input and output devices.
  • Common input devices: keyboard, mouse, microphone, camera, touch screens.
  • Common output devices: speakers, monitors, headsets, haptic devices.
  • Graphical user interfaces (GUIs) provide a user-friendly way to interact but limit the scope of HCI.
  • Command line offers a powerful alternative for experienced users, allowing:
    • Performing tasks faster with fewer errors.
    • Automating tasks.
    • Controlling remote servers.
    • Downloading files.
    • Running virtual software.
  • Basic commands covered:
    • cd: Change directory.
    • touch: Create new file.
    • mkdir: Create new folder.
    • history: View command history.
  • Example scenario: Creating a folder, file, and opening it in VS code.
  • Practice makes perfect – get comfortable using the command line for more advanced tasks.

Takeaway:

Understanding HCI beyond GUIs unlocks advanced computer interaction possibilities. Learning even basic command line skills can significantly enhance your productivity and control.

To create a file called myfile.html what command would you use?

touch myfile.html

You are correct! You will use the touch myfile.html file to create the file .

One of the first things you learn to do when you
use a computer for the first time is to operate the mouse and type
on the keyboard. At first, it goes slow. But as you become
more competent, you interact with your computer and it responds as
you want it to. But what does it really mean to interact with your computer? In the context of
using a computer, the term interact
simply means to exchange information
or even simpler, send and receive information. Essentially, a computer sending data to you and you receive it. In turn, you also
send some data to your computer and the
computer receives it. I’ve talked about the
mouse and the keyboard, but can you think
of other ways in which you and your
computer interact? Computers have various
input and output devices. The input devices include
a keyboard, mouse, microphone, camera, touch-sensitive
devices, and so on. The output devices are
things like speakers, monitors, headsets, and haptic devices to
name just a few. You use all these
devices to send data to a computer and
receive data from it. But there’s something else that supports communication
with your devices. These are graphical
user interfaces or GUIs which facilitate
your interactions. GUIs are popular because they require very little
training to use. GUIs offer an easy way to
interact with devices, but they also somewhat limit the scope of human-computer
interaction. As an alternative to GUIs and input devices such
as microphones, you will learn to interact with your computer through
the command line. The command line is a very powerful alternative
because it allows developers to perform tasks quicker and with
enough experience, less potential for errors. To use this powerful
tool effectively, you need to have a certain
level of knowledge. You might feel that
the learning curve for the command line
is a bit steep, but take it from me, the
payoff is definitely worth it. By learning just a few commands, you can perform various tasks, such as creating new
directories, creating new files, combining directories, copying
and moving files around different directories, and
searching through files using various criteria and keywords. As you become more advanced
in using the command line, you will be able
to perform tasks such as track software, access and control
remote servers, search for files using
specific criteria, unzip archives, access
software manuals and display them in
the command line. Install, upgrade, and
uninstall software, and mount and unmount
computer drives, or check disk space and so on. Pretty advanced stuff,
don’t you think? But the list goes on. You can write scripts to
automate various tasks, control user access to
files and programs, stop, start and
restart programs. Create aliases of
only a few characters long to initiate
very long commands, download files
from the Internet, run various software,
and finally, run and control self-contained
virtual software, which is also known
as containerization. There are many, many ways
to use the command line. But for now, I will
guide you through some basic commands
to get you started. First, the cd command, which stands for
change directory. This is used to point our command line to a
specific directory. For instance, a certain folder. For example, on Linux, if I type cd tilde, forward slash and desktop, I will point the command line to the desktop of my computer. When you type cd.. you will move out of the current directory and back into the
parent directory. Next is the touch command, which makes a new file of
whatever type you specify. For example, to build
a brand new file, you can run touch followed
by the new file’s name, for instance, example.txt. Note that the newly created
file will be empty. You can also make new folders
using the mkdir command. For example, mkdir followed by the title you want
to give the new folder. To view a history of the most
recently typed commands, you can use the history command. There are many other
commands that you can use, but with the ones
I just introduced, you can already do quite a lot. I’ll take you through a quick
scenario as an example. Let’s say you want to
point the command line to the desktop directory
and then add a new folder there
titled myjsproject. Next, you want to point
the command line to the myjsproject directory
and make a new file, which you will call example.js. Finally, you want to open the example.js file in VS code. To do all of this, you will need to run
the following commands. The first action
you’ll do is to use the change directory
or cd command. Then you want to use the mkdir command to
make the new folder. To move into the new
folder directory, you use the cd command again, and then you use the touch
command to create the file. The final command is
the code command, which will open the
file in VS code. If you’ve run all these
commands correctly, you’ll end up with a myjsproject
directory on the desktop with the example.js file
inside of it and additionally, that example.js file open inside VS code
ready to be edited. In this video, you discovered
that you can interact with computers on a more
advanced level through the command line. You now have a better
idea of what kind of advanced tasks the command
line allows you to do. You are also ready to try
out a few basic commands. I encourage you to start practicing some of
these commands. Just like you got
better and better at typing and moving the
cursor with your mouse, I assure you that with
practice you will soon use command
line like a pro.

Video: What are Unix commands?

Summary of the passage about Unix commands:

Importance of Unix commands for developers:

  • Knowing Unix commands is valuable in today’s software development world, especially for tasks like folder creation or renaming files.
  • Most companies run their platforms on the cloud, many using Linux, which heavily relies on Unix commands.

Unix origins and relation to Linux:

  • Unix predates Linux and served as its inspiration.
  • Many Unix commands used today originated from Unix and work on modern Linux flavors.

Command line vs. GUI:

  • Command line may seem intimidating initially, but it’s just a layer below the GUI actions we perform.
  • Windows became popular due to its easier-to-use GUI, but developers still utilize Unix commands for tasks.

Exploring basic Unix commands:

  • man: displays detailed instructions for a specific command.
  • flags: modify the behavior of commands like ls (-l for long format, -a for hidden files).
  • common commands:
    • cd: change directories.
    • ls: list directory contents.
    • pwd: print working directory path.
    • cp: copy files/folders.
    • mv: move files/folders.

Takeaway:

Understanding Unix commands gives developers more control and flexibility compared to just using GUI interfaces. The next video will demonstrate these commands in action.

There is a folder called bi_project. What command would you use to change directory into that folder?

cd bi_project

You are correct! You use the cd command to change directories.

I’m pretty sure you
use your phone to perform a number of activities, such as sending messages, shopping online and
watching videos. You simply tap your
screen, scroll and swipe. But have you ever thought
of how your phone responds to your tapping,
scrolling, and swiping? You interact with your
phone and computer through a graphic user interface or GUI, which is just a layer above underlying commands that
tells the device what to do. Developers, however,
need to know how to use specific commands to perform
various types of tasks. For example, to create a
new folder on the desktop, you right-click and
choose New Folder. In the command line, use the specific command mkdir to achieve the same result. Having a grip on UNIX
commands specifically is a great skill to have in today’s software
development world. In this video, you
will get started with a few basic Unix commands. Did you know that
the majority of companies run their
platforms on the Cloud, and 90 percent of these systems run on a platform called Linux? You might be wondering why
I am discussing Linux, while the topic of this
video is Unix commands. To answer this, let’s
explore some history. Unix preceded Linux and was
developed by Ken Thompson and Dennis Ritchie and team
at AT&T Labs in 1969. Linux came much later
on and was originally developed as a hobby
by Linus Torvalds, hence the name Linux. The commands that
you will explore in this video originated
from the Unix platform, but you can use them in most modern environments that
run some flavor of Linux. Using the command
line could seem a little intimidating at first, but you will quickly learn
that Unix commands are simply a layer below
the normal actions, such as opening file
directories or renaming files. Windows, for example, became the dominant
desktop operating system, mainly due to it’s
easier to use GUI. Windows allowed non-technical
users to perform tasks without having to
learn a list of commands. But you, as an
aspiring developer, will get to use Unix commands
to develop those tasks. Before I delve into some of
the most common commands, it’s important to note that each command has a set
of helper instructions. These helpers give
detailed information about how the commands can be run and how something we
call flags can be passed. One of these helpers
is the man command. Man is short for manual, and when called
against a command, it will display a
detailed manual of instructions for
that given command. You can also use the
command man space ls, which will show the
detailed manual of instructions for the
list command ls. We can also use something called flags in conjunction
with Unix commands. Flags are used to modify
the behavior of a command. Think of them as options
that can either change or extend the functionality
of the given command. Next, you will learn
about some of the most common to use
Unix commands, and in the next video, you will see some
of them in action. The cd or change
directory command is used to move from
different directories of the file system. You can learn more
about working with relative and absolute paths from the additional reading
at the end of this lesson. Ls is used to show the contents of the
current working directory. The ls command can accept
many different types of flags that will change what is returned
in the response. For example, ls -l, lists the file out, in list order, and shows the read or
write permissions, owners and groups it belongs to. Ls -a, on the other hand, will list all files and directories including
hidden ones. The PWD or print working directory command shows the full path of the
current working directory. The copy or cp
command copies files or folders from one
destination to another, and the MV, move command, moves files from one
directory to another. In this video, you
learned about some of the most commonly
used Unix commands. Next time you use your device, think about the commands
that run underneath the GUI to complete
tasks and commands.

Reading: Using Bash on Mac Terminal

Video: Using Bash on Windows

Summary of Bash Shell Scripting Tutorial:

Key Points:

  • Navigation: cd ~/ navigates to home directory, ls -la lists all files including hidden ones.
  • Configuration files:
    • .bashrc: for shell configurations like colors and history settings.
    • .bash_profile: for setting environment variables like Java/Python home directories.
  • Creating a shell script:
    • Use vim editor, create file like testshell.sh with :.w!.
    • Start with shebang line: #!/bin/bash.
    • Use echo command to print output (e.g., “Hello World”).
    • Save and exit vim with :wq!.
  • Making script executable:
    • Use chmod 755 testshell.sh to set execute permission.
  • Running script:
    • Use ./testshell.sh to run the script and see its output.

Learning Outcome:

This tutorial provides a basic understanding of creating and running simple shell scripts in the Bash environment. It covers navigating directories, understanding configuration files, writing script commands, and making scripts executable.

Remember:

  • Use appropriate commands for navigation and file management.
  • Add shebang line and echo commands to your scripts.
  • Set execute permission before running your scripts.

Bash Shell Scripting Tutorial: Automate Your Tasks with the Power of Bash

The Bash shell is a powerful tool for interacting with your computer. But it can do more than just execute commands one at a time. With Bash scripting, you can automate repetitive tasks, create custom tools, and even build complex applications.

This tutorial will guide you through the basics of Bash shell scripting, from setting up your environment to writing your first scripts.

1. Getting Started:

Open your terminal window. This is where you’ll interact with the Bash shell.

2. Navigating the Filesystem:

Use the cd command to move between directories. For example, cd ~/Documents will take you to your Documents folder.

To see a list of files and folders in the current directory, use the ls command. You can add -l to see more details about each file, like its size and permissions.

3. Working with Files:

Use the cat command to view the contents of a file. For example, cat README.md will display the contents of the README file.

To create a new file, use the touch command. For example, touch myscript.sh will create a new file called myscript.sh.

4. Basic Scripting Commands:

  • Echo: Print text to the terminal.
  • Variables: Store data for later use. For example, name="John" creates a variable called name with the value “John”.
  • Comments: Explain your code for human readers. Start comments with a pound sign (#).
  • Conditional statements: Control the flow of your script based on conditions. Use if, else, and fi to create branching logic.

5. Writing Your First Script:

Let’s create a simple script that greets you by name:

Bash

#!/bin/bash

# Ask for the user's name
read -p "What is your name? " name

# Greet the user
echo "Hello, $name!"

Save this script as greet.sh and make it executable with chmod +x greet.sh. Then, run the script with ./greet.sh. You should see a prompt for your name, followed by a greeting message.

6. Building More Complex Scripts:

As you learn more, you can add features to your scripts, like:

  • Loops: Repeat tasks a certain number of times.
  • Functions: Define reusable blocks of code.
  • Error handling: Deal with unexpected situations gracefully.

7. Learning Resources:

Remember:

  • Start small and practice.
  • Don’t be afraid to experiment!
  • There’s a vast community of Bash users online who can help you if you get stuck.

With dedication and practice, you can become a proficient Bash shell scripter and unlock the full potential of your command line.

Bonus Tip:

Check out online repositories like GitHub for examples of Bash scripts to learn from and get inspiration for your own projects.

Okay. I’ve opened up
my terminal window. Let’s navigate to
my home directory. I type the CD command
and then a tilde, followed by the Enter key. Then I use the LS-LA command to return all of the files in a list including
all hidden files. Notice two files, a bashRC
file and a bash profile file. For now, I want you to focus
on the bash RC file first. I can use the command
les.bashRC to check this file. The bashRC file is mainly
for configurations. It is essentially a
script file that’s executed when you first
open the terminal window. What’s in there will
be configurations for the shell itself, for example, the types of
colors that I’m using. I can also add in things
around my shell history, like how much history of previous commands I want
to store and so on. Any configuration options
that I put in here will be executed when the
terminal session begins. I press the Q key to exit
the less environment. The other file is the
bash profile file. I can run the less command
again, this time with.profile. This tends to be used more
for environment variables. For example, I can use it for setting
environment variables for my Java home directory or my Python home directory or whatever is needed
during development. Again, I press the
Q key to exit. Now, I will create a
simple shell script. For this example,
I will use Vim, which is an editor that I
can use which accepts input. Type Vim and then I
create a new file by typing the test shell.sh
and press the Enter key. Then at the top of the file, I put in what type of
file I want it to be. In this case, it’s going
to be a bash file. If I press the I
key on my keyboard, it’ll set insert mode. Then I put in a hash symbol followed by an exclamation mark, a forward slash, the word bin, another forward slash
and then the word bash. This let’s to the
operating system know that this is a bash script. This script is very simple. I want to print out some type
of text onto the screen. I use the echo command and type in what I
want to print out. In this case, HelloWorld, press “Escape” to get
out of insert mode. Then I type:wq! to save the file. Press “Enter”. If I look in the
directories now, notice there’s now a
file named testshell.sh. The other thing
to notice is that this file can’t be
run at the moment. In other words, it’s
not executable. It’s just a read write file. But I want it to be executable, which requires that I have
an x being set on it. In order to do that, I have to use another command
which is called chmod. After using this command, I type in the type of
permissions that I want. I type in 755. Then I want to set the file that I want
to add the permissions to, which is testshell.sh. If I use the LS-LA
command again, I notice that the
file has now been turned into an executable file. This means that I can now run the file from the command line. To run the file, I press./testshell.sh
followed by the Enter key. Now you notice the words hello world are printed
out on the screen. This is how you can
create simple scripts and make them executable
within the bash shell.

Video: Change directories and list contents

Video: Creating and moving directories and files

Summary: File and Directory Management in the Terminal

Key Points:

  • Navigated directories with cd: checked current directory with pwd, created submissions directory, and switched into it.
  • Created files with touch: added test1.txt and test2.txt inside submissions.
  • Listed directory contents with ls: verified files and directories using ls-l for details.
  • Created another directory archive at root level.
  • Moved the submissions directory with files into archive: used mv submissions archive.
  • Verified successful move with ls-l in both directories.

Learnings:

  • Basic commands for creating, listing, and moving directories and files.
  • Using cd and pwd to navigate the file system.
  • Moving directories and files with mv and verifying their location.

Remember:

  • Commands like mkdir, touch, and mv require specifying file/directory names.
  • ls-l shows detailed information about directories and files.
  • Use cd.. to move up one directory level in the hierarchy.

File and Directory Management in the Terminal: Tame Your Digital Landscape

The terminal might seem intimidating at first, but it’s a powerful tool for managing your files and directories. This tutorial will walk you through the essential commands you need to know to organize your digital world like a pro.

Navigating the File System:

Think of your computer’s storage as a tree. The root directory (/) is the trunk, from which branches (directories) and leaves (files) sprout. Let’s see how to explore:

  • pwd: Shows your current location in the tree (e.g., /home/you).
  • cd: Change directories. Think of climbing branches!
    • cd ..: Climb up one branch (to the parent directory).
    • cd directory_name: Move to a specific directory (e.g., cd Documents).

Listing Files and Directories:

Once you’re in a directory, you need to see what’s inside:

  • ls: Lists the contents of the current directory.
  • ls -l: Shows detailed information, like file size and permissions.

Creating and Deleting Files and Directories:

Need to organize your files or clear out clutter? These commands are your friends:

  • mkdir directory_name: Create a new directory (e.g., mkdir projects).
  • touch file_name: Create an empty file (e.g., touch report.txt).
  • rm file_name: Delete a file (be careful!).
  • rmdir directory_name: Delete an empty directory.

Moving and Renaming Files and Directories:

Sometimes, files and directories need to be relocated or renamed for better organization:

  • mv old_name new_name: Rename a file or directory.
  • mv file_name directory_name: Move a file to another directory (e.g., mv report.txt Documents).

Bonus Tip: Use the tab key for autocompletion! It saves you typing and avoids typos.

Remember:

  • Always double-check file names before deleting or moving them.
  • Use descriptive names for files and directories to stay organized.
  • Practice makes perfect! The more you use the terminal, the more comfortable you’ll become.

Further Exploration:

This is just the tip of the iceberg! The terminal offers a vast array of commands for managing your files and directories. Here are some resources to delve deeper:

So, open your terminal, roll up your sleeves, and start mastering your digital landscape!

Pro Tip: Feeling adventurous? Try combining these commands to create powerful workflows. For example, you can pipe the output of ls to grep to search for specific files in a directory.

With a little practice, you’ll be navigating the terminal like a seasoned explorer, commanding your files and directories with ease!

So first I went to check what
current directory I’m in by using the PWD command and
pressing the enter key. You can see that I’m in the root
directory which displays as forward/root. If I type in the LS-L command
notice that I have one directory in here called projects. Now I will create a new
directory called submissions. I do this by typing mkdir,
which stands for make directory. And then the word submissions. This is the name of the directory I want
to create and then I hit the enter key. I then type in ls-l for list so
that I can see the list structure. And now notice that a new directory
called submissions has been created. I can then go into this directory
using the change directory command. I do this by typing cd submissions and
then press the enter key. I type the ls command and
notice that there’s nothing in there. If I want to add some text files or
some content, I can use another command called touch. I type touch test1.txt,
followed by the enter key. To add another text file, I type in touch test2.txt followed by the entry key again. Now I run the ls-l command and noticed that the two text files
are listed inside the submissions folder. After this, I want to go back
to the root level directory and I do this by typing in
cd..followed by the enter key. And then I run the ls-l command again and notice two directories are now listed,
projects and submissions. Now I want to create another
directory called archive. To do this type mkdir followed by the word
archive and then hit the enter key. To see all of the directories, enter the ls-l command followed
by the entry key again. Once again, notice that three
directories are now listed, archive, projects and submissions. To get back to the top
view of my terminal, I can clear my screen by typing clear,
followed by the enter key. After this, I type in the ls-l command and
now I can see all three directories. Okay, so let’s say I want to move the submissions
folder into the archive folder. This requires a different command
called move written as mv. In this example, I need to specify
the directory I want to move and then where to move it to. So I type in mv submissions
followed by the word archive and then I hit the enter key. Then I can check to see if the move
happened by using the ls-l command and now notice that the submissions
directory is gone. So now I want to go to the archive
directory by typing cd archive. Again, I use the ls-l command and
now notice that the submissions directory is listed inside
of the archive directory. Recall that I created two text files
inside the submissions directory. Well, you’ll notice that they were
also moved to the archive directory. So I go to the submissions
directory by typing cd submissions, which changes the directory. I use the ls-l command,
followed by the enter key. And now you can see the two text files
are present and they were moved too. You have now learned how to
make directories and files and move directories and files.

Lab: Make and change directories and files

Video: Pipes

This script demonstrates basic terminal commands for file management and text analysis:

Navigation:

  • ls: lists files and directories
  • cd: changes directory

File content:

  • cat: displays file content

Text analysis:

  • wc: word count
  • -w: flag for word count

Pipes:

  • |: connects commands, passing output from one as input to another

Summary:

  • Navigated through directories and files using ls and cd.
  • Viewed file content using cat.
  • Analyzed word count for single and multiple files with wc and pipes.

This script provides a basic understanding of command-line usage for file exploration and text processing.

I have launched my terminal and I’m running the ls command. It informs me that I have two folders, archive
and projects. Next, I can change directory
into archive using the cd command and
search inside using ls. This reveals a
submissions folder. I can then type the cd
submissions command to enter into the
submissions folder and check what’s inside. The ls command reveals two files : File1.txt and file2.txt. Each of these files have
some content in them. I can check the
content of a file by running another
command called cat. I run the command cat file1.txt. This returns the
contents of the file, which is some simple text. Another command is the
word count command, which is abbreviated as WC. To use this command, I can just call it against the file by typing
wcfile1.txt-w. The w flag tells the WC command
to return the word count. The output informs me that there are a 181 words in the file. Let’s run another
example with pipes. Pipes allow you to
pass the output from one command as
the input to another. I can perform an ls command
on the current directory. Note that this outputs
to file names. Let’s type the ls command again. Then I pass in my pipe using
the vertical line character. Then I use the WC command
with the dash W flag. Notice that it
returns a result of two because there’s two
files in the system. What if I want to find the word count of a
file using pipes? I just changed the ls command
to cat file1.txt pipe wc-w. This returns a word count
of a 181 for file1.txt. Did you know that you can
also combine this command against the directory
or multiple files? For example, I can use
the command to get a combined word count
for file 1 and file 2. To get this data, I just input the command cat file1.txt, and then also pass in file2.txt. Then I use a pipe
followed by a wc-w. This returns a total word count
of 362 for the two files.

Video: Redirection

Summary of Redirection in Linux:

This video covers redirection, a technique to change the standard input and output of commands in Linux.

Standard Input/Output:

  • Input: Normally comes from the keyboard (<).
  • Output: Typically displayed on the screen (>).
  • Error: Shown on the console by default.

Redirection Types:

  1. Standard Input Redirection:
    • Use < symbol followed by filename.
    • Example: cat < input.txt reads from input.txt.
  2. Standard Output Redirection:
    • Use > symbol followed by filename.
    • Example: ls -l > output.txt saves ls output to output.txt.
  3. Standard Error Redirection:
    • Use 2> symbol followed by filename.
    • Example: ls -l /invalid/dir 2> error.txt saves errors to error.txt.

Combining Redirections:

  • &>: Send both standard output and error to the same file.
  • 2>&1: Send errors to the same destination as standard output.

Key Points:

  • Redirection allows more control over where commands send their data.
  • Standard input, output, and error each have a designated file descriptor (0, 1, 2).
  • Redirection symbols work with these descriptors for fine-grained control.

Remember:

  • Incorrect redirection can overwrite existing files.
  • Use redirection strategically to manage information flow in your commands.

Redirection in Linux

Redirection is a powerful feature in Linux that allows you to change the default input and output of commands. This can be useful for saving the output of a command to a file, or for using the output of one command as input for another.

There are three main types of redirection:

  • Standard input redirection: This is used to redirect the input of a command to a file instead of the keyboard.
  • Standard output redirection: This is used to redirect the output of a command to a file instead of the screen.
  • Standard error redirection: This is used to redirect the error messages of a command to a file instead of the screen.

Standard Input Redirection

The standard input redirection operator is <. For example, the following command will take the contents of the file input.txt and use them as input for the cat command:

cat < input.txt

The output of this command will be the same as the contents of the file input.txt.

Standard Output Redirection

The standard output redirection operator is >. For example, the following command will save the output of the ls command to a file called output.txt:

ls > output.txt

The original output of the ls command will not be displayed on the screen, but it will be saved to the file output.txt.

Standard Error Redirection

The standard error redirection operator is 2>. For example, the following command will save the error messages of the ls command to a file called error.txt:

ls /does/not/exist 2> error.txt

The original error message (“ls: cannot access /does/not/exist: No such file or directory”) will not be displayed on the screen, but it will be saved to the file error.txt.

Combining Redirection Operators

You can combine redirection operators to achieve more complex results. For example, the following command will save the output of the ls command to a file called output.txt and also save the error messages to a file called error.txt:

ls > output.txt 2> error.txt

The following command will take the contents of the file input.txt and use them as input for the cat command, and then save the output to a file called output.txt:

cat < input.txt > output.txt

Conclusion

Redirection is a powerful tool that can be used to automate tasks, save data, and make your work with Linux more efficient. I hope this tutorial has given you a basic understanding of how to use redirection in Linux.

In this video, you will
learn about redirection and the different types of
redirection you can use. The basic workflow of any Linux command
is that it takes an input and gives an output. The standard input
device is the keyboard. The standard output
device is the screen. With redirection, you can change the standard input
and/or output. There are three types of IO
or input/output redirections. Standard input, standard
output, and standard error. The shell keeps a reference
of standard input, output, and error by
a numbering system. The zero is for standard input, one is for standard output, and two is for standard error. Now you will learn about each
of these redirection types. Let’s start with standard input. Taking input
normally refers to a user typing information
from the keyboard. We use the less than
sign for user input. The cat command can
be used to record user input and
save it to a file. How do we take input
and store it in a file such as a.TXT file? Let me explain how you
can do this by using an example to store user
input to a text file. I have just launched
my terminal, but how do we take
input and store it in a file such as a.TXT file? One of the commands
you learn to use frequently in this course
is the cat command. This command is actually
set up to take in input. On my terminal, I type the cat command, followed by a greater than sign, and then follow it by the
name of the input file. In this scenario,
input.txt, press Enter. Now I can add text to
the text file created. At the end of the
text, press Enter. Next, press Control D to tell the cat command that’s
the end of the file. To output the contents
of the file, enter cat, followed by a less than sign, followed by input.txt,
press Enter. Notice that the
text that I added from the keyboard displays. Let’s discuss
standard output now. A lot of the commands
we have already used, for example, ls, send their output to a special file called
the standard output. Output direction is handled
with a greater than sign. IO allows us to use redirection to control
where the output goes. Now, I will demonstrate
how you can send output to a text file. Everything in Unix,
Linux is a file. This means every time you run a command like ls
and press Enter, it sends the output of the
file to an stdout file. In Linux, if you want to
control where the output goes, you can use a redirection. How do we do that? Enter the ls command, enter-L to print it as a list. Instead of pressing Enter, add a greater than
sign redirection. Now we have to tell it where
we want the data to go. In this scenario, I choose
an output.txt file. The output.txt file has
not been created yet, but it will be created
based on the command I’ve set here with a
redirection flag. Press Enter, type ls, then press Enter again to
display the directory. The output file displays. To view the content
of that file, use the last command followed by output.txt and press Enter. The content that displays using the ls minus L directory includes the different
files available. Errors occur when
things go wrong. When using redirection,
you also have to specify that the error
should be written to a file. You can do that by explicitly
setting the number 2 before the output
arrow and you can also combine it with the standard output
of two greater than Ampersand 1 to print both the standard output
and error if any occurs. You have already
learned that input is represented by zero
according to the shell, output is represented by one. When an error occurs, input has the less than sign and output has
the greater than sign. It may happen that
an error occurs when you are outputting
data to a text file. Remember that the error will not correspond with
the output stream. It will change to use
the error stream, which is represented by two. Let me now demonstrate
how this works. I have the terminal
open and I’m running a similar example to
the standard output. Type the ls command, follow this by -L to try
and print it as a list. Instead of using a directory
that we know exists, I’m going to use one
that doesn’t exist. Enter /bin/usr. Now type a greater than sign, followed by the name
of the output file. In this scenario type
error.txt. Press Enter. Notice that the
message cannot access. It states that there is no
such file or directory. Normally, you think
that it would still print the
contents of the file, but because an error occurred, it prints it to the console. There are two ways to send the contents to the
error.txt file. I type ls -l /bin/, then add the number 2, which represents
the error output, followed by the
greater than sign. Now enter the name
of the output file, error.txt, press Enter. To see what we have
inside the error file, type less followed
by the filename. In this case,
error.txt. Press Enter. The error message ls
cannot access /bin/. No such file or
directory displays. If you want to handle
both cases where it may find data or
may not find data, you can pass in a
different redirection. It handles each one, both for output and for error. To do this, again, enter ls -l /bin/. Next, add the greater
than sign for output, followed by the file
name error_output.txt. This time, we’re going to use another redirection to signify that we also want to get errors. To do this, I enter a two followed by a
greater than sign. This is followed by an
ampersand sign and the number 1 to get the output that
is available. Press Enter. To have a log inside
the error file, type less error_output.txt
and press Enter. Notice that the error
is contained inside the error.output
file this place. That brings us to the
end of this video. Now you know what redirection
is and how to use the three types of input/output
redirections. Well done.

Video: Grep

Grep: Powerful File and Folder Searching in Linux

This tutorial demonstrates the usefulness of Grep for searching files and folders in Linux. Here are the key takeaways:

  • Grep basics:
    • Stands for “global regular expression print.”
    • Used to search file contents and folders.
    • Case-sensitive by default.
  • Finding patterns:
    • Search for names starting with “Sam”: grep Sam names.txt
    • Ignore case sensitivity: grep -i Sam names.txt
    • Exact match: grep -w Sam names.txt
  • Using flags:
    • -i: Ignore case sensitivity.
    • -w: Match entire word only.
  • Combining searches:
    • Pipe Grep with other commands like ls for filtering results.
    • Example: ls /bin | grep zip to find zip files in the bin directory.

In short, Grep is a versatile tool for efficient file and folder exploration in Linux. By understanding its functionalities and flags, you can refine your searches and save time.

Grep: Powerful File and Folder Searching in Linux

Grep is a powerful command-line tool in Linux used for searching text files and folders for specific patterns. It stands for “global regular expression print,” meaning it can search for patterns that match a regular expression across multiple files. This makes it an invaluable tool for system administrators, programmers, and anyone who needs to work with text files on a regular basis.

Grep Basics

To use Grep, you simply specify the pattern you want to search for and the files or folders you want to search in. For example, to search for the word “hello” in the file “myfile.txt”, you would use the following command:

grep hello myfile.txt

This command will print any lines in the file “myfile.txt” that contain the word “hello”.

Grep is case-sensitive by default, so the pattern “hello” will not match the word “Hello”. To perform a case-insensitive search, you can use the -i flag:

grep -i hello myfile.txt

Regular Expressions

Grep supports regular expressions, which are powerful patterns that can match a wide variety of text strings. For example, the regular expression .*hello.* will match any line that contains the word “hello”, regardless of where it appears on the line.

Here are some other common regular expressions:

  • ^: Matches the beginning of a line.
  • $: Matches the end of a line.
  • .: Matches any single character.
  • *: Matches zero or more occurrences of the preceding character.
  • +: Matches one or more occurrences of the preceding character.
  • ?: Matches zero or one occurrence of the preceding character.
  • []: Matches any character within the brackets.
  • [^]: Matches any character not within the brackets.

For more information on regular expressions, you can refer to the man page for Grep:

man grep

Searching Multiple Files

You can use Grep to search multiple files at the same time. For example, to search for the word “hello” in all files in the current directory, you would use the following command:

grep hello *

The asterisk (*) is a wildcard character that matches any filename.

Searching Recursively

You can use the -r flag to search for patterns recursively, meaning that Grep will search all files in the current directory and its subdirectories. For example, to search for the word “hello” in all files in the current directory and its subdirectories, you would use the following command:

grep -r hello *

Other Grep Flags

Grep has many other flags that can be used to customize your searches. Here are a few of the most common:

  • -v: Invert the match, so that Grep only prints lines that do not match the pattern.
  • -n: Print the line number of each matching line.
  • -c: Only print the count of matching lines.
  • -o: Only print the matched part of each line.

For more information on Grep flags, you can refer to the man page:

man grep

Examples

Here are some examples of how you can use Grep to search for patterns in Linux:

  • Find all lines in the file “access.log” that contain the IP address “192.168.1.1”:
grep 192.168.1.1 access.log
  • Find all files in the current directory that contain the word “error”:
grep -r error *
  • Count the number of lines in all files in the current directory that contain the word “warning”:
grep -r -c warning *
  • Only print the matched part of each line in the file “passwd” that contains the word “root”:
grep -o root passwd

Grep is a powerful tool that can be used to search for a wide variety of patterns in text files. By understanding its basic syntax and the available flags, you can use Grep to quickly and efficiently find the information you need.

Grep stands for
global regular expression print. And it’s used for
searching across files and folders as well as the contents of files. On my local machine,
I enter the command ls -l, and see that there’s a file called names.txt. If I access that file using the lass
command, it displays a list of first names in non sequential order,
as in, not arranged alphabetically. So what I’ll do first is use Grep to
find some patterns of names that start with similar matches. Then I’ll also show how great can
be passed with different flags to get different results. First I’ll perform a standard
search using Grep. So what I’m going to do is look for
names that begin with Sam, by entering the command,
grep Sam names.txt. This then returns a list of
names that begin with Sam. Keep in mind that Grep is case sensitive,
which means if I run the same query with a lower case S, it returns
a completely different set of results. Because this query doesn’t match
the capital S, it returns partial matches in which Sam appears in the middle or
end of a name rather than the beginning. Fortunately I can pass in a flag
to ignore case sensitivity. I can do this with a command,
grep -i, followed by the keyword Sam. And then the file name names.txt again. This time I get back both
users that begin with Sam and also with Sam as a partial match in
the middle or the end of the name. So the results set changes based on the
type of query that I passed through with different flags. We could also do an exact match by
passing in a different flag, and that’s the dash W. Which means it’ll match
exactly what I’m looking for. So I’ll input, grep -w, and
then pass in the key word Sam, with a capital S, and
finally our file name names.txt. In this case we only get back
a single result with the name Sam, as any partial matches are ignored. Lastly, I can use a pipe command
to combine different searches for Grep itself. For example, let’s say I went to search
across a list of directories for certain, executable files. I can combine that with
different commands and search across the file structure to
find exactly what I’m looking for. If I check all the executable files
inside the bin directory by running, ls /bin,
it returns a long list of results. In order to filter that down,
I can run the same query of ls /bin, but this time I’m going to pipe it. Pass in a Grep and
then enter the keyword zip. You’ll find that in this case I get a much
smaller subset back in the results. And if I need to refine it further, then
I can also apply the different flags to look for an exact match, a partial
match or ignore case sensitivity.

Practice Quiz: Knowledge Check: Unix Commands

The options you pass to a command are known as _____________.

What command do you use to change directory?

What command do you use to move files and directories?

To pass the output from one command as the input to another command, what do you use?

What command do you use to create a directory?

Quiz: Module Quiz: Command Line

What command do you use to print the current working directory?

What command do you use to search text content?

Which of the following are standard input/output (I/O) streams? Select all that apply.

Pipes are used to pass the output of one command as input to another command.

What command do you use to list files and directories?

The mv command is used to create directories.

Flags can be used to change the behaviour of a command.

Which of the following are benefits of using the command line? Select all that apply.

Video: Module Summary: Command Line

This module covered the basics of using the command line in Linux. You learned how to:

  • Navigate your hard drive using commands like PWD for current directory, cd to change directories, and mkdir to create new ones.
  • Create, rename, and delete files using commands like touch, mv, and rm.
  • Use powerful tools like grep to search for specific content within files and folders.
  • Automate tasks by chaining commands together using pipes and redirection.

Overall, you gained the ability to explore your computer’s file system, manage files, and perform basic operations efficiently through the command line.

Great work. You’ve reached the end of this module
on the command line. It’s now time to review what you’ve learned during
these lessons. In this module, you’ve
learned how to use the command line to
execute commands in Linux. You were introduced to some of the most commonly used
commands that traverse, create, rename, and delete
files on your hard drive. You’ve learned how to use
piping and redirection to create powerful workflows
that automate your work. Having completed this module, you should be able to describe what the command line
is and how it is used. Explore your hard drive
using the command line. Create, rename, and delete files and folders
on your hard drive using Unix commands and use
pipes and redirection. This module began with a video exploring the answer
to the question, what are Unix commands? You learned how to determine the current working directory
using the PWD command. You also explored how
to create and change directories and files
using the command line. You can now create a
working directory, create two different
directories: dir 1 and dir 2, create files and directories
inside dir 1 and dir 2. You can now also use Grep
to search for files, folders, and contents of files. You’re now familiar
with the command line. Well done. You’re making good progress on your
learning journey.

Reading: Additional Resources

Reading