参考
- TryHackMe | Linux Fundamentals Part 1
- TryHackMe | Linux Fundamentals Part 2
- TryHackMe | Intro to Digital Forensics
一覧
超基本コマンド
ls | 現在のフォルダ(ディレクトリ)内にあるファイル一覧を表示 |
---|---|
mkdir | 新しいディレクトリの作成 |
cd | カレントディレクトリ(作業フォルダ)を変更 |
pwd | 現在のディレクトリを表示 |
rm | ファイルの削除 |
mv | ファイルを移動 |
echo | 変数などの表示 |
cat | ファイルの中身を表示 |
file | ファイルの種類を表示 |
touch | ファイル作成 |
find | ファイルの存在を確認 |
やや発展的なコマンド
pdfinfo | pdfの情報を表示 |
---|---|
exifinfo | 写真の位置情報などを表示 |
基本コマンドの使用例
ls
カレントディレクトリにあるファイル一覧を表示します。
~/sample$ ls
sample.txt
-aを付けると、隠しファイル(先頭がピリオドのファイル)も表示されます。
~/sample$ ls -a . .. sample.txt
mkdir
新しいディレクトリを作成
~/sample$ mkdir newfolder ~/sample$ ls newfolder sample.txt
cd
カレントディレクトリ(作業フォルダ)を変更
# 現在のディレクトリにディレクトリ「myfolder」があることを確認 ~/sample$ ls newfolder sample.txt # myfolderをカレントディレクトリにする ~/sample$ cd myfolder ~/sample/myfolder$
cd ../とすれば1つ上のディレクトリに戻れる。
~/sample/myfolder$ cd ../
~/sample$
pwd
現在のディレクトリを表示できる。
~/sample$ pwd
/home/user_name/sample
rm
ファイルの削除ができる
# 現在のディレクトリの中身を確認 ~/sample$ ls newfolder sample.txt # test.txtを削除 ~/sample$ rm sample.txt ~/sample$ ls newfolder
-Rをつけることでディレクトリを削除
# 現在のディレクトリの中身を確認 ~/sample$ ls newfolder sample.txt # newfolderを削除 ~/sample$ rm -R newfolder ~/sample$ ls sample.txt
mv
ファイルの移動ができる
# 現在のディレクトリの中身を確認 ~/sample$ ls newfolder sample.txt # sample.txtをnewfolder内に移動 ~/sample$ mv sample.txt newfolder # newfolder内に移動したことを確認 ~/sample$ ls newfolder ~/sample$ ls newfolder sample.txt
同じフォルダ内に移動によりファイル名の変更も可能。
# 現在のディレクトリの中身を確認 ~/sample$ ls newfolder sample.txt # 「sample.txt」のファイル名を「hoge.txt」に変更 ~/sample$ mv sample.txt hoge.txt ~/sample$ ls hoge.txt newfolder
echo & cat
- echo:変数などをコマンドに表示
- cat:ファイルの中身を表示
echoを使えば、コマンド上に文字を出力できる。
~/sample$ echo "hello world" hello world
さらに、echoで
- 「>」を使用:ファイルに上書きで書き込み
- 「>>」を使用:ファイルに追加で書き込み
が可能で、ファイルの中身はcatで表示できる。
# sample.txtに上書きで書き込み ~/sample$ echo "hello world" > sample.txt ~/sample$ cat sample.txt hello world # sample.txtに追加で書き込み ~/sample$ echo "thank you" >> sample.txt ~/sample$ cat sample.txt hello world thank you # 再度sample.txtに上書きで書き込み(hello worldとthank youは消えている) ~/sample$ echo "bye" > sample.txt ~/sample$ cat sample.txt bye
file
ファイルの種類を表示できる。
# 現在のディレクトリの中身を確認 ~/sample$ ls equipment01.jpg newfolder pamph_ndl.pdf sample.txt # sample.txtのファイルの種類を表示 ~/sample$ file sample.txt sample.txt: ASCII text # newfolderのファイルの種類を表示 ~/sample$ file newfolder newfolder: directory # pamph_ndl.pdfのファイルの種類を表示 ~/sample$ file pamph_ndl.pdf pamph_ndl.pdf: PDF document, version 1.5 # equipment01.jpgのファイルの種類を表示 ~/sample$ file equipment01.jpg equipment01.jpg: JPEG image data, ~(略)
touch
空のファイルを作成
# 現在のディレクトリの中身を確認 ~/sample$ ls newfolder sample.txt # sample2.txtを作成 ~/sample$ touch sample2.txt # 現在のディレクトリの中身を再確認 ~/sample$ file newfolder newfolder sample2.txt sample.txt # sample2.txtのファイルの種類がempyであることを確認 file sample2.txt sample2.txt: empty
find
ファイル・ディレクトリが存在するかを確認
# 現在のディレクトリの中身を確認 ~/sample$ ls newfolder sample1.txt sample2.txt # ファイルがある場合の処理 ~/sample$ find sample1.txt sample1.txt # フォルダがある場合の処理 ~/sample$ find newfolder newfolder # ファイルがない場合の処理 ~/sample$ find sample3.txt find: ‘sample3.txt’: No such file or directory
「*」(ワイルドカード)を使うことで指定した拡張子のファイルが存在するかなどを調べることも可能。
# 拡張子txtのファイルの存在を確認 ~/sample$ find *.txt sample1.txt sample2.txt # ファイル名が「~2.txt」のファイルを検索 ~/sample$ find *2.txt sample2.txt