GNU/Linux >> Linux の 問題 >  >> Linux

Linux初心者向けの10の「rm」コマンド例

rm 削除」の略です ‘、名前が示すように、rmコマンドは、オペレーティングシステムのようなLinuxおよびUNIXでファイルとディレクトリを削除または削除するために使用されます。 Linuxを初めて使用する場合は、rmコマンドを実行する際は十分に注意する必要があります。これは、ファイルまたはディレクトリを削除すると、ファイルおよびディレクトリの内容を復元できないためです。削除されたファイルを復元できるツールやコマンドはいくつかありますが、そのためには専門的なスキルが必要です。

この投稿では、10個のLinuxrmコマンドの例を示します。以下は、rmコマンドの基本的な構文です。

#rm {files}

rmコマンドで使用されるオプション。

例:1ファイルを削除または削除します。

「linuxstufff.log」という名前のファイルを削除しましょう

[[email protected] ~]$ rm linuxstufff.log
[[email protected] ~]$

一度に複数のファイルを削除します。

一度に4つのテキストファイルを削除したいとします。以下の構文を使用してください

#rm {file1} {file2}] {file3} {file4}

[[email protected] ~]$ rm file1.txt file2.txt file3.txt file4.txt
[[email protected] ~]$

例:2ファイルをインタラクティブに削除します。

-i rmコマンドの‘オプションは、ファイルを削除する前にプロンプ​​トを表示します。例を以下に示します。

[[email protected] ~]$ rm -i linuxstufff.log 
rm: remove regular file ‘linuxstufff.log’? y
[[email protected] ~]$

例:3Linuxで空のディレクトリを削除する

-d」を使用します 空のディレクトリを削除するためのrmコマンドの‘オプション。

[[email protected] ~]$ ls -R appdata/
appdata/:
[[email protected] ~]$ rm -d appdata/
[[email protected] ~]$

rmdir」を使用することもできます ‘Linuxで空のディレクトリを削除するコマンド。

[[email protected] ~]$ ls -R appdata/
appdata/:
[[email protected] ~]$ rmdir appdata
[[email protected] ~]$

例:4「-r」オプションを使用してディレクトリを再帰的に削除する

-r rmコマンドの‘オプションは、親ディレクトリのすべてのファイルとサブディレクトリを再帰的に削除します。

[[email protected] ~]$ ls -lR dbstore/
dbstore/:
total 0
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 26 23:59 file1.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 26 23:59 file2.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 26 23:59 file3.log
drwxrwxr-x. 2 linuxtechi linuxtechi 6 Mar 26 23:59 service

dbstore/service:
total 0
[[email protected] ~]$ rm -r dbstore/
[[email protected] ~]$

例:5ファイルとサブディレクトリをインタラクティブに削除します。

-ri」を使用します rmコマンドの‘オプションを使用して、ファイルとサブディレクトリをインタラクティブに削除します。‘ dbstore’のすべてのファイルとディレクトリを削除するとします。 ディレクトリをインタラクティブに。

[[email protected] ~]$ ls -lR dbstore/
dbstore/:
total 0
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 00:02 file1.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 00:02 file2.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 00:02 file3.log
drwxrwxr-x. 2 linuxtechi linuxtechi 6 Mar 27 00:02 service

dbstore/service:
total 0
[[email protected] ~]$ rm -ri dbstore/
rm: descend into directory ‘dbstore/’? y
rm: remove regular empty file ‘dbstore/file1.log’? y
rm: remove regular empty file ‘dbstore/file2.log’? y
rm: remove regular empty file ‘dbstore/file3.log’? y
rm: remove directory ‘dbstore/service’? y
rm: remove directory ‘dbstore/’? y
[[email protected] ~]$

例:6「-f」オプションを使用してファイルを強制的に削除する

-f rmコマンドの‘オプションは、アクセス許可に関係なくファイルを強制的に削除または削除し、存在しないファイルも無視します。

書き込み禁止のファイル「tech.txt」を削除しましょう

[[email protected] ~]$ ls -l tech.txt 
-r--r--r--. 1 linuxtechi linuxtechi 0 Mar 27 00:23 tech.txt
[[email protected] ~]$
[[email protected] ~]$ rm tech.txt 
rm: remove write-protected regular empty file ‘tech.txt’?
>

上記のように、「-f」オプションを指定せずにrmコマンドを使用して書き込み保護されたファイルを削除しようとすると、書き込み保護されたファイルを削除するように求められます。

次に、「-f」オプションを使用してファイルを削除してみます。

[[email protected] ~]$ rm -f tech.txt 
[[email protected] ~]$

また、存在しないファイルを削除してみてください。

[[email protected] ~]$ rm -f nonexist.txt
[[email protected] ~]$

:rmコマンドの「-f」オプションは書き込み禁止ディレクトリでは機能しません

例を見てみましょう。ディレクトリ‘ / home / linuxtechi / location / ‘は書き込み保護され、ファイル(‘ db_stuff ‘)このディレクトリ内は保護されていません。

[[email protected] ~]$ ls -ld /home/linuxtechi/location/
drwxrwxr-x. 2 root root 29 Mar 27 00:43 /home/linuxtechi/location/

[[email protected] ~]$ ls -l /home/linuxtechi/location/db_stuff 
-rw-rw-r--. 1 linuxtechi linuxtechi 17 Mar 27 00:43 /home/linuxtechi/location/db_stuff

[[email protected] ~]$ rm -f /home/linuxtechi/location/db_stuff
rm: cannot remove ‘/home/linuxtechi/location/db_stuff’: Permission denied
[[email protected] ~]$

例7:3つ以上のファイルを削除する前、または再帰的に削除する前に1回プロンプトを表示します。

-私 rmコマンドの‘オプションは、3つ以上のファイルを削除する前、または再帰的に削除する前に1回プロンプトを表示します。

ディレクトリ「linux_store」の下にある「app」という名前で始まるすべてのログファイルを削除したいとします。

[[email protected] ~]$ ls -l linux_store/
total 0
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:07 app1.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:07 app2.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:07 app3.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:07 app4.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:07 app5.log
[[email protected] ~]$ rm -I linux_store/app*
rm: remove 5 arguments? y
[[email protected] ~]$

例:8rmコマンドの正規表現

rmコマンドで正規表現を使用できます。いくつかの例を以下に示します:

ディレクトリ「linux_store」の下のlog1からlog5までの5つのログファイルを削除しましょう。 ‘。

[[email protected] linux_store]$ pwd
/home/linuxtechi/linux_store
[[email protected] linux_store]$ ll
total 0
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log1.txt
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log2.txt
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log3.txt
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log4.txt
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log5.txt
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log6.txt
........................................
[[email protected] linux_store]$ 
[[email protected] linux_store]$ rm -f log{1..5}.txt
[[email protected] linux_store]$

「.txt」で終わる現在のディレクトリのすべてのファイルを削除します

[[email protected] linux_store]$ rm -f *.txt
[[email protected] linux_store]$

拡張子が3文字の現在の作業ディレクトリのすべてのファイルを削除します。

[[email protected] linux_store]$ rm -f *.???
[[email protected] linux_store]$

例:9rmコマンドを使用して多数のファイルを削除します。

rmコマンドを使用して多数のファイルを削除しようとすると、「引数リストが長すぎます」というエラーメッセージが表示されます

次の例では、ディレクトリ「/ home / linuxtechi / linux_store」のすべてのファイル(「300001」付近)を一度に削除しようとしています。

[[email protected] linux_store]$ ls -l | wc -l
300001
[[email protected] linux_store]$ rm *.log
-bash: /bin/rm: Argument list too long
[[email protected] linux_store]$

この問題を解決するには、以下のfindコマンドを使用します。

[[email protected] ~]$ find ~/linux_store/ -type f -exec rm {} \;
[[email protected] ~]$

例:10ハイフン記号(-)で始まるファイルを削除する

-store」という名前のファイルがあるとします。 ‘現在の作業ディレクトリにあり、このファイルを削除します。

[[email protected] linux_store]$ ll
total 0
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 02:05 -store
[[email protected] linux_store]$ rm -store
rm: invalid option -- 's'
Try 'rm --help' for more information.
[[email protected] linux_store]$

このようなファイルを削除するには、以下のいずれかのコマンドを使用してください。

[[email protected] linux_store]$ rm -- \ -store 
[[email protected] linux_store]$

OR

[[email protected] linux_store]$ rm ./\ -store 
[[email protected] linux_store]$

続きを読む16Linux初心者向けの便利な「cp」コマンドの例


Linux
  1. 初心者向けのLinuxcdコマンドチュートリアル(8例)

  2. 初心者向けのLinux日付コマンドチュートリアル(8例)

  3. 初心者向けのLinuxkillallコマンド(8例)

  1. 初心者のための10のLinuxCatコマンドの例

  2. 初心者向けのLinux通信コマンドチュートリアル(5例)

  3. 初心者向けのLinuxdfコマンドチュートリアル(8例)

  1. 初心者向けのLinuxdirコマンド(10例)

  2. 初心者向けのLinuxCsplitコマンドの説明(6例)

  3. 初心者向けのLinuxduコマンドチュートリアル(10例)