# Redirectors and wildcards Here we are going to start covering some of the components of the command line that make it so versatile and powerful! If you'd like to follow along, but need to pull up the proper working environment again, revisit [here](shell-getting-started-01.html#how-to-access-the-shell-for-now) and then come back 🙂 ---
To be sure we are starting in the same place, let's run: ```bash cd ~/shell_intro ```--- ## Redirectors When we are talking about "redirectors" here, we are referring to things that change where the output of something is going. The first we're going to look at is called a "pipe" (**`|`**). > A pipe (**`|`**) is used to connect multiple commands. It takes the output from the previous command and "pipes" it into the input of the following command. Let's look at an example. Remember we used **`wc -l`** to count how many lines were in a file: ```bash wc -l example.txt ``` And that **`ls`** lists the files and directories in our current working directory: ```bash ls ``` If we "pipe" (**`|`**) the **`ls`** command into the **`wc -l`** command, instead of printing the output from **`ls`** to the screen as usual, it will go into **`wc -l`** which will print out how many items there are: ```bash ls | wc -l ``` For another example, let's look at what's in the subdirectory, "data/all_samples/": ```bash ls data/all_samples/ ``` That prints out a lot of stuff, let's see how many things are in that directory: ```bash ls data/all_samples/ | wc -l ``` We'll get back to making sense of that mess when we get to *wildcards* in the next section. > Another important character is the greater than sign, **`>`**. This tells the command line to *redirect* the output to a file, rather than just printing it to the screen as we've seen so far. For an example of this we will write the output of **`ls`** to a new file called "directory_contents.txt": ```bash ls ls > directory_contents.txt ``` Notice that when we redirect the output with the **`>`**, nothing printed to the screen. And we've just created a file called "directory_contents.txt": ```bash ls head directory_contents.txt ``` **It's important to remember that the `>` redirector will overwrite the file you are pointing to if it already exists.** ```bash ls experiment/ > directory_contents.txt head directory_contents.txt ``` If we want to append an output to a file, rather than overwrite it like that, we can use two of them instead, `>>`: ```bash ls >> directory_contents.txt head directory_contents.txt ``` ## Wildcards > Wildcards as used at the command line are special characters that enable us to specify multiple items very easily. The **`*`** and **`?`** are probably the most commonly used, so let's try them out!
So far we've just been using the **`*`** wildcard with the **`ls`** command. But wildcards can be used with many of the common shell commands we've seen so far. For example, we can use it with the **`mv`** command to move all 300 of the ".fq" files into their own directory at once: ```bash ls | wc -l mkdir fastq_files ls fastq_files/ ls *.fq mv *.fq fastq_files/ ls fastq_files/ ls | wc -l ```PRACTICE! With 900 files and 3 file types (".txt", ".tsv", and ".fq"), we might expect there to be 300 of each type, but let's make sure. Using what we've seen above, how can we count how many files of each type there are in this directory?SolutionAh good, it's nice when things make sense 🙂ls *.txt | wc -l ls *.tsv | wc -l ls *.fq | wc -l
> **Note:** When using wildcards, running **`ls`** first like done in the above example (**`ls *.fq`**) is good practice before actually running a command. It is a way of checking that we are specifying exactly what we think we are specifying. ### BONUS ROUND: History! The shell also keeps track of our previous commands for us. There are a few different ways we can take advantage of this, one is by using the **`history`** command. But that alone will print all of it to the screen. It can be more practical to "pipe" (**`|`**) that into something else like **`tail`** to see the last few commands: ```bash history | tail ``` Or **`less`** so we can scroll through our previous commands: ```bash history | less ``` To get out of **`less`**, press the q key. We can also use the up and down arrows at the command line to scroll through previous commands. This is useful for finding commands, but it's also useful for making sure we are acting on the files we want to act on when using wildcards. As mentioned above, we can check first with **`ls *.fq`**, press return to see we are acting on the files we want, and then press the up arrow to bring up the previous command, and change it to what we want without altering the "*.fq" part of the command – as we already know it's correct. Any time we can remove the chance of human error, we should 🙂QUICK QUESTION! Why does this say 601 instead of 600?SolutionIt's also counting the new directory we created 🙂
PRACTICE! We've already moved all the ".fq" files into their own directory. Create separate directories for the ".txt" files and the ".tsv" files too, and then try to move those files into their appropriate directories.SolutionIt doesn't matter what the directories are named, but at the end they should be the only 3 things in the working directory 🙂mkdir text_files ls *.txt mv *.txt text_files
mkdir tsv_files ls *.tsv mv *.tsv tsv_files
ls