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**
9 **Modified by : Sasha Wood**
10 **This presentation originally developed by: Milad Fatenejad**
12 # What is the shell how do I access the shell?
14 The *shell* is a program that presents a command line interface
15 which allows you to control your computer using commands entered
16 with a keyboard instead of controlling graphical user interfaces
17 (GUIs) with a mouse/keyboard combination.
19 Use the GUI to open the tutorial on github. Single click on the "Firefox
20 Web Browser". Type in the URL:
21 github.com/JHU-SWC-2012/SWC-bootcamp
22 Click on the directory named `1-Shell`.
24 A *terminal* is a program you run that gives you access to the
25 shell. There are many different terminal programs that vary across
28 There are many reasons to learn about the shell. In my opinion, the
29 most important reasons are that:
31 1. It is very common to encounter the shell and
32 command-line-interfaces in scientific computing, so you will
33 probably have to learn it eventually
35 2. The shell is a really powerful way of interacting with your
36 computer. GUIs and the shell are complementary - by knowing both
37 you will greatly expand the range of tasks you can accomplish with
38 your computer. You will also be able to perform many tasks more
41 The shell is just a program and there are many different shell
42 programs that have been developed. The most common shell (and the one
43 we will use) is called the Bourne-Again SHell (bash). Even if bash is
44 not the default shell, it is usually installed on most systems and can be
45 started by typing `bash` in the terminal. Many commands, especially a
46 lot of the basic ones, work across the various shells but many things
47 are different. I recommend sticking with bash and learning it well.
48 [Here is a link for more information](http://en.wikipedia.org/wiki/Bash_(Unix_shell))
50 To open a terminal, just single click on the "Terminal" icon on the
53 # The Example: Manipulating Experimental Data Files
55 We will spend most of our time learning about the basics of the shell
56 by manipulating some experimental data from a hearing test. To get
57 the data for this test, you will need internet access. Just enter the
60 git clone https://github.com/thehackerwithin/boot-camps.git
64 git checkout 2013-01-chicago
66 These 2 commands will grab all of the data needed for this workshop from the
71 One very basic command is `echo`. This command just prints text to
72 the terminal. Try the command:
76 Then press enter. You should see the text "Hello, World" printed back
77 to you. The echo command is useful for printing from a shell script,
78 for displaying variables, and for generating known values to pass
81 ## Moving around the file system
83 Let's learn how to move around the file system using command line
84 programs. This is really easy to do using a GUI (just click on
85 things). Once you learn the basic commands, you'll see that it is
86 really easy to do in the shell too.
88 First we have to know where we are. The program `pwd` (print working
89 directory) tells you where you are sitting in the directory tree. The
90 command `ls` will list the files in files in the current
91 directory. Directories are often called "folders" because of how they
92 are represented in GUIs. Directories are just listings of files. They
93 can contain other files or directories.
95 Whenever you start up a terminal, you will start in a special
96 directory called the *home* directory. Every user has their own home
97 directory where they have full access to do whatever they want. In
98 this case, the `pwd` command tells us that we are in the `/home/swc`
99 directory. This is the home directory for the `swc` user. That is our
100 user name. You can always find out your user name by entering the
105 When you enter the `ls` command lists the contents of the current
106 directory. There are several items in the home directory, notice that
107 they are all colored blue. This tells us that all of these items are
108 directories as opposed to files.
110 Lets create an empty file using the `touch` command. Enter the
115 Then list the contents of the directory again. You should see that a
116 new entry, called `testfile`, exists. It is colored white meaning that
117 it is a file, as opposed to a directory. The `touch` command just
118 creates an empty file.
120 Some terminals will not color the directory entries in this very
121 convenient way. In those terminals, use `ls -F` instead of `ls`. The
122 `-F` argument modifies the results so that a slash is placed at the
123 end of directories. If the file is *executable* meaning that it can be
124 run like a program, then a star fill be placed of the file name.
126 You can also use the command `ls -l` to see whether items in a
127 directory are files or directories. `ls -l` gives a lot more
128 information too, such as the size of the file and information about
129 the owner. If the entry is a directory, then the first letter will be
130 a "d". The fifth column shows you the size of the entries in
131 bytes. Notice that `testfile` has a size of zero.
133 Now, let's get rid of `testfile`. To remove a file, just enter the
138 The `rm` command can be used to remove files. If you enter `ls` again,
139 you will see that `testfile` is gone.
142 **Changing Directories**
144 Now, let's move to a different directory. The command `cd` (change
145 directory) is used to move around. Let's move into the `SWC-bootcamp`
146 directory. Enter the following command:
150 Now use the `ls` command to see what is inside this directory. You
151 will see that there is an entry which is green. This means that this
152 is an executable. If you use `ls -F` you will see that this file ends
155 This directory contains all of the material for this boot camp. Now
156 move to the directory containing the data for the shell tutorial:
160 If you enter the `cd` command by itself, you will return to the home
161 directory. Try this, and then navigate back to the `1-Shell`
166 Most programs take additional arguments that control their exact
167 behavior. For example, `-F` and `-l` are arguments to `ls`. The `ls`
168 program, like many programs, take a lot of arguments. But how do we
169 know what the options are to particular commands?
171 Most commonly used shell programs have a manual. You can access the
172 manual using the `man` program. Try entering:
176 This will open the manual page for `ls`. Use the space key to go
177 forward and b to go backwards. When you are done reading, just hit `q`
180 Programs that are run from the shell can get extremely complicated. To
181 see an example, open up the manual page for the `find` program,
182 which we will use later this session. No one can possibly learn all of
183 these arguments, of course. So you will probably find yourself
184 referring back to the manual page frequently.
186 **Examining the contents of other directories**
188 By default, the `ls` commands lists the contents of the working
189 directory (i.e. the directory you are in). You can always find the
190 directory you are in using the `pwd` command. However, you can also
191 give `ls` the names of other directories to view. Navigate to the
192 home directory if you are not already there. Then enter the
197 This will list the contents of the `SWC-bootcamp` directory without
198 you having to navigate there. Now enter:
200 ls SWC-bootcamp/1-Shell
202 This prints the contents of `1-Shell`. The `cd` command works in a
203 similar way. Try entering:
205 cd SWC-bootcamp/1-Shell
207 and you will jump directly to `1-Shell` without having to go through
208 the intermediate directory.
210 ## Full vs. Relative Paths
212 The `cd` command takes an argument which is the directory
213 name. Directories can be specified using either a *relative* path a
214 full *path*. The directories on the computer are arranged into a
215 hierarchy. The full path tells you where a directory is in that
216 hierarchy. Navigate to the home directory. Now, enter the `pwd`
217 command and you should see:
221 which is the full name of your home directory. This tells you that you
222 are in a directory called `swc`, which sits inside a directory called
223 `home` which sits inside the very top directory in the hierarchy. The
224 very top of the hierarchy is a directory called `/` which is usually
225 referred to as the *root directory*. So, to summarize: `swc` is a
226 directory in `home` which is a directory in `/`.
228 Now enter the following command:
230 cd /home/swc/SWC-bootcamp/1-Shell
232 This jumps to `1-Shell`. Now go back to the home directory. We saw
233 earlier that the command:
235 cd SWC-bootcamp/1-Shell
237 had the same effect - it took us to the `1-Shell` directory. But,
238 instead of specifying the full path
239 (`/home/swc/SWC-bootcamp/1-Shell`), we specified a *relative path*. In
240 other words, we specified the path relative to our current
241 directory. A full path always starts with a `/`. A relative path does
242 not. You can usually use either a full path or a relative path
243 depending on what is most convenient. If we are in the home directory,
244 it is more convenient to just enter the relative path since it
245 involves less typing.
247 Now, list the contents of the /bin directory. Do you see anything
251 ## Saving time with shortcuts, wild cards, and tab completion
255 There are some shortcuts which you should know about. Dealing with the
256 home directory is very common. So, in the shell the tilde character,
257 `~`, is a shortcut for your home directory. Navigate to the `1-Shell`
258 directory, then enter the command:
262 This prints the contents of your home directory, without you having to
263 type the full path. The shortcut `..` always refers to the directory
264 above your current directory. Thus:
268 prints the contents of the `/home/swc/SWC-bootcamp`. You can chain
273 prints the contents of `/home/swsc` which is your home
274 directory. Finally, the special directory `.` always refers to your
275 current directory. So, `ls`, `ls .`, and `ls ././././.` all do the
276 same thing, they print the contents of the current directory. This may
277 seem like a useless shortcut right now, but we'll see when it is
278 needed in a little while.
280 To summarize, the commands `ls ~`, `ls ~/.`, `ls ../../`, and `ls
281 /home/swc` all do exactly the same thing. These shortcuts are not
282 necessary, they are provided for your convenience.
284 **Our data set: Cochlear Implants**
286 A cochlear implant is a small electronic device that is surgically
287 implanted in the inner ear to give deaf people a sense of
288 hearing. More than a quarter of a million people have them, but there
289 is still no widely-accepted benchmark to measure their effectiveness.
290 In order to establish a baseline for such a benchmark, our supervisor
291 got teenagers with CIs to listen to audio files on their computer and
294 1. the quietest sound they could hear
295 2. the lowest and highest tones they could hear
296 3. the narrowest range of frequencies they could discriminate
298 To participate, subjects attended our laboratory and one of our lab
299 techs played an audio sample, and recorded their data - when they
300 first heard the sound, or first heard a difference in the sound. Each
301 set of test results were written out to a text file, one set per file.
302 Each participant has a unique subject ID, and a made-up subject name.
303 Each experiment has a unique experiment ID. The experiment has
304 collected 351 files so far.
306 The data is a bit of a mess! There are inconsistent file names, there
307 are extraneous "NOTES" files that we'd like to get rid of, and the
308 data is spread across many directories. We are going to use shell
309 commands to get this data into shape. By the end we would like to:
311 1. Put all of the data into one directory called "alldata"
313 2. Have all of the data files in there, and ensure that every file
314 has a ".txt" extension
316 3. Get rid of the extraneous "NOTES" files
318 If we can get through this example in the available time, we will move
319 onto more advanced shell topics...
323 Navigate to the `~/SWC-bootcamp/Shell-1/data/THOMAS` directory. This
324 directory contains our hearing test data for THOMAS. If we type `ls`,
325 we will see that there are a bunch of files which are just four digit
326 numbers. By default, `ls` lists all of the files in a given
327 directory. The `*` character is a shortcut for "everything". Thus, if
328 you enter `ls *`, you will see all of the contents of a given
329 directory. Now try this command:
333 This lists every file that ends with a `1`. This command:
337 Lists every file in `/usr/bin` that ends in the characters `.sh`. And
342 lists every file in the current directory which contains the number
343 `4`, and ends with the number `1`. There are four such files: `0241`,
344 `0341`, `0431`, and `0481`.
346 So how does this actually work? Well...when the shell (bash) sees a
347 word that contains the `*` character, it automatically looks for files
348 that match the given pattern. In this case, it identified four such
349 files. Then, it replaced the `*4*1` with the list of files, separated
350 by spaces. In other the two commands:
353 ls 0241 0341 0431 0481
355 are exactly identical. The `ls` command cannot tell the difference
356 between these two things.
361 Do each of the following using a single `ls` command without
362 navigating to a different directory.
364 1. List all of the files in `/bin` that contain the letter `a`
365 2. List all of the files in `/bin` that contain the letter `a` or the letter `b`
366 3. List all of the files in `/bin` that contain the letter `a` AND the letter `b`
372 Navigate to the home directory. Typing out directory names can waste a
373 lot of time. When you start typing out the name of a directory, then
374 hit the tab key, the shell will try to fill in the rest of the
375 directory name. For example, enter:
379 The shell will fill in the rest of the directory name for
380 `SWC-bootcamp`. Now enter:
384 When you hit the first tab, nothing happens. The reason is that there
385 are multiple directories in the home directory which start with
386 3. Thus, the shell does not know which one to fill in. When you hit
387 tab again, the shell will list the possible choices.
389 Tab completion can also fill in the names of programs. For example,
390 enter `e<tab><tab>`. You will see the name of every program that
391 starts with an `e`. One of those is `echo`. If you enter `ec<tab>` you
392 will see that tab completion works.
396 You can easily access previous commands. Hit the up arrow.
397 Hit it again. You can step backwards through your command history.
398 The down arrow takes your forwards in the command history.
400 ^-C will cancel the command you are writing, and give you a fresh prompt.
402 ^-R will do a reverse-search through your command history. This
407 Commands like `ls`, `rm`, `echo`, and `cd` are just ordinary programs
408 on the computer. A program is just a file that you can *execute*. The
409 program `which` tells you the location of a particular program. For
414 Will return "/bin/ls". Thus, we can see that `ls` is a program that
415 sits inside of the `/bin` directory. Now enter:
419 You will see that `find` is a program that sits inside of the
420 `/usr/bin` directory.
422 So ... when we enter a program name, like `ls`, and hit enter, how
423 does the shell know where to look for that program? How does it know
424 to run `/bin/ls` when we enter `ls`. The answer is that when we enter
425 a program name and hit enter, there are a few standard places that the
426 shell automatically looks. If it can't find the program in any of
427 those places, it will print an error saying "command not found". Enter
432 This will print out the value of the `PATH` environment variable. More
433 on environment variables later. Notice that a list of directories,
434 separated by colon characters, is listed. These are the places the
435 shell looks for programs to run. If your program is not in this list,
436 then an error is printed. The shell ONLY checks in the places listed
437 in the `PATH` environment variable.
439 Navigate to the `1-Shell` directory and list the contents. You will
440 notice that there is a program (executable file) called `hello` in
441 this directory. Now, try to run the program by entering:
445 You should get an error saying that hello cannot be found. That is
446 because the directory `/home/swc/SWC-bootcamp/1-Shell` is not in the
447 `PATH`. You can run the `hello` program by entering:
451 Remember that `.` is a shortcut for the current working
452 directory. This tells the shell to run the `hello` program which is
453 located right here. So, you can run any program by entering the path
454 to that program. You can run `hello` equally well by specifying:
456 /home/swc/SWC-bootcamp/1-Shell/hello
462 When there are no `/` characters, the shell assumes you want to look
463 in one of the default places for the program.
468 We now know how to switch directories, run programs, and look at the
469 contents of directories, but how do we look at the contents of files?
471 The easiest way to examine a file is to just print out all of the
472 contents using the program `cat`. Enter the following command:
476 This prints out the contents of the `ex_data.txt` file. If you enter:
478 cat ex_data.txt ex_data.txt
480 It will print out the contents of `ex_data.txt` twice. `cat` just
481 takes a list of file names and writes them out one after another (this
482 is where the name comes from, `cat` is short for concatenate).
487 1. Print out the contents of the `~/SWC-bootcamp/1-Shell/dictionary.txt`
488 file. What does this file contain?
490 2. Without changing directories, (you should still be in `1-Shell`),
491 use one short command to print the contents of all of the files in
492 the /home/swc/SWC-bootcamp/1-Shell/data/THOMAS directory.
496 `cat` is a terrific program, but when the file is really big, it can
497 be annoying to use. The program, `less`, is useful for this
498 case. Enter the following command:
500 less ~/SWC-bootcamp/1-Shell/dictionary.txt
502 `less` opens the file, and lets you navigate through it. The commands
503 are identical to the `man` program. Use "space" to go forward and hit
504 the "b" key to go backwards. The "g" key goes to the beginning of the
505 file and "G" goes to the end. Finally, hit "q" to quit.
507 `less` also gives you a way of searching through files. Just hit the
508 "/" key to begin a search. Enter the name of the word you would like
509 to search for and hit enter. It will jump to the next location where
510 that word is found. Try searching the `dictionary.txt` file for the
511 word "cat". If you hit "/" then "enter", `less` will just repeat
512 the previous search. `less` searches from the current location and
513 works its way forward. If you are at the end of the file and search
514 for the word "cat", `less` will not find it. You need to go to the
515 beginning of the file and search.
517 Remember, the `man` program uses the same commands, so you can search
518 documentation using "/" as well!
523 Use the commands we've learned so far to figure out how to search
524 in reverse while using `less`.
531 Let's turn to the experimental data from the hearing tests that we
532 began with. This data is located in the `~/SWC-bootcamp/1-Shell/data`
533 directory. Each subdirectory corresponds to a particular participant
534 in the study. Navigate to the `Bert` subdirectory in `data`. There
535 are a bunch of text files which contain experimental data
536 results. Lets print them all:
540 Now enter the following command:
542 cat au* > ../all_data
544 This tells the shell to take the output from the `cat au*` command and
545 dump it into a new file called `../all_data`. To verify that this
546 worked, examine the `all_data` file. If `all_data` had already
547 existed, we would overwritten it. So the `>` character tells the shell
548 to take the output from what ever is on the left and dump it into the
549 file on the right. The `>>` characters do almost the same thing,
550 except that they will append the output to the file if it already
556 Use `>>`, to append the contents of all of the files which contain the
557 number 4 in the directory:
559 /home/swc/SWC-bootcamp/1-Shell/data/gerdal
561 to the existing `all_data` file. Thus, when you are done `all_data`
562 should contain all of the experiment data from Bert and any
563 experimental data file from gerdal that contains the number 4.
568 ## Creating, moving, copying, and removing
570 We've created a file called `all_data` using the redirection operator
571 `>`. This is critical file so we have to make copies so that the data
572 is backed up. Lets copy the file using the `cp` command. The `cp`
573 command backs up the file. Navigate to the `data` directory and enter:
575 cp all_data all_data_backup
577 Now `all_data_backup` has been created as a copy of `all_data`. We can
578 move files around using the command `mv`. Enter this command:
580 mv all_data_backup /tmp/
582 This moves `all_data_backup` into the directory `/tmp`. The directory
583 `/tmp` is a special directory that all users can write to. It is a
584 temporary place for storing files. Data stored in `/tmp` is
585 automatically deleted when the computer shuts down.
587 The `mv` command is also how you rename files. Since this file is so
588 important, let's rename it:
590 mv all_data all_data_IMPORTANT
592 Now the file name has been changed to all_data_IMPORTANT. Let's delete
595 rm /tmp/all_data_backup
597 The `mkdir` command is used to create a directory. Just enter `mkdir`
598 followed by a space, then the directory name.
605 1. Rename the `all_data_IMPORTANT` file to `all_data`.
606 2. Create a directory in the `data` directory called `foo`
607 3. Then, copy the `all_data` file into `foo`
611 By default, `rm`, will NOT delete directories. You can tell `rm` to
612 delete a directory using the `-r` option. Enter the following command:
619 The `wc` program (word count) counts the number of lines, words, and
620 characters in one or more files. Make sure you are in the `data`
621 directory, then enter the following command:
625 For each of the files indicated, `wc` has printed a line with three
626 numbers. The first is the number of lines in that file. The second is
627 the number of words. Finally, the total number of characters is
628 indicated. The final line contains this information summed over all of
629 the files. Thus, there were 10445 characters in total.
631 Remember that the `Bert/*` and `gerdal/*4*` files were merged
632 into the `all_data` file. So, we should see that `all_data` contains
633 the same number of characters:
637 Every character in the file takes up one byte of disk space. Thus, the
638 size of the file in bytes should also be 10445. Let's confirm this:
642 Remember that `ls -l` prints out detailed information about a file and
643 that the fifth column is the size of the file in bytes.
648 Figure out how to get `wc` to print the length of the longest line in
653 ## The awesome power of the Pipe
655 Suppose I wanted to only see the total number of character, words, and
656 lines across the files `Bert/*` and `gerdal/*4*`. I don't want to
657 see the individual counts, just the total. Of course, I could just do:
661 Since this file is a concatenation of the smaller files. Sure, this
662 works, but I had to create the `all_data` file to do this. Thus, I
663 have wasted a precious 7062 bytes of hard disk space. We can do this
664 *without* creating a temporary file, but first I have to show you two
665 more commands: `head` and `tail`. These commands print the first few,
666 or last few, lines of a file, respectively. Try them out on
672 The `-n` option to either of these commands can be used to print the
673 first or last `n` lines of a file. To print the first/last line of the
679 Let's turn back to the problem of printing only the total number of
680 lines in a set of files without creating any temporary files. To do
681 this, we want to tell the shell to take the output of the `wc Bert/*
682 gerdal/*4*` and send it into the `tail -n 1` command. The `|`
683 character (called pipe) is used for this purpose. Enter the following
686 wc Bert/* gerdal/Data0559 | tail -n 1
688 This will print only the total number of lines, characters, and words
689 across all of these files. What is happening here? Well, `tail`, like
690 many command line programs will read from the *standard input* when it
691 is not given any files to operate on. In this case, it will just sit
692 there waiting for input. That input can come from the user's keyboard
693 *or from another program*. Try this:
697 Notice that your cursor just sits there blinking. Tail is waiting for
698 data to come in. Now type:
705 then CONTROL+d. You should is the lines:
710 printed back at you. The CONTROL+d keyboard shortcut inserts an
711 *end-of-file* character. It is sort of the standard way of telling the
712 program "I'm done entering data". The `|` character is replaces the
713 data from the keyboard with data from another command. You can string
714 all sorts of commands together using the pipe.
716 The philosophy behind these command line programs is that none of them
717 really do anything all that impressive. BUT when you start chaining
718 them together, you can do some really powerful things really
719 efficiently. If you want to be proficient at using the shell, you must
720 learn to become proficient with the pipe and redirection operators:
724 **A sorting example**
726 Let's create a file with some words to sort for the next example. We
727 want to create a file which contains the following names:
734 To do this, we need a program which allows us to create text
735 files. There are many such programs, the easiest one which is
736 installed on almost all systems is called `nano`. Navigate to `/tmp`
737 and enter the following command:
741 Now enter the four names as shown above. When you are done, press
742 CONTROL+O to write out the file. Press enter to use the file name
743 `toBeSorted`. Then press CONTROL+x to exit `nano`.
745 When you are back to the command line, enter the command:
749 Notice that the names are now printed in alphabetical order.
754 Use the `echo` command and the append operator, `>>`, to append your
755 name to the file, then sort it and make a new file called Sorted.
759 Let's navigate back to `~/SWC-bootcamp/1-Shell/data`. Enter the following command:
761 wc Bert/* | sort -k 3 -n
763 We are already familiar with what the first of these two commands
764 does: it creates a list containing the number of characters, words,
765 and lines in each file in the `Bert` directory. This list is then
766 piped into the `sort` command, so that it can be sorted. Notice there
767 are two options given to sort:
769 1. `-k 3`: Sort based on the third column
770 2. `-n`: Sort in numerical order as opposed to alphabetical order
772 Notice that the files are sorted by the number of characters.
777 Use the `man` command to find out how to sort the output from `wc` in
785 Combine the `wc`, `sort`, `head` and `tail` commands so that only the
786 `wc` information for the largest file is listed
788 Hint: To print the smallest file, use:
790 wc Bert/* | sort -k 3 -n | head -n 1
794 Printing the smallest file seems pretty useful. We don't want to type
795 out that long command often. Let's create a simple script, a simple
796 program, to run this command. The program will look at all of the
797 files in the current directory and print the information about the
798 smallest one. Let's call the script `smallest`. We'll use `nano` to
799 create this file. Navigate to the `data` directory, then:
803 Then enter the following text:
806 wc * | sort -k 3 -n | head -n 1
808 Now, `cd` into the `Bert` directory and enter the command
809 `../smallest`. Notice that it says permission denied. This happens
810 because we haven't told the shell that this is an executable
811 file. If you do `ls -l ../smallest`, it will show you the permissions on
812 the left of the listing.
814 Enter the following commands:
816 chmod a+x ../smallest
819 The `chmod` command is used to modify the permissions of a file. This
820 particular command modifies the file `../smallest` by giving all users
821 (notice the `a`) permission to execute (notice the `x`) the file. If
826 You will see that the file name is green and the permissions have changed.
827 Congratulations, you just created your first shell script!
831 You can search the contents of a file using the command `grep`. The
832 `grep` program is very powerful and useful especially when combined
833 with other commands by using the pipe. Navigate to the `Bert`
834 directory. Every data file in this directory has a line which says
835 "Range". The range represents the smallest frequency range that can be
836 discriminated. Lets list all of the ranges from the tests that Bert
844 Create an executable script called `smallestrange` in the `data`
845 directory, that is similar to the `smallest` script, but prints the
846 file containing the file with the smallest Range. Use the commands
847 `grep`, `sort`, and `tail` to do this.
854 The `find` program can be used to find files based on arbitrary
855 criteria. Navigate to the `data` directory and enter the following
860 This prints the name of every file or directory, recursively, starting
861 from the current directory. Let's exclude all of the directories:
863 find . -type f -print
865 This tells `find` to locate only files. Now try these commands:
867 find . -type f -name "*1*"
868 find . -type f -name "*1*" -or -name "*2*" -print
869 find . -type f -name "*1*" -and -name "*2*" -print
871 The `find` command can acquire a list of files and perform some
872 operation on each file. Try this command out:
874 find . -type f -exec grep Volume {} \;
876 This command finds every file starting from `.`. Then it searches each
877 file for a line which contains the word "Volume". The `{}` refers to
878 the name of each file. The trailing `\;` is used to terminate the
879 command. This command is slow, because it is calling a new instance
880 of `grep` for each item the `find` returns.
882 A faster way to do this is to use the `xargs` command:
884 find . -type f -print | xargs grep Volume
886 `find` generates a list of all the files we are interested in,
887 then we pipe them to `xargs`. `xargs` takes the items given to it
888 and passes them as arguments to `grep`. `xargs` generally only creates
889 a single instance of `grep` (or whatever program it is running).
894 Navigate to the `data` directory. Use one find command to perform each
895 of the operations listed below (except number 2, which does not
896 require a find command):
898 1. Find any file whose name is "NOTES" within `data` and delete it
900 2. Create a new directory called `cleaneddata`
902 3. Move all of the files within `data` to the `cleaneddata` directory
904 4. Rename all of the files to ensure that they end in `.txt` (note:
905 it is ok for the file name to end in `.txt.txt`
907 Hint: If you make a mistake and need to start over just do the
910 1. Navigate to the `1-Shell` directory
912 2. Delete the `data` directory
914 3. Enter the command: `git checkout -- data` You should see that the
915 data directory has reappeared in its original state
919 Redo exercise 4, except rename only the files which do not already end
920 in `.txt`. You will have to use the `man` command to figure out how to
921 search for files which do not match a certain name.
929 **backtick, xargs**: Example find all files with certain text
933 **variables** -> use a path example
943 **Regular Expressions**
947 **Chaining commands together**