ファイルとディレクトリの操作
このステップでは、Linux でファイルとディレクトリを管理するための重要なコマンドを学びます。
ディレクトリの作成と移動
mkdir
コマンドは新しいディレクトリを作成し、cd
コマンドはディレクトリを変更することができます。
- 演習用の新しいディレクトリを作成しましょう。
cd ~/project
mkdir linux_practice
- 新しいディレクトリに移動します。
cd linux_practice
- 正しいディレクトリにいることを確認します。
pwd
以下のように表示されるはずです。
/home/labex/project/linux_practice
ファイルの作成と表示
次に、このディレクトリ内のいくつかのファイルを操作しましょう。
echo
コマンドと出力リダイレクトを使用して、簡単なテキストファイルを作成します。
echo "Linux is a powerful operating system." > about_linux.txt
- 追記リダイレクト演算子 (
>>
) を使用して、ファイルに別の行を追加しましょう。
echo "It is open-source and widely used in servers and embedded systems." >> about_linux.txt
cat
コマンドを使用して、ファイルの内容を表示します。
cat about_linux.txt
以下のように表示されるはずです。
Linux is a powerful operating system.
It is open-source and widely used in servers and embedded systems.
ファイルのコピーと移動
cp
コマンドはファイルをコピーし、mv
コマンドはファイルを移動または名前を変更します。
- ファイルのコピーを作成しましょう。
cp about_linux.txt linux_copy.txt
- 両方のファイルが存在することを確認します。
ls
以下のように表示されるはずです。
about_linux.txt linux_copy.txt
- コピーしたファイルの名前を変更しましょう。
mv linux_copy.txt linux_benefits.txt
- 名前を変更したファイルの内容を変更します。
echo "Benefits of Linux include security, stability, and flexibility." > linux_benefits.txt
- 両方のファイルの内容を確認します。
echo "Content of about_linux.txt:"
cat about_linux.txt
echo -e "\nContent of linux_benefits.txt:"
cat linux_benefits.txt
以下のように表示されるはずです。
Content of about_linux.txt:
Linux is a powerful operating system.
It is open-source and widely used in servers and embedded systems.
Content of linux_benefits.txt:
Benefits of Linux include security, stability, and flexibility.
- プロジェクトディレクトリに戻ります。
cd ~/project