Appendix E — Bash files

This is an extended lesson on bash that is not always assigned, but is still useful.

E.1 Goal

Let’s learn some more command-line juju for working with files. These commands will help look at code files on your computer.

E.2 Set up

Let’s make sure we are in our class folder. (This is review from Moving Around).

  1. Make sure you are in your icj project folder:
cd ~/Documents/icj
  1. Create a new folder called myproject:
mkdir myproject
  1. Use cd to move inside the myproject folder then use pwd to make sure you are in the right place. The result should be something like this:
$ cd
Users/ccm346/Documents/icj/myproject

E.3 curl

We need some text to work with, so we’re going to pull down some text from Github. I might as well explain what we are doing.

curl is a command to transfer files. I think of it as “Capture URL”. We need to give curl a couple of flags (or options) for this job:

  • -L stands for “Location”. It allows curl to follow a URL if it is redirected.
  • -o for “output”. So we can write this to a file (which we are calling data.csv) instead of our terminal window. (We could use -O instead to just use the current file name.)
Warning

This URL below needs to be updated

curl -L -o data.csv https://raw.githubusercontent.com/utdata/icj-class/main/resources/data_example.csv

OK, now we should be able to ls and see our file is there. My output looks like this:

$ ls
data.csv

E.5 tail

tail shows you the bottom of the file. It takes the same -n flags.

tail data.csv

That result won’t show you the header, because you are looking at the last 10 lines of the file.

E.6 wc

wc I think of as “word count”, but it can also count lines and bytes.

wc data.csv

gives you this output:

$ wc data.csv
     100    1136   15642 data.csv

The first column is number of lines, then the number of words, then bytes.

If you want just the number of lines, us -l.

wc -l data.csv

E.7 cat

cat means to concatenate and print a file to your window. If you feed it two file names, it will give you the first, then the other. Do this:

cat data.csv

This will print the contents of data.csv to your screen. It’s showing all 100 lines.

But what you can do is redirect that output into a file by using >. Do this.

cat data.csv > file01.csv

What you’ve done is take the contents of data.csv, printed the contents and then redirected that content into another file called file01.csv. Since that didn’t exist already, it was created on the fly.

If I wanted to take two files, file01.csv and file02.csv, and then combine them into a single file on your computer, it would look like this. (You don’t have to do these commands, just understand them.):

cat file01.csv file02.csv > combined.csv

Now, combined.csv would be the combination of both files.

E.8 grep

grep is for using regular expressions to find patterns within a file. It takes a regular expression input and the file name and gives in return the lines of the file that match that regular expression.

grep 'ATX INVESTMENTS' data.csv

… will print out just the lines in data.csv that have ‘ATX INVESTMENTS’ somewhere in them. Note that the name in the field is ‘ATX INVESTMENTS LLC’ but it found it with just part of that.

If you want to just know how many lines there are with ‘ATX INVESTMENTS’, use the -c flag for count:

$ grep -c 'ATX INVESTMENTS' data.csv

The answer should be 4.

E.9 Piping commands

You can “pipe” the results of one command into another command with the | character, which you’ll find as the shift of your backslash key. You can string commands together with that, so if I just wanted to see the first lines that has ‘ATX INVESTMENTS’, I can do this:

grep 'ATX INVESTMENTS' data.csv | head -n 1