0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CODEPREP「はじめての Linuxコマンド」に登場するコマンド

Posted at

備忘録として、CODEPREP「はじめての Linuxコマンド」に登場するコマンドをまとめてみました。

pwd

「Print Working Directory」を表し、現在作業しているディレクトリの絶対パス(ルートから始まるパス)を出力する。

cmd
> pwd
/root/src

whoami

現在使用しているユーザーアカウントを表示する。

cmd
> whoami
root

ls

現在の作業ディレクトリにある全てのディレクトリとファイルを表示する。

cmd
> ls

-aオプション

隠れファイルを含むすべてのファイルを一覧を表示する。

cmd
> touch message.txt
> touch .hidden_file
> ls
message.txt
> ls -a
.
..
.hiddeb_file
message.txt

-lオプション

ファイルやディレクトリの情報をより詳細に表示する。

複数オプション

短くまとめて実行できる。

cmd
> ls -la // -l -a をまとめて実行

mkdir

ディレクトリを作成する。
引数としてディレクトリ名を取る。

cmd
> mkdir documents
> ls
documents

-pオプション

ネストしたディレクトリを1コマンドで作成する。
存在しないディレクトリがあった場合には、それも作成してくれるようになる。

cmd
> mkdir -p photos/vacation/italy
> ls -R .
.:
photos
./photos:
vacation
./photos/vacation/
italy
./photos/vacation/italy:

cd

ワーキングディレクトリを変更する。
異動先のディレクトリ名を引数に渡す。

cmd
> cd documents

全てのディレクトリには以下の2つの特殊なディレクトリが存在する。

.  - 現在のディレクトリ
.. - 親ディレクトリ

echo

コンソール上にテキストを表示する。

cmd
> echo "Hello World!"
Hello World!

>演算子(作成)

初期内容を含むファイルを作成する。
>演算子を用いてリダイレクトすることで、その出力をファイルに書き込むことができる。

cmd
> echo "this is a file" > message.txt
> cat message.txt
this is a file

>演算子(上書き)

ファイルを上書きする。
>演算子を用いて既に存在するファイルに出力をリダイレクトする場合、そのファイルは新しい出力内容で上書きされる。

cmd
> echo "this was overwritten" > message.txt
> cat message.txt
this was overwritten

>>演算子

ファイルを追記する。

cmd
> echo "this text was appended" >> message.txt
> cat message.txt
this was overwritten
this text was appended

touch

空のファイルを作成する。

cmd
> touch message.txt
> ls
message.txt

cat

ファイルの中身を出力する。

cmd
> cat message.txt
this is a file

cp

ファイルをコピーする。
コピー元、コピー先、2つの引数を取る。

cmd
> copy message.txt message_copy.txt
> ls
message.txt
message_copy.txt

mv

リネームする。

mv OLD_FILE_NAME NEW_FILE_NAME
cmd
> mv message.txt renamed_message.txt
> ls
message_copy.txt
renamed_message.txt

rm

ファイルを削除する。

cmd
> rm renamed_message.txt
> ls
message_copy.txt

-rオプション

ディレクトリを削除する。
ディレクトリを削除したい場合、-rオプションをコマンドにつける必要がある。

cmd
> ls
documents
> rm -r documents
> ls

--helpオプション

コマンドの説明を表示する。

find

ファイルを探す。

find LOCATION -iname NAME_TO_FIND

現在のディレクトリ(.)を指定し、-inameオプションを使用してファイル名を指定。

cmd
> find . -iname needle.txt
./e/haystack_1/haystack_2/needle.txt

ワイルドカード(*)

ワイルドカード(*)を使用することで、任意の文字列にマッチさせることができる。

cmd
> find . -iname needle*
./e/haystack_1/haystack_2/needle.txt
./e/haystack_1/needle.png
./e/haystack_1/haystack_2/needle.gif
./e/haystack_1/haystack_2/haystack_3/needle.jpg

grep

文字列を検索する。

grep PATTERN FILE

PATTERNには調べたい文字列をFILEには検索したいファイル名を指定。

cmd
> grep dear sonnet.txt
And with old woes new wail my dear time's waste:
But if the while I think on thee, dear friend,

-nオプション

行番号を出力する。

cmd
> grep -n dear sonnet.txt
4:And with old woes new wail my dear time's waste:
13:But if the while I think on thee, dear friend,

-rオプション

指定したディレクトリ配下を再帰的に検索する。

-iオプション

大文字小文字を区別しない。

wc

word count。単語数、バイト数、行数、文字数その他いくつかの数え上げができる。

wc FILE

何もオプションをつけずにwcを実行すると以下の3つの数値が出力される。

  • 行数
  • 単語数
  • 文字数
cmd
> wc sonnet.txt
       13       116       610 sonnet.txt

-wオプション

単語数だけを出力。

cmd
> wc -w sonnet.txt
116 sonnet.txt

du

disk usage。ファイルやディレクトリの実容量を表示する。

do FILE

オプション無しのduはKB単位のバイト数とファイル名を出力する。

cmd
> du sonnet.txt
4 sonnet.txt

ディレクトリでも実行できる。

cmd
> du .
4  ./f/haystack_1/haystack_2/haystack_3
8  ./f/haystack_1/haystack_2
12 ./f/haystack_1
16 ./f

-sオプション

ディレクトリ単位のサイズを出力する。

-hオプション

ファイルサイズに適切な単位をつけて表示する。

df

それぞれのディスクの使用量、未使用量を表示する。

cmd
Filesystem                Size      Used Available Use% Mounted on

overlay                  14.5G     12.1G      2.4G  84% /
tmpfs                   495.4M         0    495.4M   0% /dev
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?