3 [Back To The Menu](http://github.com/JHU-SWC-2012/SWC-bootcamp/)
4 - [Forward to Python Variables](http://github.com/JHU-SWC-2012/SWC-bootcamp/tree/master/2a-PythonVariables/)
8 **Updated and presented by : Radhika Khetani**
10 **Modified by : Sasha Wood**
12 **This presentation originally developed by: Milad Fatenejad**
14 # What is the shell how do I access the shell?
16 The *shell* is a program that presents a command line interface
17 which allows you to control your computer using commands entered
18 with a keyboard instead of controlling graphical user interfaces
19 (GUIs) with a mouse/keyboard combination.
21 Use the GUI to open the tutorial on github. Single click on the "Firefox
22 Web Browser". Type in the URL:
23 github.com/thehackerwithin/boot-camps/tree/2013-01-chicago
25 Click on the directory named `Shell`.
27 A *terminal* is a program you run that gives you access to the
28 shell. There are many different terminal programs that vary across
31 There are many reasons to learn about the shell. In my opinion, the
32 most important reasons are that:
34 1. It is very common to encounter the shell and
35 command-line-interfaces in scientific computing, so you will
36 probably have to learn it eventually
38 2. The shell is a really powerful way of interacting with your
39 computer. GUIs and the shell are complementary - by knowing both
40 you will greatly expand the range of tasks you can accomplish with
41 your computer. You will also be able to perform many tasks more
44 The shell is just a program and there are many different shell
45 programs that have been developed. The most common shell (and the one
46 we will use) is called the Bourne-Again SHell (bash). Even if bash is
47 not the default shell, it is usually installed on most systems and can be
48 started by typing `bash` in the terminal. Many commands, especially a
49 lot of the basic ones, work across the various shells but many things
50 are different. I recommend sticking with bash and learning it well.
51 ([Here is a link for more information](http://en.wikipedia.org/wiki/Bash_(Unix_shell))
53 To open a terminal, just single click on the "Terminal" icon on the
56 # The Example: Manipulating Experimental Data Files
58 We will spend most of our time learning about the basics of the shell
59 by manipulating some experimental data from a hearing test. To get
60 the data for this test, you will need internet access. Just enter the
63 git clone https://github.com/thehackerwithin/boot-camps.git
67 git checkout 2013-01-chicago
69 These 2 commands will grab all of the data needed for this workshop from the
74 One very basic command is `echo`. This command just prints text to
75 the terminal. Try the command:
79 Then press enter. You should see the text "Hello, World" printed back
80 to you. The echo command is useful for printing from a shell script,
81 for displaying variables, and for generating known values to pass
84 ## Moving around the file system
86 Let's learn how to move around the file system using command line
87 programs. This is really easy to do using a GUI (just click on
88 things). Once you learn the basic commands, you'll see that it is
89 really easy to do in the shell too.
91 First we have to know where we are. The program `pwd` (print working
92 directory) tells you where you are sitting in the directory tree. The
93 command `ls` will list the files in files in the current
94 directory. Directories are often called "folders" because of how they
95 are represented in GUIs. Directories are just listings of files. They
96 can contain other files or directories.
98 Whenever you start up a terminal, you will start in a special
99 directory called the *home* directory. Every user has their own home
100 directory where they have full access to do whatever they want. In
101 this case, the `pwd` command tells us that we are in the `/home/swc`
102 directory. This is the home directory for the `swc` user. That is our
103 user name. You can always find out your user name by entering the
108 When you enter the `ls` command lists the contents of the current
109 directory. There are several items in the home directory, notice that
110 they are all colored blue. This tells us that all of these items are
111 directories as opposed to files.
113 Lets create an empty file using the `touch` command. Enter the
118 Then list the contents of the directory again. You should see that a
119 new entry, called `testfile`, exists. It is colored white meaning that
120 it is a file, as opposed to a directory. The `touch` command just
121 creates an empty file.
123 Some terminals will not color the directory entries in this very
124 convenient way. In those terminals, use `ls -F` instead of `ls`. The
125 `-F` argument modifies the results so that a slash is placed at the
126 end of directories. If the file is *executable* meaning that it can be
127 run like a program, then a star fill be placed of the file name.
129 You can also use the command `ls -l` to see whether items in a
130 directory are files or directories. `ls -l` gives a lot more
131 information too, such as the size of the file and information about
132 the owner. If the entry is a directory, then the first letter will be
133 a "d". The fifth column shows you the size of the entries in
134 bytes. Notice that `testfile` has a size of zero.
136 Now, let's get rid of `testfile`. To remove a file, just enter the
141 The `rm` command can be used to remove files. If you enter `ls` again,
142 you will see that `testfile` is gone.
145 **Changing Directories**
147 Now, let's move to a different directory. The command `cd` (change
148 directory) is used to move around. Let's move into the `SWC-bootcamp`
149 directory. Enter the following command:
153 Now use the `ls` command to see what is inside this directory. You
154 will see that there is an entry which is green. This means that this
155 is an executable. If you use `ls -F` you will see that this file ends
158 This directory contains all of the material for this boot camp. Now
159 move to the directory containing the data for the shell tutorial:
163 If you enter the `cd` command by itself, you will return to the home
164 directory. Try this, and then navigate back to the `1-Shell`
169 Most programs take additional arguments that control their exact
170 behavior. For example, `-F` and `-l` are arguments to `ls`. The `ls`
171 program, like many programs, take a lot of arguments. But how do we
172 know what the options are to particular commands?
174 Most commonly used shell programs have a manual. You can access the
175 manual using the `man` program. Try entering:
179 This will open the manual page for `ls`. Use the space key to go
180 forward and b to go backwards. When you are done reading, just hit `q`
183 Programs that are run from the shell can get extremely complicated. To
184 see an example, open up the manual page for the `find` program,
185 which we will use later this session. No one can possibly learn all of
186 these arguments, of course. So you will probably find yourself
187 referring back to the manual page frequently.
189 **Examining the contents of other directories**
191 By default, the `ls` commands lists the contents of the working
192 directory (i.e. the directory you are in). You can always find the
193 directory you are in using the `pwd` command. However, you can also
194 give `ls` the names of other directories to view. Navigate to the
195 home directory if you are not already there. Then enter the
200 This will list the contents of the `SWC-bootcamp` directory without
201 you having to navigate there. Now enter:
203 ls SWC-bootcamp/1-Shell
205 This prints the contents of `1-Shell`. The `cd` command works in a
206 similar way. Try entering:
208 cd SWC-bootcamp/1-Shell
210 and you will jump directly to `1-Shell` without having to go through
211 the intermediate directory.
213 ## Full vs. Relative Paths
215 The `cd` command takes an argument which is the directory
216 name. Directories can be specified using either a *relative* path a
217 full *path*. The directories on the computer are arranged into a
218 hierarchy. The full path tells you where a directory is in that
219 hierarchy. Navigate to the home directory. Now, enter the `pwd`
220 command and you should see:
224 which is the full name of your home directory. This tells you that you
225 are in a directory called `swc`, which sits inside a directory called
226 `home` which sits inside the very top directory in the hierarchy. The
227 very top of the hierarchy is a directory called `/` which is usually
228 referred to as the *root directory*. So, to summarize: `swc` is a
229 directory in `home` which is a directory in `/`.
231 Now enter the following command:
233 cd /home/swc/SWC-bootcamp/1-Shell
235 This jumps to `1-Shell`. Now go back to the home directory. We saw
236 earlier that the command:
238 cd SWC-bootcamp/1-Shell
240 had the same effect - it took us to the `1-Shell` directory. But,
241 instead of specifying the full path
242 (`/home/swc/SWC-bootcamp/1-Shell`), we specified a *relative path*. In
243 other words, we specified the path relative to our current
244 directory. A full path always starts with a `/`. A relative path does
245 not. You can usually use either a full path or a relative path
246 depending on what is most convenient. If we are in the home directory,
247 it is more convenient to just enter the relative path since it
248 involves less typing.
250 Now, list the contents of the /bin directory. Do you see anything
254 ## Saving time with shortcuts, wild cards, and tab completion
258 There are some shortcuts which you should know about. Dealing with the
259 home directory is very common. So, in the shell the tilde character,
260 `~`, is a shortcut for your home directory. Navigate to the `1-Shell`
261 directory, then enter the command:
265 This prints the contents of your home directory, without you having to
266 type the full path. The shortcut `..` always refers to the directory
267 above your current directory. Thus:
271 prints the contents of the `/home/swc/SWC-bootcamp`. You can chain
276 prints the contents of `/home/swsc` which is your home
277 directory. Finally, the special directory `.` always refers to your
278 current directory. So, `ls`, `ls .`, and `ls ././././.` all do the
279 same thing, they print the contents of the current directory. This may
280 seem like a useless shortcut right now, but we'll see when it is
281 needed in a little while.
283 To summarize, the commands `ls ~`, `ls ~/.`, `ls ../../`, and `ls
284 /home/swc` all do exactly the same thing. These shortcuts are not
285 necessary, they are provided for your convenience.
287 **Our data set: Cochlear Implants**
289 A cochlear implant is a small electronic device that is surgically
290 implanted in the inner ear to give deaf people a sense of
291 hearing. More than a quarter of a million people have them, but there
292 is still no widely-accepted benchmark to measure their effectiveness.
293 In order to establish a baseline for such a benchmark, our supervisor
294 got teenagers with CIs to listen to audio files on their computer and
297 1. the quietest sound they could hear
298 2. the lowest and highest tones they could hear
299 3. the narrowest range of frequencies they could discriminate
301 To participate, subjects attended our laboratory and one of our lab
302 techs played an audio sample, and recorded their data - when they
303 first heard the sound, or first heard a difference in the sound. Each
304 set of test results were written out to a text file, one set per file.
305 Each participant has a unique subject ID, and a made-up subject name.
306 Each experiment has a unique experiment ID. The experiment has
307 collected 351 files so far.
309 The data is a bit of a mess! There are inconsistent file names, there
310 are extraneous "NOTES" files that we'd like to get rid of, and the
311 data is spread across many directories. We are going to use shell
312 commands to get this data into shape. By the end we would like to:
314 1. Put all of the data into one directory called "alldata"
316 2. Have all of the data files in there, and ensure that every file
317 has a ".txt" extension
319 3. Get rid of the extraneous "NOTES" files
321 If we can get through this example in the available time, we will move
322 onto more advanced shell topics...
326 Navigate to the `~/SWC-bootcamp/Shell-1/data/THOMAS` directory. This
327 directory contains our hearing test data for THOMAS. If we type `ls`,
328 we will see that there are a bunch of files which are just four digit
329 numbers. By default, `ls` lists all of the files in a given
330 directory. The `*` character is a shortcut for "everything". Thus, if
331 you enter `ls *`, you will see all of the contents of a given
332 directory. Now try this command:
336 This lists every file that ends with a `1`. This command:
340 Lists every file in `/usr/bin` that ends in the characters `.sh`. And
345 lists every file in the current directory which contains the number
346 `4`, and ends with the number `1`. There are four such files: `0241`,
347 `0341`, `0431`, and `0481`.
349 So how does this actually work? Well...when the shell (bash) sees a
350 word that contains the `*` character, it automatically looks for files
351 that match the given pattern. In this case, it identified four such
352 files. Then, it replaced the `*4*1` with the list of files, separated
353 by spaces. In other the two commands:
356 ls 0241 0341 0431 0481
358 are exactly identical. The `ls` command cannot tell the difference
359 between these two things.
364 Do each of the following using a single `ls` command without
365 navigating to a different directory.
367 1. List all of the files in `/bin` that contain the letter `a`
368 2. List all of the files in `/bin` that contain the letter `a` or the letter `b`
369 3. List all of the files in `/bin` that contain the letter `a` AND the letter `b`
375 Navigate to the home directory. Typing out directory names can waste a
376 lot of time. When you start typing out the name of a directory, then
377 hit the tab key, the shell will try to fill in the rest of the
378 directory name. For example, enter:
382 The shell will fill in the rest of the directory name for
383 `SWC-bootcamp`. Now enter:
387 When you hit the first tab, nothing happens. The reason is that there
388 are multiple directories in the home directory which start with
389 3. Thus, the shell does not know which one to fill in. When you hit
390 tab again, the shell will list the possible choices.
392 Tab completion can also fill in the names of programs. For example,
393 enter `e<tab><tab>`. You will see the name of every program that
394 starts with an `e`. One of those is `echo`. If you enter `ec<tab>` you
395 will see that tab completion works.
399 You can easily access previous commands. Hit the up arrow.
400 Hit it again. You can step backwards through your command history.
401 The down arrow takes your forwards in the command history.
403 ^-C will cancel the command you are writing, and give you a fresh prompt.
405 ^-R will do a reverse-search through your command history. This
410 Commands like `ls`, `rm`, `echo`, and `cd` are just ordinary programs
411 on the computer. A program is just a file that you can *execute*. The
412 program `which` tells you the location of a particular program. For
417 Will return "/bin/ls". Thus, we can see that `ls` is a program that
418 sits inside of the `/bin` directory. Now enter:
422 You will see that `find` is a program that sits inside of the
423 `/usr/bin` directory.
425 So ... when we enter a program name, like `ls`, and hit enter, how
426 does the shell know where to look for that program? How does it know
427 to run `/bin/ls` when we enter `ls`. The answer is that when we enter
428 a program name and hit enter, there are a few standard places that the
429 shell automatically looks. If it can't find the program in any of
430 those places, it will print an error saying "command not found". Enter
435 This will print out the value of the `PATH` environment variable. More
436 on environment variables later. Notice that a list of directories,
437 separated by colon characters, is listed. These are the places the
438 shell looks for programs to run. If your program is not in this list,
439 then an error is printed. The shell ONLY checks in the places listed
440 in the `PATH` environment variable.
442 Navigate to the `1-Shell` directory and list the contents. You will
443 notice that there is a program (executable file) called `hello` in
444 this directory. Now, try to run the program by entering:
448 You should get an error saying that hello cannot be found. That is
449 because the directory `/home/swc/SWC-bootcamp/1-Shell` is not in the
450 `PATH`. You can run the `hello` program by entering:
454 Remember that `.` is a shortcut for the current working
455 directory. This tells the shell to run the `hello` program which is
456 located right here. So, you can run any program by entering the path
457 to that program. You can run `hello` equally well by specifying:
459 /home/swc/SWC-bootcamp/1-Shell/hello
465 When there are no `/` characters, the shell assumes you want to look
466 in one of the default places for the program.
471 We now know how to switch directories, run programs, and look at the
472 contents of directories, but how do we look at the contents of files?
474 The easiest way to examine a file is to just print out all of the
475 contents using the program `cat`. Enter the following command:
479 This prints out the contents of the `ex_data.txt` file. If you enter:
481 cat ex_data.txt ex_data.txt
483 It will print out the contents of `ex_data.txt` twice. `cat` just
484 takes a list of file names and writes them out one after another (this
485 is where the name comes from, `cat` is short for concatenate).
490 1. Print out the contents of the `~/SWC-bootcamp/1-Shell/dictionary.txt`
491 file. What does this file contain?
493 2. Without changing directories, (you should still be in `1-Shell`),
494 use one short command to print the contents of all of the files in
495 the /home/swc/SWC-bootcamp/1-Shell/data/THOMAS directory.
499 `cat` is a terrific program, but when the file is really big, it can
500 be annoying to use. The program, `less`, is useful for this
501 case. Enter the following command:
503 less ~/SWC-bootcamp/1-Shell/dictionary.txt
505 `less` opens the file, and lets you navigate through it. The commands
506 are identical to the `man` program. Use "space" to go forward and hit
507 the "b" key to go backwards. The "g" key goes to the beginning of the
508 file and "G" goes to the end. Finally, hit "q" to quit.
510 `less` also gives you a way of searching through files. Just hit the
511 "/" key to begin a search. Enter the name of the word you would like
512 to search for and hit enter. It will jump to the next location where
513 that word is found. Try searching the `dictionary.txt` file for the
514 word "cat". If you hit "/" then "enter", `less` will just repeat
515 the previous search. `less` searches from the current location and
516 works its way forward. If you are at the end of the file and search
517 for the word "cat", `less` will not find it. You need to go to the
518 beginning of the file and search.
520 Remember, the `man` program uses the same commands, so you can search
521 documentation using "/" as well!
526 Use the commands we've learned so far to figure out how to search
527 in reverse while using `less`.
534 Let's turn to the experimental data from the hearing tests that we
535 began with. This data is located in the `~/SWC-bootcamp/1-Shell/data`
536 directory. Each subdirectory corresponds to a particular participant
537 in the study. Navigate to the `Bert` subdirectory in `data`. There
538 are a bunch of text files which contain experimental data
539 results. Lets print them all:
543 Now enter the following command:
545 cat au* > ../all_data
547 This tells the shell to take the output from the `cat au*` command and
548 dump it into a new file called `../all_data`. To verify that this
549 worked, examine the `all_data` file. If `all_data` had already
550 existed, we would overwritten it. So the `>` character tells the shell
551 to take the output from what ever is on the left and dump it into the
552 file on the right. The `>>` characters do almost the same thing,
553 except that they will append the output to the file if it already
559 Use `>>`, to append the contents of all of the files which contain the
560 number 4 in the directory:
562 /home/swc/SWC-bootcamp/1-Shell/data/gerdal
564 to the existing `all_data` file. Thus, when you are done `all_data`
565 should contain all of the experiment data from Bert and any
566 experimental data file from gerdal that contains the number 4.
571 ## Creating, moving, copying, and removing
573 We've created a file called `all_data` using the redirection operator
574 `>`. This is critical file so we have to make copies so that the data
575 is backed up. Lets copy the file using the `cp` command. The `cp`
576 command backs up the file. Navigate to the `data` directory and enter:
578 cp all_data all_data_backup
580 Now `all_data_backup` has been created as a copy of `all_data`. We can
581 move files around using the command `mv`. Enter this command:
583 mv all_data_backup /tmp/
585 This moves `all_data_backup` into the directory `/tmp`. The directory
586 `/tmp` is a special directory that all users can write to. It is a
587 temporary place for storing files. Data stored in `/tmp` is
588 automatically deleted when the computer shuts down.
590 The `mv` command is also how you rename files. Since this file is so
591 important, let's rename it:
593 mv all_data all_data_IMPORTANT
595 Now the file name has been changed to all_data_IMPORTANT. Let's delete
598 rm /tmp/all_data_backup
600 The `mkdir` command is used to create a directory. Just enter `mkdir`
601 followed by a space, then the directory name.
608 1. Rename the `all_data_IMPORTANT` file to `all_data`.
609 2. Create a directory in the `data` directory called `foo`
610 3. Then, copy the `all_data` file into `foo`
614 By default, `rm`, will NOT delete directories. You can tell `rm` to
615 delete a directory using the `-r` option. Enter the following command:
622 The `wc` program (word count) counts the number of lines, words, and
623 characters in one or more files. Make sure you are in the `data`
624 directory, then enter the following command:
628 For each of the files indicated, `wc` has printed a line with three
629 numbers. The first is the number of lines in that file. The second is
630 the number of words. Finally, the total number of characters is
631 indicated. The final line contains this information summed over all of
632 the files. Thus, there were 10445 characters in total.
634 Remember that the `Bert/*` and `gerdal/*4*` files were merged
635 into the `all_data` file. So, we should see that `all_data` contains
636 the same number of characters:
640 Every character in the file takes up one byte of disk space. Thus, the
641 size of the file in bytes should also be 10445. Let's confirm this:
645 Remember that `ls -l` prints out detailed information about a file and
646 that the fifth column is the size of the file in bytes.
651 Figure out how to get `wc` to print the length of the longest line in
656 ## The awesome power of the Pipe
658 Suppose I wanted to only see the total number of character, words, and
659 lines across the files `Bert/*` and `gerdal/*4*`. I don't want to
660 see the individual counts, just the total. Of course, I could just do:
664 Since this file is a concatenation of the smaller files. Sure, this
665 works, but I had to create the `all_data` file to do this. Thus, I
666 have wasted a precious 7062 bytes of hard disk space. We can do this
667 *without* creating a temporary file, but first I have to show you two
668 more commands: `head` and `tail`. These commands print the first few,
669 or last few, lines of a file, respectively. Try them out on
675 The `-n` option to either of these commands can be used to print the
676 first or last `n` lines of a file. To print the first/last line of the
682 Let's turn back to the problem of printing only the total number of
683 lines in a set of files without creating any temporary files. To do
684 this, we want to tell the shell to take the output of the `wc Bert/*
685 gerdal/*4*` and send it into the `tail -n 1` command. The `|`
686 character (called pipe) is used for this purpose. Enter the following
689 wc Bert/* gerdal/Data0559 | tail -n 1
691 This will print only the total number of lines, characters, and words
692 across all of these files. What is happening here? Well, `tail`, like
693 many command line programs will read from the *standard input* when it
694 is not given any files to operate on. In this case, it will just sit
695 there waiting for input. That input can come from the user's keyboard
696 *or from another program*. Try this:
700 Notice that your cursor just sits there blinking. Tail is waiting for
701 data to come in. Now type:
708 then CONTROL+d. You should is the lines:
713 printed back at you. The CONTROL+d keyboard shortcut inserts an
714 *end-of-file* character. It is sort of the standard way of telling the
715 program "I'm done entering data". The `|` character is replaces the
716 data from the keyboard with data from another command. You can string
717 all sorts of commands together using the pipe.
719 The philosophy behind these command line programs is that none of them
720 really do anything all that impressive. BUT when you start chaining
721 them together, you can do some really powerful things really
722 efficiently. If you want to be proficient at using the shell, you must
723 learn to become proficient with the pipe and redirection operators:
727 **A sorting example**
729 Let's create a file with some words to sort for the next example. We
730 want to create a file which contains the following names:
737 To do this, we need a program which allows us to create text
738 files. There are many such programs, the easiest one which is
739 installed on almost all systems is called `nano`. Navigate to `/tmp`
740 and enter the following command:
744 Now enter the four names as shown above. When you are done, press
745 CONTROL+O to write out the file. Press enter to use the file name
746 `toBeSorted`. Then press CONTROL+x to exit `nano`.
748 When you are back to the command line, enter the command:
752 Notice that the names are now printed in alphabetical order.
757 Use the `echo` command and the append operator, `>>`, to append your
758 name to the file, then sort it and make a new file called Sorted.
762 Let's navigate back to `~/SWC-bootcamp/1-Shell/data`. Enter the following command:
764 wc Bert/* | sort -k 3 -n
766 We are already familiar with what the first of these two commands
767 does: it creates a list containing the number of characters, words,
768 and lines in each file in the `Bert` directory. This list is then
769 piped into the `sort` command, so that it can be sorted. Notice there
770 are two options given to sort:
772 1. `-k 3`: Sort based on the third column
773 2. `-n`: Sort in numerical order as opposed to alphabetical order
775 Notice that the files are sorted by the number of characters.
780 Use the `man` command to find out how to sort the output from `wc` in
788 Combine the `wc`, `sort`, `head` and `tail` commands so that only the
789 `wc` information for the largest file is listed
791 Hint: To print the smallest file, use:
793 wc Bert/* | sort -k 3 -n | head -n 1
797 Printing the smallest file seems pretty useful. We don't want to type
798 out that long command often. Let's create a simple script, a simple
799 program, to run this command. The program will look at all of the
800 files in the current directory and print the information about the
801 smallest one. Let's call the script `smallest`. We'll use `nano` to
802 create this file. Navigate to the `data` directory, then:
806 Then enter the following text:
809 wc * | sort -k 3 -n | head -n 1
811 Now, `cd` into the `Bert` directory and enter the command
812 `../smallest`. Notice that it says permission denied. This happens
813 because we haven't told the shell that this is an executable
814 file. If you do `ls -l ../smallest`, it will show you the permissions on
815 the left of the listing.
817 Enter the following commands:
819 chmod a+x ../smallest
822 The `chmod` command is used to modify the permissions of a file. This
823 particular command modifies the file `../smallest` by giving all users
824 (notice the `a`) permission to execute (notice the `x`) the file. If
829 You will see that the file name is green and the permissions have changed.
830 Congratulations, you just created your first shell script!
834 You can search the contents of a file using the command `grep`. The
835 `grep` program is very powerful and useful especially when combined
836 with other commands by using the pipe. Navigate to the `Bert`
837 directory. Every data file in this directory has a line which says
838 "Range". The range represents the smallest frequency range that can be
839 discriminated. Lets list all of the ranges from the tests that Bert
847 Create an executable script called `smallestrange` in the `data`
848 directory, that is similar to the `smallest` script, but prints the
849 file containing the file with the smallest Range. Use the commands
850 `grep`, `sort`, and `tail` to do this.
857 The `find` program can be used to find files based on arbitrary
858 criteria. Navigate to the `data` directory and enter the following
863 This prints the name of every file or directory, recursively, starting
864 from the current directory. Let's exclude all of the directories:
866 find . -type f -print
868 This tells `find` to locate only files. Now try these commands:
870 find . -type f -name "*1*"
871 find . -type f -name "*1*" -or -name "*2*" -print
872 find . -type f -name "*1*" -and -name "*2*" -print
874 The `find` command can acquire a list of files and perform some
875 operation on each file. Try this command out:
877 find . -type f -exec grep Volume {} \;
879 This command finds every file starting from `.`. Then it searches each
880 file for a line which contains the word "Volume". The `{}` refers to
881 the name of each file. The trailing `\;` is used to terminate the
882 command. This command is slow, because it is calling a new instance
883 of `grep` for each item the `find` returns.
885 A faster way to do this is to use the `xargs` command:
887 find . -type f -print | xargs grep Volume
889 `find` generates a list of all the files we are interested in,
890 then we pipe them to `xargs`. `xargs` takes the items given to it
891 and passes them as arguments to `grep`. `xargs` generally only creates
892 a single instance of `grep` (or whatever program it is running).
897 Navigate to the `data` directory. Use one find command to perform each
898 of the operations listed below (except number 2, which does not
899 require a find command):
901 1. Find any file whose name is "NOTES" within `data` and delete it
903 2. Create a new directory called `cleaneddata`
905 3. Move all of the files within `data` to the `cleaneddata` directory
907 4. Rename all of the files to ensure that they end in `.txt` (note:
908 it is ok for the file name to end in `.txt.txt`
910 Hint: If you make a mistake and need to start over just do the
913 1. Navigate to the `1-Shell` directory
915 2. Delete the `data` directory
917 3. Enter the command: `git checkout -- data` You should see that the
918 data directory has reappeared in its original state
922 Redo exercise 4, except rename only the files which do not already end
923 in `.txt`. You will have to use the `man` command to figure out how to
924 search for files which do not match a certain name.
932 **backtick, xargs**: Example find all files with certain text
936 **variables** -> use a path example
946 **Regular Expressions**
950 **Chaining commands together**