SlideShare a Scribd company logo
1 of 33
Download to read offline
プログラミング講座
#4 ターミナルを使いこなす
@ZOI_dayo
「ターミナル」とは?
こんなの→
パソコンいじるときによく使う
開き方
Windowsの場合、右下の検索欄に「
cmd」と入力、「開く」を押す
macの場合、Finderで「アプリケーション」→「ユーティリティ」の中の
「ターミナル.app」を開く
→ なんか黒くて文字入れる画面が出てくる
使い方
基本は「コマンド入れてEnter」を繰り返すだけ(どんなコマンドなのかは後述
)
ここで「カレントディレクトリ」をいうのを知っとくべき
(※ ディレクトリ≒フォルダなので「現在位置フォルダ」という意味
)
カレントディレクトリ
まずは「pwd」といれてEnterを押してみる
(Windowsの人は「cd」)
「/Users/〇〇」とかなんとか表示される
これが「カレントディレクトリ」、
つまり「今いるフォルダ」の場所
カレントディレクトリ (一覧表示)
今いるフォルダのファイルやフォルダを一覧表示するには
macなら「ls」、winなら「dir」を入力
カレントディレクトリにある
ファイルやフォルダの一覧が出る
例えば右の例だと、/Users/zoi/Applications、
/Users/zoi/Desktop、/Users/zoi/Documents、...
がある
カレントディレクトリ (移動)
「cd 行き先」を実行すると移動できる
(mac、winどちらでも)
この場合、
「/Users/zoi」から「Desktop」に移動したので
最終的に「/Users/zoi/Desktop」にいる
特殊パス
じゃあ「一つ前」には戻れないの
...? → 戻れます
全部のフォルダの中には「
..」という名前のフォルダがあることになってて、
そこへ移動すると「一つ前」のフォルダに移動することになる
例えば、/Users/zoi にいるときにcd .. と打てば、/Usersへ移動できる!
絶対パス
他にも、/Usersにいるときに、cd zoi/Desktop と打つことで、
/Users/zoi/Desktopまで一気に移動できる
また、cd / と打てば、一気に/ まで移動できる
つまり、どこにいてもcd /Users/zoi と打てば/Users/zoiにいけるということ
(あと、「/Users/ユーザー名」はよく使うので、「
~」に省略できる「cd ~/Desktop」でOK)
ここまでのまとめ
カレントディレクトリ(現在位置)確認 ...「pwd」(mac)、「cd」(win)
カレントディレクトリの内容確認… 「ls」(mac)、「dir」(win)
移動 … 「cd (移動先)」(mac、winともに)
一つ前へ移動… 「cd .. 」(「cd ../ 」でも全く同じ)
自分が使う方は覚えとくできればmac版も覚えといてほしい
他のコマンド
フォルダ移動以外の色々
echo “Hello World” 文字が表示される
cat test.txt ファイルの内容表示(winならcatじゃなくtype?)
touch aaa.txt 空ファイル作成(winではちょっとめんどくさいので略
)
色々できそう!
コマンドの構造
コマンドは基本的に単語がスペースで繋げられてできている
(1単語の場合もあるけど)
で、基本的には「1単語目がアプリ名、2単語目からはオプション」
例えば「echo “Hello World”」なら、
「echo」コマンド(文字を表示する)へ、表示内容として「”Hello World”」を渡している
つまり、文字が表示される
macとwinでコマンド違うの面倒くさい
それな〜〜〜〜
ちなみにプログラミング界隈では、ほとんどの人が
windows使ってるのにmac版コマンドが
よく話されてる→ なぜ...?
実は、昔のプログラマーたちは
LinuxというOSを使っていた(今でも使ってる人もいる)
その名残で今でもLinuxのコマンドがよく使われている
macはLinuxといとこぐらいの関係なので、コマンドが似ている(winは全く無関係)
※
ここからは、
“Linuxコマンド使いたい”
と思ってから見るのが
おすすめです
Linuxを使ってみる
というわけでLinuxを使ってもらいます
基本は変わらず、単語をスペースで区切って入力
→Enterだけです
といってもLinux入りのPCを持ってるはずがないので、オンラインの
Linuxを使います
(どういうこと?)
リモートデスクトップ
リモートデスクトップって知ってますか
?
キーボードやマウスの信号をネットで送り、計算はサーバーでして、
画面だけ返信してくれるやつです
今からやるのはそれです
ただし、コマンドラインの文字しか送りません
画面送ると回線やばいからね
...
SSH
文字だけverのリモートデスクトップは「
SSH」(Secure Shell)と呼ばれる
パスワード打ったりもするので、安全
(=Secure)なのが特徴
安全なので、認証のために鍵が必要だったりとめんどくさいけど
...
鍵の生成
鍵というより名札のイメージかも
?
コマンドラインで「ssh-keygen」と入力してEnter
途中でパスワードを設定しろ、と言って止まりますが、こだわりがなければそのまま
Enter
(パスワードなしになるけど、元々ある程度セキュリティ高いので
...もちろん設定してもOK)
多分パスワード再入力させられるので再入力
or設定してなければそのまま
Enter
公開鍵、秘密鍵
ユーザーフォルダ内の.sshフォルダ(winならC:Users〇〇.ssh、macなら/Users/〇〇/.ssh)
の中に「id_rsa」、「id_rsa.pub」のファイルがあるはず(他のファイルあるかもだけど無視
)
このうち、.pubの方が「公開鍵」(public)、無い方が「秘密鍵」
これは昔の天才が考えた暗号で、例えば
Aさんの公開鍵と秘密鍵があったとき、
Aの公開鍵を持っていれば暗号を作ることができ
(解読はできない)
秘密鍵を持っていれば暗号を解読できる、というふうな特徴がある
つまり、公開鍵を渡せば、相手は暗号化して送ってくれるけど、自分にしか読めない
→ 便利!
公開鍵、秘密鍵
つまり、公開鍵(.pub)の方はネットにあげても全然大丈夫
(量子コンピューター出るまでは
)
秘密鍵は何があっても自分の
PCから出すべきではない
SSH
話を戻します
SSHの鍵ができたので、Discordなどでサーバー持ってる人
(ZOI)に
「これが僕の公開鍵だからアカウント作って」と声をかけてください
設定するので、終わったらアドレスとユーザー名を教えます
「ssh ユーザー名@アドレス」を実行すると、リモートの
Linuxにつながります
(ssh zoi@xxx.xxx.xxx.xxx みたいな感じになる)
SSH
テロ行為以外は大体何してもいいです。法に触れるのはやめてね。
ただし、データの保証はしません
(大事なデータや個人情報置かないで
)
リモートから抜ける時は「exit」を入れてください
(ログアウトコマンドです。共用
PCなのでシャットダウンしないでね
)
リモートにいる間は、コマンド入れる左側のところの表示が少し変わるかもです
WSL
macとLinuxはコマンドがかなり共通なのですが、
Windowsだけ全然違います...
さっきのSSHを使ってリモートに繋げるのでもいいんですが、
オフラインの時とか性能が必要な時とか困ります
なので、「Windowsの中にLinuxを入れる」という方法で無理やり解決します
(仮想OSとか呼ばれるやつですね)
macの人は飛ばして次に進んでください
WSL
まずはMicrosoft Storeというアプリを開き、Ubuntuと検索してください
Linuxは似たOSたちの総称で、Ubuntuはその中で有名なやつです。
Ubuntu 22.04とかいう名前だと思います、インストールしましょう
(めっちゃダウンロードするので家でやってね
)
WSL
それが終わったら、コマンドプロンプトを開き「
wsl」と入力してください
もしかしたらユーザー名やパスワードを決めさせられるかもですが、
指示通りに、好きなように入力してください
パスワードは入力してるように見えないですが裏で入ってます
気にせず入れましょう
それが終われば、そこはLinuxの世界です。おめでとうございます
!
SSHの時と同じくexitでコマンドプロンプトにもどり
(ウィンドウ消してもOK)、
もう一度wslで再度Linuxに入ることができます
パッケージマネージャ
コンソールに慣れてくると、アプリを入れるために
Googleでアプリ名を検索して、
ダウンロードしてクリックして開く、というのがめんどくさくなってきます
また、その方法ではアップデートに気づかなかったりするかもです
なので、コマンドラインだけでアプリを入れられる「パッケージマネージャ」というものが
あります
パッケージマネージャ
Windowsなら、wingetというコマンドです
(設定しないと使えないかも
...)
winget search chrome で、「chrome」と名前が似ているアプリの一覧が出ます
winget install chrome で、そのアプリを入れることができます
winget update でアプリ一覧の更新、winget upgradeでアプリの更新、
winget list で入れたものの一覧を表示です
パッケージマネージャ
macなら、homebrewというコマンドです
https://brew.sh/index_ja に書いてあるコマンドをコピペで実行すれば使えます
(危険だからコピペ実行はあんまりよくないけどね
...w)
brew search chrome で、「chrome」と名前が似ているアプリの一覧が出ます
brew install chrome で、そのアプリを入れることができます
brew update でアプリ一覧の更新、brew upgradeでアプリの更新、
brew list で入れたものの一覧を表示です
パッケージマネージャ
Linuxには色々ありますが、Ubuntuならaptというコマンドです(apt-getのこともある、同じ)
ただし、SSHのサーバーは管理者権限与えてないので
sudo使えません...
入れたいアプリあれば教えてください
sudo apt search chrome で、「chrome」と名前が似ているアプリの一覧が出ます
sudo apt install chrome で、そのアプリを入れることができます
sudo apt update でアプリ一覧の更新、sudo apt upgradeでアプリの更新、
sudo apt list --installed で入れたものの一覧を表示です
シェル
皆さんが使っているこれ、コマンドを打つ前のところに
$マークがあるのわかりますか
?
なので、Webページでは「$ brew list」みたいに
$マーク付きで書いてることもありますが、
実際に入れるのは「brew〜」の部分だけです
シェル
ちなみに、この$を表示しているのも一つのアプリです
コマンドを入力されたら実行するというアプリです
macはzsh、Linuxはbashがデフォルトになってます
カスタムするとかっこよくなったりおしゃれになったりして面白いです
詳しくは→ お前らのターミナルはダサい- Qiita (少し難しいです)
まとめ
今は何に使うかよくわからんと思いますが、少しすれば何するにも使うことになります
コードを書くのは今まで通りエディタですが、その前後の処理に使ったりします
使っているうちにゆったり慣れていけばいいので、現時点ではなんかかっこいい〜くらいでいいです
とりあえず、黒い画面出てきて「とりあえず読んでみるか
?」と思うなら十分です
終わり

More Related Content

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

プログラミング講座 #4 ターミナルを使いこなす