Linuxのディレクトリ、基本コマンド

1.ディレクトリ構成

Linuxのディレクトリ構成は、「Filesystem Hierarchy Standard」というもので定められています。
以下のようなディレクトリが存在します。(一部抜粋)

ディレクトリ名説明
/bin基本コマンドの実行ファイル(ls, cd, catなど)
/sbinシステム管理の実行ファイル(fdisk, mkfs, sysctlなど)
/etcシステムに関する設定ファイル
/tmp一時的な置き場(起動時に削除)
/optオプションのアプリケーション
/usr/binアプリケーション(apt, ncat, nmapなど)
/usr/share共有データ

2.基本コマンド(man)

manコマンドは、コマンドの概要、説明を表示できるコマンドです。
コマンドの使い方やオプションを知りたい場合に役立つコマンドです。

kali@kali:~$ man man

NAME
       man - an interface to the system reference manuals

SYNOPSIS
       man [man options] [[section] page ...] ...
       man -k [apropos options] regexp ...
       man -K [man options] [section] term ...
       man -f [whatis options] page ...
       man -l [man options] file ...
       man -w|-W [man options] page ...

DESCRIPTION
       man  is  the system's manual pager.  Each page argument given to man is normally the name of a program, utility or function.  The
       manual page associated with each of these arguments is then found and displayed.  A section, if provided, will direct man to look
       only in that section of the manual.  The default action is to search in all of the available sections following a pre-defined or‐
       der (see DEFAULTS), and to show only the first page found, even if page exists in several sections.

       The table below shows the section numbers of the manual followed by the types of pages they contain.

       1   Executable programs or shell commands
       2   System calls (functions provided by the kernel)
       3   Library calls (functions within program libraries)
       4   Special files (usually found in /dev)
       5   File formats and conventions, e.g. /etc/passwd
       6   Games
       7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
       8   System administration commands (usually only for root)
       9   Kernel routines [Non standard]

       A manual page consists of several sections.

       Conventional section names include NAME, SYNOPSIS, CONFIGURATION, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUE, ERRORS,  ENVI‐

3.基本コマンド(ls)

lsコマンドは、存在するファイルを表示させるコマンドです。
オプション追加することで、表示形式の変更をさせることができます。
「a」オプションは隠しファイルを表示
「1」オプションは1行ずつ表示
「l」オプションはファイルの詳細を表示

kali@kali:/$ ls
bin dev home initrd.img.old lib32 libx32 media opt root sbin sys usr vmlinuz
boot etc initrd.img lib lib64 lost+found mnt proc run srv tmp var vmlinuz.old
kali@kali:/$

kali@kali:/$ ls -a
. bin .cache etc initrd.img lib lib64 lost+found mnt proc run srv tmp var vmlinuz.old
.. boot dev home initrd.img.old lib32 libx32 media opt root sbin sys usr vmlinuz
kali@kali:/$

kali@kali:/$ ls -1
bin
boot
dev
etc
home
initrd.img
initrd.img.old
lib
lib32
lib64
libx32
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
vmlinuz
vmlinuz.old
kali@kali:/$

kali@kali:/$ ls -l
total 68
lrwxrwxrwx 1 root root 7 Jul 27 13:09 bin -> usr/bin
drwxr-xr-x 3 root root 4096 Sep 19 19:48 boot
drwxr-xr-x 17 root root 3320 Sep 20 22:48 dev
drwxr-xr-x 158 root root 12288 Oct 4 02:33 etc
drwxr-xr-x 3 root root 4096 Jul 27 13:27 home
lrwxrwxrwx 1 root root 33 Sep 19 19:44 initrd.img -> boot/initrd.img-5.8.0-kali1-amd64
lrwxrwxrwx 1 root root 33 Jul 27 13:10 initrd.img.old -> boot/initrd.img-5.7.0-kali1-amd64
lrwxrwxrwx 1 root root 7 Jul 27 13:09 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Jul 27 13:09 lib32 -> usr/lib32
lrwxrwxrwx 1 root root 9 Jul 27 13:09 lib64 -> usr/lib64
lrwxrwxrwx 1 root root 10 Jul 27 13:09 libx32 -> usr/libx32
drwx------ 2 root root 16384 Jul 27 13:09 lost+found
drwxr-xr-x 3 root root 4096 Jul 27 13:09 media
drwxr-xr-x 2 root root 4096 Jul 27 13:09 mnt
drwxr-xr-x 5 root root 4096 Sep 21 02:21 opt
dr-xr-xr-x 248 root root 0 Sep 20 22:48 proc
drwx------ 6 root root 4096 Sep 21 09:30 root
drwxr-xr-x 34 root root 820 Sep 21 00:02 run
lrwxrwxrwx 1 root root 8 Jul 27 13:09 sbin -> usr/sbin
drwxr-xr-x 3 root root 4096 Jul 27 13:22 srv
dr-xr-xr-x 13 root root 0 Sep 20 22:48 sys
drwxrwxrwt 27 root root 4096 Oct 4 02:39 tmp
drwxr-xr-x 14 root root 4096 Jul 27 13:11 usr
drwxr-xr-x 12 root root 4096 Jul 27 13:11 var
lrwxrwxrwx 1 root root 30 Sep 19 19:44 vmlinuz -> boot/vmlinuz-5.8.0-kali1-amd64
lrwxrwxrwx 1 root root 30 Jul 27 13:10 vmlinuz.old -> boot/vmlinuz-5.7.0-kali1-amd64
kali@kali:/$

4.基本コマンド(locate,find)

locate,findコマンドはファイルの検索ができるコマンドです。
「locate」コマンドは「locate.db」というデータベースから検索します。「locate.db」は定期的に更新されていますが、「updatedb」コマンドで更新することができます。

「find」コマンドは検索を開始するディレクトリを指定できます。また、正規表現でファイル名を検索することができます 。

kali@kali:~/test$ sudo locate test.log
/home/kali/test/test.log

kali@kali:~/test$ sudo find / -name  test.log
/home/kali/test/test.log
kali@kali:~/test$ 

5.基本コマンド(cd,pwd)

cdコマンドはディレクトリを移動できるコマンドです。pwdコマンドは現在のディレクトリを表示できます。

kali@kali:~$ pwd
/home/kali

kali@kali:~$ cd test/
kali@kali:~/test$ pwd
/home/kali/test

kali@kali:~/test$ cd ..
kali@kali:~$ pwd
/home/kali

6.基本コマンド(mkdir,rm)

mkdirコマンドはディレクトリを作成するコマンドです。「p」オプションで親ディレクトリの作成もできます。
rmコマンドはファイルの削除コマンドです。「rf」コマンドでディレクトリの削除ができます。

kali@kali:~/test$ ls
test.log
kali@kali:~/test$ mkdir test1
kali@kali:~/test$ ls
test1  test.log

kali@kali:~/test$ mkdir -p test2/test2
kali@kali:~/test$ ls
test1  test2  test.log
kali@kali:~/test$ cd test2
kali@kali:~/test/test2$ ls
test2
kali@kali:~/test$ ls
test1  test2  test.log
kali@kali:~/test$ rm test.log
kali@kali:~/test$ ls
test1  test2
kali@kali:~/test$ rm -rf test2
kali@kali:~/test$ ls
test1

7.基本コマンド(ss)

ssコマンドはソケットの状態を表示するコマンドです。
「n」オプションは、ポート番号で表示(サービスの名前解決をしない)
「t」オプションは、tcpの状態を表示
「l」オプションは、接続待ちのソケットのみ表示
「p」オプションは、プロセスも表示

kali@kali:~$ sudo ss -ntlp
State       Recv-Q      Send-Q                 Local Address:Port              Peer Address:Port      Process                               
LISTEN      0           50                [::ffff:127.0.0.1]:40297                        *:*          users:(("java",pid=1949,fd=35))      
LISTEN      0           50                [::ffff:127.0.0.1]:8080                         *:*          users:(("java",pid=1949,fd=38))      
kali@kali:~$