Git-Leon a071634419 Update README.md | 7 lat temu | |
---|---|---|
README.md | 7 lat temu |
pwd
, ls
, cd
, cat
, less
, grep
The console (also referred to as the command line, terminal, or shell) is an essential tool for programmers to be familiar with. The command line shell we will be using is called Bash. This lab will focus on the basics for navigating and investigating the file system through the command line. In this lab we will only be using the command line in interactive mode (more on that in the Bash labs). Here are the commands we will focus on:
pwd
- "Print Working Directory" prints out your current location (known as your current working directory or 'cwd')ls
- lists contents of the cwdcd
- change directory. This is how you move around the file system. You can specify the destination as an absolute or relative path.echo
- prints text to the terminalcat
- concatenates zero or more files. Often used to print the contents of a file to the terminal. Often misusedless
/more
- Display contents of a file one page at a time ...just remember that less is more (more or less) and you'll be fine.grep
- search for the specified text or patternhead
- display the first lines of a filetail
- display the last lines of a fileThis lab is an instructional introduction to terminal. There are no unit tests.
It's a good practice to have a directory called Dev
inside your home directory for all development work. You can create one in Finder (right click -> New Folder) or in the terminal (mkdir ~/Dev
). This directory will come in handy in case things go horribly wrong, or if you want to back up all of your work. You should always make sure you are working in Dev or a directory inside of it during this course.
pwd
ls
cd ~
. Use pwd
again to confirm.cd Dev
. If you don't have one you should create one (see above)echo "Hello Terminal" >myFile.txt
. We'll explain this command in greater detail in the next lab.ls
to list the contents of your directory after running that command to see what changed.cat
command.cat
to print the contents of multiple files; Create another file the same way you created myFile.txt and then print them both together (eg: cat myFile.txt myFile2.txt
)cat
is fine for small files, or when you just want to print the entire contents of a file, but if you want to read through a large file it's not great. Try using cat on the file /etc/bashrc_Apple_Terminal
cat
; try viewing it again with less
. You can move around in less
using ENTER, SPACE, and your arrow keys. To exit less, press the letter q
. (note that if this is the first time you've used the terminal, your .bash_history file may still be very small)grep
. You can use this to search for a pattern in a file or other body of text. Try using it on that first file you created like this: grep Hello myFile.txt
. Notice that the result is the whole line where the text you searched for occurs. What happens if you use grep to search for something that isn't in the file?In Part 1 lab you learned to navigate your file system in the terminal. In Part 2 you will learn how to actually make changes to the file system and where to find documentation to get a better grasp on commands you are already familiar with as well as those you haven't seen before. The commands you will practice in this lab are:
touch
mkdir
rmdir
rm
cp
mv
<
,>
,>>
,|
In lab 1 you learned one way to create a file using the echo
command and output redirection. That can be useful but there are other methods that make more sense sometimes. Here we'll focus on two other methods:
touch
command.To create a file using touch, simply type the command followed by the name of the file you want to create, like this: touch filename
. Touch is actually meant to update the timestamp on files, but as a side effect it creates files that are named but don't already exist. It's time to practice touch
ing files
touch
to create two files, called myfile1 and myfile2touch
to create three more files, but all in the same command. Just give touch the filenames you want one right after the other, separated by a spacels -lT
(that's a lowercase 'L' after the dash) command to see the full timestamps of the files you just created. Notice that the first two are different but the last three are all the same.-
There are many text editors out there. We will be using an editor called VIM (short for VI iMproved -- it is a clone of the editor VI with some improvements). Other options that you can explore on your own include emacs and nano (a clone of a program called pico). Here are the VIM commands we'll use along with a few other useful basics (there are many, many, many many more):
:w
- Write; writes the current contents of the buffer (editor) to a file.:q
- Quit; Quits VIM. Complains if you have unsaved changes (use :w
first, or :q!
to force quit and abandon your changes):set number
/:set nonumber
- Enables/disables line numbers.a
- Switch to insert mode after the current character. A
goes to the end of the line instead.i
- Switch to insert mode before the current character. I
goes to the beginning of the line insteadx
- Delete the character under the cursor.dd
- Delete the current line.h
j
k
l
- Work like the left, down, up, and right arrow keys respectively.-
vim
command.esc
key switches to normal mode from insert mode but not the other way -- if you ever get lost just hit escape.:w
command from normal mode. Type :w myfile6
to save this empty file as "myfile6":q
command and list the contents of your directory to confirm that you created a file (Shortcut: :wq
allows you to save a file and quit with just one command)a
key and type the message "Hello Editor!". Remember to press esc
when you finish to switch back to normal mode.A
(note that it's capitalized) command to add text to the end of the line; Press enter to go to the next line and then type a short message. See what happens when you finish the message and press esc
:set number
command.9dd
. You should now see only one line of your message.mkdir
to make a directory with your namecd
ing into it, make a directory inside dir1. When you give the location of a file or directory relative to your current position that's called a relative pathcp
command to copy the file you wrote in the last section. Try using the man file for cp to see what the correct syntax is (anything between [] braces is optional)mv
command.mv
command doesn't have to move a file to a new location; it can simply change the name of the file in place. Change the name to something that starts with a dot (.) such as ".myFile"ls
. Where is the file you just renamed? In Linux and Unix systems, files and directories with names starting in "." are hidden, so by renaming your file you actually made it a hidden file. Let's see how we can view hidden files with ls
man
command lets you view the man(ual) pages for any command that is documented. Try viewing the man pages for ls, cp, and mv. Once you've looked at each of these and have a feel for how they are laid out, take another look at the ls man page and see if you can figure out how to view hidden files (remember, hidden files begin with a dot '.' ) hint: viewing man pages is like using less; you can move up and down in the file with the keyboard and when you want to exit you can press the 'q' key*rm
command deletes files, try using that on one of the files you created.unlink
command. Use this to delete the second file.rm -i
to delete the last file and notice that you have to confirm it before it's deleted.rm
. Notice that on its own it won't delete directories.rmdir
(or rm -d
) to delete the empty directory, then try it on the directory with a file in it.rm -R
command. The -R
flag tells rm
to recursively delete the contents of each directory it encounters and then delete the directory.echo "Hello" >-toughFile
and then try to delete the file created by that command. There are at least two ways to do this.Remember in the previous lab when you typed the command echo "Hello Terminal" >myfile.txt
? That 'greater than' sign is actually a special character in bash used for redirecting output to a file. There are several of these operators for redirecting output and input. The main redirection operators to be aware of are as follows:
<
- Redirects input to come from the specified file. By default input comes from a stream called STDIN (STandarD INput)>
- Redirects output to the specified file (if the file exists its contents are erased first). By default normal program output is directed to a stream called STDOUT (STandarD OUTput) and errors are directed to a stream called STDERR (STandarD ERRor -- Redirect STDERR using 2>
instead)>>
- Appends output to the specified file instead of overwriting it|
- redirects the output from the previous command to be the input of the next command.-
find
command to search for all files on your machine with "core' in the name. You can do that with find / -name core
./dev/null
, effectively throwing away the output from STDERR.corefiles.log
.grep
to search for any lines that contain the string 'lib'. We want the search to be case insensitive so we'll use the -i
flag like so: grep -i lib corefiles.log
(note: your file may have very few lines in it if your computer is rather new and has not had many programs installed yet. It may seem silly to use grep to search for a particular line in a file with only two lines, but when you're searching for an error that occurred only once in over 10000 lines of debugging output you will want to know this tool well.)|
(pipe) character to redirect the output from find
to the input for grep
. Don't forget to throw out the STDERR output; we don't need grep to search through that since we already know it won't contain the files we're looking for.Did you notice how you had to wait a few seconds every time you ran that find command? Sometimes you may need to use a long-running command and don't want it to lock up your terminal the whole time it's running. That's when you would want to run it in the background (by default, any command you run on a terminal runs in the foreground. Let's play with backgrounding and foregrounding a bit.
find | grep
command that you used in the last step, but add the background operator, &
, to the end of it. Notice that you immediately get a prompt, but after a few seconds the result of the grep prints to the command line.Now let's practice one more backgrounding technique with a different command.
python -m SimpleHTTPServer
. This will run forever until you kill it with Ctrl
+C
Ctrl
+Z
to suspend (not kill) that program.jobs
you'll see that python is currently stopped. You can bring it back with fg
, but that will block your prompt again; instead, use bg
to resume it in the background.jobs
. Bring it back to the foreground with fg
and then kill it with Ctrl
+C
For more practice with the basics of the terminal, try the Command Line Crash Course.
vimtutor
-- type this command on your command line for a built-in lesson in VIM basics.
The next lab is here.