Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

UNIX(Linux)
Beginner

  1. What is UNIX (Linux)? - Japanese Only
  2. What are Distributions? - Japanese Only
  3. About CentOS, macOS, and CUI - Japanese Only
  4. Installing CentOS with Virtualization Software and macOS CUI Environment - Japanese Only
  5. Login, root User, and Shutdown - Japanese Only
  6. Command Structure and Important Notes - Japanese Only
  7. File System and Navigating Directories (cd and pwd) - Japanese Only
  8. Key Directories and Listing Contents (ls) - Japanese Only
  9. Advanced Directory Navigation (cd) and Paths - Japanese Only
  10. Advanced ls Command (Part 1) and Dotfiles - Japanese Only
  11. Advanced ls Command (Part 2) - Japanese Only
  12. Tab Completion for Input - Japanese Only
  13. Creating Directories (mkdir) - Japanese Only
  14. Quick File Creation (touch) and About Files - Japanese Only
  15. Standard I/O, Redirection (> and >>), echo, and cat - Japanese Only
  1. Deleting Files and Directories (rm) - Japanese Only
  2. Copying Files and Directories (cp) - Japanese Only
  3. Moving and Renaming Files and Directories (mv) - Japanese Only
  4. What is the vi Editor? - Japanese Only
  5. Basic vi Usage (Part 1) - Japanese Only
  6. Basic vi Usage (Part 2) - Japanese Only
  7. Remote Operations via Terminal (SSH and Tera Term) - Japanese Only
  8. Installing Software (yum and Package Managers) - Japanese Only
  9. User Overview, Verification, Creation, and Switching (adduser and su) - Japanese Only
  10. User Passwords, Deletion, Modification, and sudo (passwd, userdel, usermod) - Japanese Only
  11. About Groups and the groups Command - Japanese Only
  12. Adding, Deleting, and Modifying Groups (groupadd, groupdel, groupmod) - Japanese Only
  13. About Permissions - Japanese Only
  14. Setting Permissions (chmod and chown) - Japanese Only
  1. Home
  2. UNIX(Linux)Beginner - Deleting Files and Directories (rm)

Deleting Files and Directories (rm) - Japanese Only

みなさまどうもおはこんばんちわわでございます。

続きましてファイルとかディレクトリの削除方法についてやっていきましょう。

ファイルとかディレクトリを削除するには『rm』コマンドくんを使用します。では使い方について色々とやっていきましょう。

まずファイルの削除方法からですが適当にtest.txtってファイルを『touch』コマンドくんで作ってみます。

[root@localhost ~]# touch test.txt
[root@localhost ~]# ls
test.txt

そしたら『rm test.txt』って入力します。

[root@localhost ~]# rm test.txt

この状態でエンターキーを叩くと以下のように

[root@localhost ~]# rm test.txt
rm: remove regular empty file `test.txt'?

「ファイル削除していいの?」って聞かれますので「y」か「Y」と入力してエンターキーを押してあげて下さい。

[root@localhost ~]# rm test.txt
rm: remove regular empty file `test.txt'? y

そうすると通常のプロンプトが表示され入力待ち状態になります。これでちゃんと削除は出来てます。『ls』コマンドで確認してみると

[root@localhost ~]# rm test.txt
rm: remove regular empty file `test.txt'? y
[root@localhost ~]# ls

ちゃんと『test.txt』は消えてますね。基本的な使い方はこんな感じになります。

ちなみに「ファイル削除していいの?」っていう返答についてなんですがこれが若干OSによって違いがあったりします。CentOSの場合は「最初の文字が「y」または「Y」だった場合に「yes」と判定する」って感じになるので以下のように

[root@localhost ~]# touch test.txt
[root@localhost ~]# rm test.txt
rm: remove regular empty file `test.txt'? y初音ミク!
[root@localhost ~]# ls

「y初音ミク!」なんてへんてこりんな記述をしてもOKだったりします。

逆に「削除したくないぜー」なんてときには最初の文字が「y」または「Y」以外にしてしまえばOKです。なので何も入力しない状態でエンターキーを押してしまうのが楽ちんかもですね。

続いてファイルではなくディレクトリの削除方法についてです。まず『mkdir』くんで適当に『test』ディレクトリを作ってあげて

[root@localhost ~]# mkdir test

それを『rm』コマンドで削除しようとするとこうなります。

[root@localhost ~]# rm test
rm: cannot remove `test': is a directory

「testはディレクトリで削除できないよー」って怒られちゃいましたね。こんな感じで何もオプションを付けない『rm』コマンドだとディスプレイの削除をすることは出来ません。じゃあどうするのかというと『-r』オプションを付けてあげます。こんな感じですね。

[root@localhost ~]# rm -r test

そうすると先程のフォルダ削除時と同じように「削除していいっすかー?」って聞かれるので「y」とかを入力してあげればOKです。

[root@localhost ~]# rm -r test
rm: remove directory `test'? y

こんな感じですね。『rm』コマンドの使い方はそれほど難しくないかと思います。

続いてちょいと便利なオプションをいくつか紹介しておきます。まず削除するときって「削除していいっすかー?」ってご丁寧に確認してくれるわけなんですがその確認が邪魔っていうちょっと切ない気分になっちゃうこともあるかと思います。

例えばディスプレイを適当に作って更にその中にファイルを適当に3つくらい作って

[root@localhost ~]# mkdir test
[root@localhost ~]# touch test/test1.txt
[root@localhost ~]# touch test/test2.txt
[root@localhost ~]# touch test/test3.txt

削除しようとするとこうなります。

[root@localhost ~]# mkdir test
[root@localhost ~]# touch test/test1.txt
[root@localhost ~]# touch test/test2.txt
[root@localhost ~]# touch test/test3.txt
[root@localhost ~]# rm -r test/
rm: descend into directory `test'? y
rm: remove regular empty file `test/test3.txt'? y
rm: remove regular empty file `test/test1.txt'? y
rm: remove regular empty file `test/test2.txt'? y
rm: remove directory `test'? y

毎回確認されちゃうのでちょっと大変ですね。ファイルが100000個とかあったらそれだけで1日が終わってしまいますしファイルが10000000000000000000個とかあったらおじいさんおばあさんになってしまいますね。

そんな時には『-f』オプションを付けてみましょう。そうするとこんな感じで

[root@localhost ~]# mkdir test
[root@localhost ~]# touch test/test1.txt
[root@localhost ~]# touch test/test2.txt
[root@localhost ~]# touch test/test3.txt
[root@localhost ~]# rm -rf test/
[root@localhost ~]# ls

確認されずにそのままさくっと削除してくれますのでとっても便利です。

続いて削除した内容をどっかに保存しておきたい、という場合です。その場合は『-v』を付けてあげましょう。そうするとこんな感じで

[root@localhost ~]# mkdir test
[root@localhost ~]# touch test/test1.txt
[root@localhost ~]# touch test/test2.txt
[root@localhost ~]# touch test/test3.txt
[root@localhost ~]# rm -rfv test
removed `test/test3.txt'
removed `test/test1.txt'
removed `test/test2.txt'
removed directory: `test'

削除したファイルとかディレクトリを標準出力で教えてくれます。

そしてお察しの良い方はもう予想がついてるとは思いますが「出力先は標準出力」って事なので前回使用した『>』とかを使えば削除した情報をそのままテキストとしてファイル保存する事が可能です。こんな感じですね。

[root@localhost ~]# mkdir test
[root@localhost ~]# touch test/test1.txt
[root@localhost ~]# touch test/test2.txt
[root@localhost ~]# touch test/test3.txt
[root@localhost ~]# rm -rfv test > remove_list.txt
[root@localhost ~]# cat remove_list.txt
removed `test/test3.txt'
removed `test/test1.txt'
removed `test/test2.txt'
removed directory: `test'

これもかなり便利なのでぜひ使ってみて下さい。

そして超重要な注意点なのですが『rm』コマンドくんで削除したファイルは完全に闇に葬られるため復旧することは出来ません

WindowOSとかMacOSだとファイルを削除したとしても実質ゴミ箱に移動するだけでゴミ箱から復旧させる事ができますよね。『rm』コマンドくんだとそうはいかないのでここ間違えないよう注意です。もし重要なファイルだった場合は『cp』コマンドくんとかでバックアップを作って置くと良いかもです。『cp』コマンドくんについては次の記事で解説しますね。

前にも解説しましたがディレクトリを削除するコマンドで『rmdir』という子が居ます。

この子もディレクトリを削除できるコマンドなのですが「空のディレクトリしか削除出来ない」という制約があるのであまり使い物になりません。多分覚えなくてOKかなと思います。

というわけで以上になりますね。続いての記事ではファイルとディレクトリのコピーについてやっていきましょう。

ではでは失礼致します。

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .