(1)基础知识
一:
$ cd 2015
1. cd stands for "change directory". Just as you would click on a folder in Windows Explorer or Finder, cd switches you into the directory you specify. In otherwords, cdchanges the workingdirectory.
2. The directory we change intois 2015. When a file, directory orprogram is passed into a command, it is called an argument. Herethe 2015 directory is an argument for the cd command.
The cd commandtakes a directory name as an argument, and switches into thatdirectory.
二:
$ pwd /home/ccuser/workspace/blog
pwd
stands for "print working directory". It out puts the name of the directory you are currently in, called the working directory.
Here the working directory is blog/. In Codecademy courses, your working directory is usually inside the home/ccuser/workspace/ directory.
Together with ls
, the pwd
commandis useful to show where you are in the filesystem.
三:
$ cd ..
To move up one directory, use cd ..
. Here, cd ..
navigates up from jan/memory/ to jan/.
四:
$ mkdir media
The mkdir
command stands for"make directory". It takes in a directory name as an argument, and then creates a new directory in the current working directory.
Here we used mkdir
to createa new directory named media/insidethe feb/ directory.
五:
touch keyboard.txt
The touch
command creates a new file inside the working directory. It takes in a filename as an argument,and then creates an empty file in the current working directory.
Here we used touch
to createa new file named keyboard.txt insidethe 2014/dec/ directory.
六:小结
From the command line, you can navigate through files andfolders on your computer:
· pwd outputs the name of thecurrent working directory.
· ls lists all files anddirectories in the working directory.
· cd switches you into thedirectory you specify.
· mkdir creates a new directoryin the working directory.
· touch creates a new fileinside the working directory.
(2)浏览和修改文件系统
七:
$ ls -a . .. .preferences action drama comedygenres.xt
1. The ls command lists all files and directories in the working directory.
2. The -a modifies the behavior of the ls command to also list the files and directories starting with a dot (.). Files started with a dot are hidden, and don't appear when using ls alone.
The -a is called an option. Options modify the behavior of commands. Here we used ls -a to displaythe contents of the working directory in more detail.
In additionto -a, the ls command has several more options. Here are three common options:
· -a - lists all contents,including hidden files and directories
· -l - lists all contents ofa directory in long format
· -t - order files anddirectories by the time they were last modified.
八:
$ ls -l drwxr-xr-x 5 cc eng 4096 Jun 24 16:51action drwxr-xr-x 4 cc eng 4096 Jun 24 16:51 comedy drwxr-xr-x 6 cc eng 4096Jun 24 16:51 drama -rw-r--r-- 1 cc eng 0 Jun 24 16:51 genres.txt
The -l option lists files anddirectories as a table. Here there are four rows, with seven columns separatedby spaces. Here's what each column means:
1. Access rights. These areactions that are permitted on a file or directory.
2. Number of hard links. Thisnumber counts the number of child directories and files. This number includesthe parent directory link (..) and current directory link(.).
3. The username of the file's owner.Here the username iscc.
4. The name of the group thatowns the file. Here the group name is eng.
5. The size of the file inbytes.
6. The date & time that thefile was last modified.
7. The name of the file ordirectory.
九:
$ ls -alt
drwxr-xr-x 4 cc eng 4096 Jun 29 12:22 .
-rw-r--r-- 1 cc eng 0 Jun 29 12:22 .gitignore drwxr-xr-x 5 cc eng 4096 Jun 30 14:20 ..
drwxr-xr-x 2 cc eng 4096 Jun 29 12:22 satire drwxr-xr-x 2 cc eng 4096 Jun 29 12:22 slapstick -rw-r--r-- 1 cc eng 14 Jun 29 12:22 the-office.txt
The -t
option orders filesand directories by the time they were last modified.
Inaddition to using each option separately, like ls -a
orls -l
, multiple options can be used together,like ls -alt
.
Here, ls -alt
lists allcontents, including hidden files and directories, in long format, ordered bythe date and time they were last modified.
十:
cp frida.txt lincoln.txt
The cp
command copies files or directories. Here, we copy the contents of frida.txt into lincoln.txt.
十一:
cp biopic/cleopatra.txt historical/
To copy a file into a directory, use cp
with the source file as the first argument and the destination directory as the second argument. Here, we copy the file biopic/cleopatra.txt and place it in the historical/ directory.
cp biopic/ray.txt biopic/notorious.txt historical/
To copy multiple files into a directory, use cp
with a list of source files as the first arguments, and the destination directory as the last argument. Here, we copy the files biopic/ray.txt and biopic/notorious.txt into the historical/ directory.
十二:
cp * satire/
Inaddition to using filenames as arguments, we can use special characters like *
to select groups offiles. These special characters are called wildcards. The *
selectsall files in the working directory, so here we use cp
to copy all files into the satire/ directory.
cp m*.txt scifi/
Here, m*.txt
selects all files in the working directory starting with "m" and ending with".txt", and copies them to scifi/.
十三:
The mv
command moves files.It's similar to cp
in itsusage.
mv superman.txt superhero/
To move a file into a directory, use mv
with the source file as the first argument and the destination directory as the second argument. Here we move superman.txt into superhero/.
mv wonderwoman.txt batman.txt superhero/
To move multiple files into a directory, use mv
with a list of source files as the first arguments, and the destination directory asthe last argument. Here, we move wonderwoman.txt andbatman.txt into superhero/.
mv batman.txt spiderman.txt
To rename a file, use mv
with the old file as the first argument and the new file as the second argument. By moving batman.txt into spiderman.txt,we rename the file as spiderman.txt.
十四:
rm waterboy.txt
The rm
command deletes filesand directories. Here we remove the file waterboy.txt from the filesystem.
rm -r comedy
The -r
is an option that modifies the behavior of the rm
command. The -r
stands for"recursive," and it's used to delete a directory and all of its child directories.
Be careful when you use rm
! It deletes files and directories permanently. There isn't an undelete command, so once you delete a file or directory with rm
, it's gone.
十五:总结
· Options modify the behavior of commands:
o ls -a lists all contents of a directory, including hidden files and directories
o ls -l lists all contents in long format
o ls -t orders files and directories by the time they were last modified
o Multiple options can be used together, like ls -alt
· From the command line, you can also copy, move, and remove files and directories:
o cp copies files
o mv moves and renames files
o rm removes files
o rm -r removes directories
· Wildcards are useful for selecting groups of filesand directories
(3)重定向输入输出
一:
$ echo "Hello"
Hello
The echo
command accepts the string "Hello" as standard input, and echoes the string "Hello"back to the terminal as standard output.
Let's learn more about standard input, standard output, and standard error:
1. standard input,abbreviated as stdin
, is information inputted into the terminal through the keyboard or input device.
2. standard output,abbreviated as stdout
, is the information outputted after a process is run.
3. standard error,abbreviated as stderr
, is an error message outputted by a failed process.
Redirection reroutes standard input, standard output,and standard error to or from a different location.
二:
$ echo "Hello" > hello.txt
The >
command redirects the standard output to a file. Here,"Hello"
is entered as the standard input. The standard output"Hello"
is redirected by >
to the file hello.txt.
$ cat hello.txt
The cat
command outputs the contents of a file to the terminal. When you type cathello.txt
, the contents of hello.txt are displayed.
三:
$ cat oceans.txt > continents.txt
>
takes the standard output of the command on the left, and redirects it to the file on the right. Here the standard output ofcatoceans.txt
is redirected to continents.txt.
Note that >
overwrites all original content in continents.txt.When you view the output data by typing cat
on continents.txt,you will see only the contents of oceans.txt.
四:
$ cat glaciers.txt >> rivers.txt
>>
takes the standard output of the command on the left and appends (adds) it to the file on the right. You can view the output data of the file with cat
and the filename.
Here, the output data of rivers.txt will contain the original contents of rivers.txt with the content of glaciers.txtappended to it.
五:
$ cat < lakes.txt
<
takes the standard input from the file on the right and inputs it into the program on the left. Here, lakes.txt is the standard input for the cat
command. The standard output appears in the terminal.
六:
$ cat volcanoes.txt | wc
|
is a"pipe". The |
takes the standard output of the command on the left, and pipes it as standard input to the command on the right. You can think of this as "command to command" redirection.
Here the output of cat volcanoes.txt
is the standard input of wc
. in turn, the wc
command outputs the number of lines, words, and characters in volcanoes.txt
, respectively.
$ cat volcanoes.txt | wc | cat > islands.txt
Multiple |
s can bechained together. Here the standard output of cat volcanoes.txt
is "piped"to the wc
command.The standard output of wc
is then"piped" to cat
. Finally, the standard output of cat
is redirected to islands.txt
.
You can view the output data of this chain by typingcat islands.txt
.
七:
$ sort lakes.txt
sort
takes the standard input and orders it alphabetically for the standard output. Here, the lakes in sort lakes.txt
are listed in alphabetical order.
$ cat lakes.txt | sort > sorted-lakes.txt
Here, the command takes the standard output from cat lakes.txt
and"pipes" it to sort
. The standard output of sort
is redirected to sorted-lakes.txt.
You can view the output data by typing cat
on the file sorted-lakes.txt.
八:
uniq deserts.txt
uniq
standsfor "unique" and filters out adjacent, duplicate lines in a file.Here uniq deserts.txt
filters out duplicates of "Sahara Desert", because the duplicate of 'SaharaDesert' directly follows the previous instance. The "Kalahari Desert"duplicates are not adjacent, and thus remain.
$ sort deserts.txt | uniq
A more effective way to call uniq
is to call sort
to alphabetize a file, and "pipe" the standard output to uniq
. Here by piping sort deserts.txt
to uniq
, all duplicate lines are alphabetized (and thereby made adjacent) and filtered out.
sort deserts.txt | uniq > uniq-deserts.txt
九:
$ grep Mount mountains.txt
grep
standsfor "global regular expression print".It searches files for lines that match a pattern and returns the results. It is also case sensitive. Here, grep
searches for "Mount" in mountains.txt.
$ grep -i Mount mountains.txt
grep -i
enables the command to be case insensitive. Here,grep
searches for capitalor lowercase strings that match Mount
in mountains.txt.
The above commands are a great way to get started with grep
. If you are familiar with regular expressions, you can use regular expressions to search for patterns in files.
十:
$ grep -R Arctic /home/ccuser/workspace/geography
grep -R
searches all files in a directory and outputs filenames and lines containing matched results. -R
standsfor "recursive". Here grep -R
searches the /home/ccuser/workspace/geography directory for the string"Arctic" and outputs filenames and lines with matched results.
$ grep -Rl Arctic /home/ccuser/workspace/geography
grep -Rl
searches all files in a directory and outputs only filenames with matched results. -R
stands for"recursive" and l
stands for"files with matches". Here grep -Rl
searches the/home/ccuser/workspace/geography directory for the string"Arctic" and outputs filenames with matched results.
十一:查找和替换
sed 's/snow/rain/' forests.txt
sed stands for "streameditor". It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to "find and replace".
Let's look at the expression 's/snow/rain/':
· s: stands for"substitution". it is always used when using sed for substitution.
· snow: the search string, the text to find.
· rain: the replacement string, the text to add in place.
In this case, sed searches forests.txt for the word "snow"and replaces it with "rain." Importantly, the above command will only replace the first instance of "snow" on a line.
$ sed 's/snow/rain/g' forests.txt
The above command uses the g expression, meaning "global". Here sed searches forests.txt for the word "snow" and replaces it with "rain", globally. All instances of "snow" on a line will be turned to"rain".
十二:小结
· Redirection reroutes standardinput, standard output, and standard error.
· The common redirection commands are:
o > redirects standard output of a command to a file, overwriting previous content.
o >> redirects standard output of a command to afile, appending new content to old content.
o < redirects standard input to a command.
o | redirects standard output of a command to another command.
· A number of other commands are powerful when combined with redirection commands:
o sort: sorts lines of text alphabetically.
o uniq: filters duplicate, adjacent lines of text.
o grep: searches for a text pattern and outputs it.
o sed : searches for a text pattern, modifies it, andoutputs it.
(4)配置环境
一:
$ nano hello.txt
nano is a command line text editor. It works just like a desktop text editor like TextEdit or Notepad, except that it is accessible from the command line and only accepts keyboard input.
1. The command nano hello.txt opens a new text file named hello.txt in the nano texteditor.
2. "Hello, I am nano" is a text string entered in nano through the cursor.
3. The menu of keyboard command sat the bottom of the window allow us to save changes to hello.txt and exit nano. The ^ stands for the Ctrl key.
4. Ctrl + O saves a file. 'O' stands for output.
5. Ctrl + X exits the nano program. 'X' stands for exit.
6. Ctrl + G opens a help menu.
7. clear clears the terminal window, moving the commandprompt to the top of the screen.
二:
You created a file in nano called ~/.bash_profile and added a greeting. How does this work?
$ nano ~/.bash_profile
~/.bash_profile is the name of file used to store environment settings. It is commonly called the "bash profile". When a session starts, it will load the contents of the bash profile before executing commands.
· The ~ represents the user's home directory.
· The . indicates a hiddenfile.
· The name ~/.bash_profile is important, since this is how the command line recognizes the bash profile.
1. The command nano ~/.bash_profile opens up~/.bash_profile in nano.
2. The text echo "Welcome, Jane Doe" creates a greeting in the bash profile, which is saved. It tells the command line to echo the string "Welcome, Jane Doe" when a terminal session begins.
3. The command source ~/.bash_profile activates the changes in ~/.bash_profile for the current session. Instead of closing the terminal and needing to start a new session, source makes the changes available right away in the session we are in.
三:创建快捷方式
alias pd="pwd"
The alias command allows you to create keyboard shortcuts, or aliases, for commonly used commands.
1. Here alias pd="pwd" creates the alias pd for the pwdcommand, which is then savedin the bash profile. Each time you enter pd, the output will be the same as thepwd command.
2. The command source ~/.bash_profile makes the aliaspd available in the current session.
四:
export USER="Jane Doe"
environment variables are variables that can be used across commands and programs and hold information about the environment.
1. The line USER="Jane Doe" sets the environment variable USER to a name "Jane Doe". Usually the USER variable is set to the name of the computer's owner.
2. The line export makes the variable to be available to all child sessions initiated from the session you are in. This is a way to make the variable persist across programs.
3. At the command line, the command echo $USER returns the value of the variable. Note that $ is always used when returning a variable's value. Here, the command echo $USER returns the name set for the variable.
五:
export PS1=">> "
PS1 is a variable that defines the makeup and style of the command prompt.
1. export PS1=">> " sets the command prompt variable and exports the variable. Here we change the default command promptfrom $to >>.
2. After using the source command, the command line displays the new command prompt.
六:
$ echo $HOME
The HOME variableis an environment variable that displays the path of the home directory. Here by typing echo $HOME, the terminal displays the path/home/ccuser as output.
七:
$ echo $PATH/home/ccuser/.gem/ruby/2.0.0/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
PATH is an environment variable that stores a list of directories separated by a colon. Looking carefully,echo $PATH lists the following directories:
1. /home/ccuser/.gem/ruby/2.0.0/bin
2. /usr/local/sbin
3. /usr/local/bin
4. /usr/bin
5. /usr/sbin
6. /sbin
7. /bin
Each directory contains scripts for the command line to execute. The PATH variables imply lists which directories contain scripts.
八:小结
· The environment refersto the preferences and settings of the current user.
· The nano editor is a command line text editor used to configure the environment.
· ~/.bash_profile is where environment settings are stored. You can edit this file with nano.
· environment variables are variables that can be used across command sand programs and hold information about the environment.
o export VARIABLE="Value" sets and exports anenvironment variable.
o USER is the name of the current user.
o PS1 is the command prompt.
o HOME is the home directory. It is usually notcustomized.
o PATH returns a colon separated list of file paths.It is customized in advanced cases.
o env returns a list of environment variables.