Linux のコマンドラインからプログラムを実行する方法

Linux のコマンドラインからプログラムを実行する方法

ほとんどの Linux ディストリビューションにはグラフィカル ユーザー インターフェイスがあり、アプリケーション メニューのプログラムのアイコンをクリックすると開くことができます。ただし、場合によっては、ターミナルからプログラムを実行したい場合があります。ターミナルは、キーボード コマンドを使用してプログラムを実行したり、Linux システムを制御したりできる強力なツールです。 Linux システムの「ターミナル」ツールからプログラムを実行する方法を詳しく説明します。

方法1方法1/4:

ターミナルからプログラムを実行する

  1. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/a\/a2\/Run-a-Program-from-the-Command-Line-on-Linux-Step-1.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-1.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/a\/a2\/Run-a-Program-from-the-Command-Line-on-Linux-Step-1.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-1.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 1Ctrl + Alt + T同時に押してターミナルを開きます。ほとんどの Linux ディストリビューションでは、キーボード ショートカットCtrl + Alt + Tを使用してターミナルを開くことができます。 「アプリケーション」メニューの「ターミナル」アイコンをクリックすることもできます。通常、これは黒い画面に白いテキスト カーソルが表示されたようなアイコンです。
  2. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/f\/f1\/Run-a-Program-from-the-Command-Line-on-Linux-Step-2.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-2.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/f\/f1\/Run-a-Program-from-the-Command-Line-on-Linux-Step-2.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-2.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 2プログラム名を入力し、 Enter を押します。 Windows コンピュータのコマンド プロンプトとは異なり、Linux ではほとんどのプログラムを開くために完全なディレクトリ パスを入力したり、ディレクトリを変更したりする必要はありません。関連するプログラムがユーザーの「$PATH」環境変数に保存されている限り、Linux は自動的に「$PATH」内で検索して起動します。
    • たとえば、ターミナルから Firefox ブラウザを実行する場合は、 「firefox」と入力してEnter キーを押します。
    • プログラム名の後に-hまたは--helpと入力すると、プログラムのヘルプ メニューが開きます。多くのプログラムは、コマンド修飾子を使用して特定の方法で起動できます。たとえば、 firefox www.wikihow.comのように、Web ブラウザー名の後に URL を入力してEnter キーを押すと、Web ブラウザーで関連する Web サイトを直接起動できます。
    • プログラムを実行する権限がない、またはアクセスが拒否されたというメッセージが表示された場合は、プログラム名の前にsudoと入力してEnter キーを押します。 「sudo」コマンドを使用すると、一般ユーザーは「ターミナル」を呼び出して、管理者権限またはルート権限を必要とするコマンドを実行できます。
    • ターミナルから C または C++ プログラムを実行する場合は、ターミナルから起動する前にプログラムをコンパイルする必要があります。
    広告する
方法2方法2/4:

$PATH環境変数にないプログラムの実行

  1. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/8\/88\/Run-a-Program-from-the-Command-Line-on-Linux-Step-3.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-3.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/8\/88\/Run-a-Program-from-the-Command-Line-on-Linux-Step-3.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-3.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 1Ctrl + Alt + T同時に押してターミナルを開きます。ほとんどの Linux ディストリビューションでは、キーボード ショートカットCtrl + Alt + Tを使用してターミナルを開くことができます。 「アプリケーション」メニューの「ターミナル」アイコンをクリックすることもできます。アイコンは通常、白いテキスト カーソルが表示された黒い画面に似ています。
  2. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/9\/9e\/Run-a-Program-from-the-Command-Line-on-Linux-Step-4.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-4.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/9\/9e\/Run-a-Program-from-the-Command-Line-on-Linux-Step-4.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-4.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 2 cdと入力し、スペースを空けてから、プログラム ファイルの保存場所を入力します。プログラムの起動ファイルが $PATH 環境変数以外の場所に保存されている場合は、ターミナルでその場所に移動する必要があります。これは「cd」コマンドで実行できます。たとえば、Phython プログラムのフォルダーが Documents フォルダーに保存されている場合は、ターミナルでcd ~/Documents/Pythonまたは同様のコマンドを入力してEnter キーを押すと、そのフォルダーを見つけることができます。
  3. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/4\/45\/Run-a-Program-from-the-Command-Line-on-Linux-Step-5.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-5.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/4\/45\/Run-a-Program-from-the-Command-Line-on-Linux-Step-5.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-5.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 3 chmod a+x [filename]と入力し、 Enter を押します。 「ファイル名」をプログラムの実際の起動ファイルに置き換えます。 「chmod a+x」コマンドは、ファイルが実行可能ファイルであることを Linux システムに伝えます。 [1]
  4. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/a\/a2\/Run-a-Program-from-the-Command-Line-on-Linux-Step-6.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-6.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/a\/a2\/Run-a-Program-from-the-Command-Line-on-Linux-Step-6.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-6.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 4 「./」と開始ファイルの名前を入力し、 Enter を押します。これにより、開きたいプログラムが起動します。たとえば、Python ファイルの名前が「Helloworld.py」の場合、起動するには./helloworld.pyと入力します。 [2] 広告する
方法3方法3/4:

ディレクトリを「$PATH」環境変数に追加します

  1. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/3\/30\/Run-a-Program-from-the-Command-Line-on-Linux-Step-7.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-7.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/3\/30\/Run-a-Program-from-the-Command-Line-on-Linux-Step-7.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-7.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 1Ctrl + Alt + T同時に押してターミナルを開きます。プログラム名を入力してもプログラムを実行できない場合は、プログラムが配置されているディレクトリを $PATH 環境変数に追加する必要がある可能性があります。この操作は「ターミナル」で実行できます。まず、キーボードショートカットを使用してターミナルを開きます。
  2. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/5\/51\/Run-a-Program-from-the-Command-Line-on-Linux-Step-8.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-8.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/5\/51\/Run-a-Program-from-the-Command-Line-on-Linux-Step-8.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-8.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 2 コマンドを入力して、プログラム ディレクトリを "$PATH" 環境変数にエクスポートします。これを行うには、 export PATH=$PATH:[path/to/program]と入力し、 Enterを押します。コマンド内の「path/to/program」を、プログラムの実際のディレクトリ ツリー (保存されているパス) に置き換えます。この操作は現在のセッションが終了するまでのみ継続します。
    • たとえば、プログラムが「Home」フォルダの「bin」ディレクトリにインストールされている場合は、 export PATH=$PATH:$Home/binと入力してEnter キーを押します。これにより、「$Home/bin」ディレクトリが「$PATH」環境変数に一時的に追加されます。
    • echo $PATHコマンドを入力してEnterキーを押すと、現在「$PATH」環境変数に追加されているディレクトリが表示されます。
  3. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/3\/3b\/Run-a-Program-from-the-Command-Line-on-Linux-Step-9.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-9.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/3\/3b\/Run-a-Program-from-the-Command-Line-on-Linux-Step-9.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-9.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 3 nano ~/.bashrcと入力し、 Enter を押しますこれにより、ターミナルのテキスト エディターで .bashrc ファイルが開きます。このファイルを使用すると、$PATH 環境変数にディレクトリを永続的に追加できます。
  4. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/1\/16\/Run-a-Program-from-the-Command-Line-on-Linux-Step-10.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-10.jpg","bigUrl":"https:\/\/www.wikihow.com\/i mages\/thumb\/1\/16\/Run-a-Program-from-the-Command-Line-on-Linux-Step-10.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-10.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 4ファイルに「export PATH」コマンドを追加します。マウスでファイルの一番下までスクロールし、一番下にexport PATH=$PATH:[path/to/program]と入力します。 "[path/to/program]" を、プログラムが実際に存在するディレクトリ ツリーに置き換えます。 [3]
  5. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/9\/9b\/Run-a-Program-from-the-Command-Line-on-Linux-Step-11.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-11.jpg","bigUrl":"https:\/\/www.wikihow.com\/i mages\/thumb\/9\/9b\/Linux のコマンドラインからプログラムを実行する手順 11.jpg\/v4-728px-Linux のコマンドラインからプログラムを実行する手順 11.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":"<div class=\"mw-parser-output\"><\/div>"} 5 Ctrl + X同時に押してテキスト エディターを終了します。テキスト エディターは、ファイルを保存するかどうかを尋ねます。
  6. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/b\/b9\/Run-a-Program-from-the-Command-Line-on-Linux-Step-12.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-12.jpg","bigUrl":"https:\/\/www.wikihow.com\/i mages\/thumb\/b\/b9\/Run-a-Program-from-the-Command-Line-on-Linux-Step-12.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-12.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 6 Yキーを押してから、 Enterキーを押します。これにより、ファイルを保存することが確認され、テキスト エディターが終了し、ターミナルのコマンド ラインに戻ります。
  7. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/2\/25\/Run-a-Program-from-the-Command-Line-on-Linux-Step-13.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-13.jpg","bigUrl":"https:\/\/www.wikihow.com\/i mages\/thumb\/2\/25\/Run-a-Program-from-the-Command-Line-on-Linux-Step-13.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-13.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":"<div class=\"mw-parser-output\"><\/div>"} 7 source ~/.bashrcと入力し、 Enter を押しますこれにより、更新された「$PATH」環境変数が現在のセッションに読み込まれます。 [4]
  8. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/b\/b4\/Run-a-Program-from-the-Command-Line-on-Linux-Step-14.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-14.jpg","bigUrl":"https:\/\/www.wikihow.com\/i mages\/thumb\/b\/b4\/Run-a-Program-from-the-Command-Line-on-Linux-Step-14.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-14.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 8プログラム名を入力し、 Enterを押します。プログラムのディレクトリを「$PATH」環境変数に追加すると、プログラム名を入力してEnter キーを押すだけでプログラムを起動できるようになります。 広告する
方法 4方法4/4:

ターミナルでプログラムをインストールおよびアンインストールする

  1. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/c\/c6\/Run-a-Program-from-the-Command-Line-on-Linux-Step-15.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-15.jpg","bigUrl":"https:\/\/www.wikihow.com\/i mages\/thumb\/c\/c6\/Run-a-Program-from-the-Command-Line-on-Linux-Step-15.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-15.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":"<div class=\"mw-parser-output\"><\/div>"} 1Ctrl + Alt + T同時に押してターミナルを開きます。プログラムの起動に加えて、ターミナルからプログラムをインストールおよびアンインストールすることもできます。まず、キーボードショートカットを使用してターミナルを開きます。
  2. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/d\/d3\/Run-a-Program-from-the-Command-Line-on-Linux-Step-16.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-16.jpg","bigUrl":"https:\/\/www.wikihow.com\/i mages\/thumb\/d\/d3\/Run-a-Program-from-the-Command-Line-on-Linux-Step-16.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-16.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 2 sudo apt install [プログラム名]と入力し、 Enterを押します(Debianの一般的なオペレーティングシステム)。 "[プログラム名]" を、インストールするプログラムの実際の名前に置き換えます。このコマンドは、Ubuntu や Mint などの Debian ベースの Linux ディストリビューションで動作します。
  3. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/e\/ee\/Run-a-Program-from-the-Command-Line-on-Linux-Step-17.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-17.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/e\/ee\/Run-a-Program-from-the-Command-Line-on-Linux-Step-17.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-17.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 3 sudo apt remove [プログラム名]と入力し、 Enterを押します(Debianの一般的なオペレーティングシステム)。 "[プログラム名]" を、アンインストールするプログラムの実際の名前に置き換えます。このコマンドは、Ubuntu や Mint などの Debian ベースの Linux ディストリビューションで動作します。
  4. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/6\/60\/Run-a-Program-from-the-Command-Line-on-Linux-Step-18.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-18.jpg","bigUrl":"https:\/\/www.wikihow.com\/i mages\/thumb\/6\/60\/Run-a-Program-from-the-Command-Line-on-Linux-Step-18.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-18.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 4 sudo dnf install [プログラム名]と入力し、 Enterを押します(Red Hatシリーズ)。 "[プログラム名]" を、インストールするプログラムの実際の名前に置き換えます。このコマンドは、RHEL、Fedora、CentOS などの Red Hat Linux ディストリビューションに適用できます。
  5. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/5\/56\/Run-a-Program-from-the-Command-Line-on-Linux-Step-19.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-19.jpg","bigUrl":"https:\/\/www.wikihow.com\/i mages\/thumb\/5\/56\/Run-a-Program-from-the-Command-Line-on-Linux-Step-19.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-19.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 5 sudo dnf remove [アプリケーション名]と入力し、 Enter を押します (Red Hat シリーズ)。 )。 "[プログラム名]" を、アンインストールするプログラムの実際の名前に置き換えます。このコマンドは、RHEL、Fedora、CentOS などの Red Hat Linux ディストリビューションに適用できます。 [5]
  6. {"smallUrl":"https:\/\/www.wikihow.com\/images_en\/thumb\/a\/ac\/Run-a-Program-from-the-Command-Line-on-Linux-Step-20.jpg\/v4-460px-Run-a-Program-from-the-Command-Line-on-Linux-Step-20.jpg","bigUrl":"https:\/\/www.wikihow.com\/images\/thumb\/a\/ac\/Run-a-Program-from-the-Command-Line-on-Linux-Step-20.jpg\/v4-728px-Run-a-Program-from-the-Command-Line-on-Linux-Step-20.jpg","smallWidth":460,"smallHeight":345,"bigWidth":728,"bigHeight":546,"licensing":" class=\"mw-parser-output\"><\/div>"} 6プログラム名を入力し、 Enterを押します。プログラムがインストールされたら、その名前を入力してEnter キーを押すだけで起動します。 広告する

<<:  ドアベルハウジングの取り外し方法

>>:  Androidデバイスで実行中のアプリを表示する方法

推薦する

断食の準備方法

断食とは、一定期間、すべての食べ物と飲み物を断つことです。人々は、消化器系を浄化したり、体重を減らし...

良い彼氏になる方法

たとえ素晴らしい彼女がいたとしても、良い彼氏でいるのは必ずしも簡単ではありません。良い彼氏になるには...

AirPlayの設定方法

Apple の AirPlay 機能を使用すると、iOS モバイル デバイスから Apple TV、...

ISBN番号を取得する方法

キャラクターの作成方法、絵の構図、本の書き方に関する WikiHow ガイドをすべてようやく読み終え...

限界費用の求め方

経済学では、限界費用とは、生産量やその他のデータに基づいて計算される値であり、より多くの製品を生産す...

Excel でマクロを使用する方法

場合によっては、スプレッドシートで何らかのルールやパターンに基づいて繰り返しバッチ処理を実行する必要...

社会に貢献できる有用な人間になる方法

地球上のどこに住んでいても、あなたは社会の一員です。社会の一員として、誰もが他人を助け、私たちが住む...

コンピュータの表示言語を変更する方法

このwikiHowでは、コンピューターの表示言語を変更する方法を説明します。 Windows と M...

芝生の種から芝生を育てる方法

新しい芝生、または土がむき出しになっている小さな土地をお持ちですか?庭に芝生を植えると、家の自然の美...

他人と自分を比べるのをやめる方法

私たちは皆それぞれ異なっているので、自分を他人と比較するのは人間の本性であり、他人から学ぶことは批判...

声優になるには

声優は通常、アニメ映画やテレビ番組の吹き替え、ドキュメンタリーのナレーション、テレビやラジオのコマー...

パスタの作り方

夜の自習でいつもお腹が空いている大学生でも、キッチンで指揮を執る料理の専門家でも、パスタは満足させて...

魚の描き方

魚を描く方法はたくさんあります。以下に例を挙げます。 ステップ方法1方法1/2:リアルな魚 1図形を...

本物のラコステ ワニ柄ポロシャツと偽物の見分け方

ラコステのワニ柄ポロシャツは人気があり高価なため、よく模倣されます。ラコステのワニ柄ポロシャツを定価...

...