読者です 読者をやめる 読者になる 読者になる

プログラミング初心者がコマンドラインを学んでみる Learn the Command line | Codecademy

ターミナル Codecademy

注文したMacbookが明日届くようなので、Learn to code | Codecademyでターミナルの基礎について学んでみました。備忘録としてまとめて残しておきます。

$

shell prompt
シェルプロンプト。コマンド受付の準備ができている状態。

ls

lists the files,folders inside the folder you are in.


f:id:uraway:20151028125220p:plain
最初のディレクトリはルートディレクトリ。ファイルシステムの他すべてのディレクトリとファイルの親。
親のディレクトリは子のディレクトリとファイルを内包できる。各ディレクトリはさらにファイルと、子のファイルを内包する。

pwd

stands for "print working directory".

cd (directory)

stands for "change directory".

cd ..

moves up one directory.

mkdir (directory-name)

makes directory in the working directory.

touch (file-name.txt)

creates a new file inside the working directory.

options of list command

-a

lists all contents, including hidden files and directories.
.から始まるファイルは通常出力されない。

-l

lists all contents of a directory in long format.

1. Access rights. These are actions that are permitted on a file or directory.
2. Number of hard links. This number counts the number of child directories and files. This number includes the parent directory link (..) and current directory link (.).
3. The username of the file's owner.
4. The name of the group that owns the file.
5. The size of the file in bytes.
6. The date & time that the file was last modified.
7. The name of the file or directory.

-t

orders files and directories by the time they were last modified.

-alt

lists all contents, including hidden files and directories, in long format, ordered by the date and time they were last modified.

copy, move and remove

cp

the source file as the 1st argument
the destination file(or directory) as the 2nd argument
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.

cp biopic/ray.txt biopic/notorious.txt historical/
(*)wildcard

selects all files in the working 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/.

mv

moves files.

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

removes the files and directories

rm -r

'r' stands for recursive!
it's used to delete a directory and all of its child directories.

I/O command

echo

accepts the string as standard input, and echoes the string back to the terminal as standard output.

$ 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.
"Hello"はechoコマンドによって標準入力され、>コマンドによって、標準出力"Hello"はhello.txtファイルにリダイレクトされます。

cat

outputs the contents of a file to the terminal.

$ 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.
 >>は左側の標準出力を取り、右側のファイルに加えます(アペンド)。catコマンドでその標準出力を見ることができます。

$ 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 of cat oceans.txt is redirected to continents.txt.
 >は左側の標準出力を取り、右側のファイルにリダイレクトします。つまり、oceans.txtの標準出力をcontinents.txtの標準出力として表示します。

$ 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.
 <は右側のファイルから標準入力を取り、左側のプログラムにインプットします。ここでは、lakes.txtはcatコマンドの標準入力となり、catはそれをターミナルに標準出力します。

$ 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.
 |は”パイプ”です。左側のコマンドの標準出力を受け取り、それを右側のコマンドに標準入力として渡します。"command to command"のリダイレクトと考えることができます。
 ここでは、volcanoes.txtはwcの標準入力です。wcコマンドはvolcanoes.txtのlines,words,charactersの数をそれぞれ出力します。

sort

takes the standard input and orders it alphabetically for the standard output.

uniq

stands for "unique" and filters out adjacent, duplicate lines in a file.
 隣り合った複製を取り除きます。sortとともに用いられることが多い。

grep
$ grep Mount mountains.txt

grep stands for "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.
 特定のlineを探して、その結果を返します。ここではmountains.txtの中の"Mount"のつくlineを
探して、結果を出力します。大文字小文字を区別します。

grep -i

case insensitive
 大文字小文字を区別しません。

grep -R

searches all files in a directory and outputs filenames and lines containing matched results. -R stands for "recursive".
 ディレクトリすべてのファイルを検索し、一致した結果を含むラインとファイル名を出力する。-Rは’再帰’を意味する。 

grep -Rl

searches all files in a directory and outputs only filenames with matched results.-R stands for "recursive" and lstands for "files with matches".

sed

stands for "stream editor". It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to "find and replace".

$ sed 's/snow/rain/' forests.txt

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はforests.txtのなかの"snow"を探し、"rain"に置き換える。注意すべきは、上記コマンドでは、最初の"snow"だけが置き換えられる。

$ 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".

nano

is a command line text editor.
1. Ctrl + O saves a file. 'O' stands for output.
2. Ctrl + X exits the nano program. 'X' stands for exit.
3. Ctrl + G opens a help menu.
4. clear clears the terminal window, moving the command prompt to the top of the screen.

~/.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 hidden file.


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

allows you to create keyboard shortcuts, or aliases, for commonly used commands.

alias pd="pwd"

Here alias pd="pwd" creates the alias pd for the pwd command, which is then saved in the bash profile. Each time you enter pd, the output will be the same as the pwd command.
The command source ~/.bash_profile makes the alias pd available in the current session.

history

the command line outputs a history of commands that were entered in the current session.

export
export USER="Jane Doe"

environment variables are variables that can be used across commands and programs and hold information about the environment.
 環境変数は、コマンドとプログラムを越えて使われ、環境に関する情報を保持する変数である。
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.
 exportによって、プログラムを越えて、変数の設定を持続させることができます。

echo $USER returns the value of the variable. Note that $ is always used when returning a variable's value.
 変数に格納された値を取り出します。

export PS1=">> "

PS1 is a variable that defines the makeup and style of the command prompt.
export PS1=">> " sets the command prompt variable and exports the variable. Here we change the default command prompt from $ to >>. After using the source command, the command line displays the new command prompt.

The HOME variable is an environment variable that displays the path of the home directory.

PATH is an environment variable that stores a list of directories separated by a colon.

Each directory contains scripts for the command line to execute. The PATHvariable simply lists which directories contain scripts.
For example, many commands we've learned are scripts stored in the /bindirectory.

/bin/pwd

This is the script that is executed when you type the pwd command.

/bin/ls

This is the script that is executed when you type the ls command.

env

stands for "environment", and returns a list of the environment variables for the current user.

env | grep PATH

env | grep PATH is a command that displays the value of a single environment variable. Here the standard output of env is "piped" to the grep command. grep searches for the value of the variable PATH and outputs it to the terminal.