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

ファイルを作成、コピー、移動、および削除するためのLinuxファイル管理コマンド

この記事では、ファイルとディレクトリを表示、作成、コピー、移動、および削除するためのLinux®ファイル管理コマンドの概要について説明します。

ディレクトリとファイルを表示

ディレクトリ内のファイルを表示するには、 lsを使用します コマンド。

ファイルの内容を表示するには、 catを使用します コマンド。

ls コマンド

lsを使用できます ディレクトリの内容を表示するコマンド。 ls ls -lahなどのコマンドオプション 、追加情報を提供します。人間が読める形式の長いリストビューにすべてのファイル(隠しファイルを含む)のリストが含まれています。

構文lsディレクトリ名

次の例は、空のディレクトリのリストを示しています。 ls ls -lah はファイルを返しませんが、 隠しファイルと隠しディレクトリを返します。ディレクトリは、 dで始まる行で示されます。 。

[root@server-01 testdir]# ls
[root@server-01 testdir]#
[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:46 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..

cat コマンド

cat コマンドはファイルの内容を表示します。

構文 cat filename

次の例は、重要のコンテンツを表示する方法を示しています catを含むファイル コマンド:

[root@server-01 testdir]# cat Important
DON'T DELETE THIS TEXT.
ファイルを作成

次のコマンドを使用してファイルを作成できます。

  • タッチ
  • cat>
  • >

touch コマンド

touch コマンドは空のファイルを作成します。

構文 touch newfilename

次の例では、タッチを使用しています 新しいファイルを作成するコマンド、デモ およびsample.txt

[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:49 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
[root@server-01 testdir]# touch demo
[root@server-01 testdir]# touch sample.txt
[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:50 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:50 demo
-rw-r--r--. 1 root root    0 Apr 14 01:50 sample.txt

タッチを使用することもできます 1つのコマンドで複数のファイルを作成するコマンド。

次の例では、タッチを使用しています 新しいファイルを作成するコマンド、 sample1 sample2 、および sample3

[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:50 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:50 demo
-rw-r--r--. 1 root root    0 Apr 14 01:50 sample.txt
[root@server-01 testdir]# touch sample1 sample2 sample3
[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:52 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:50 demo
-rw-r--r--. 1 root root    0 Apr 14 01:52 sample1
-rw-r--r--. 1 root root    0 Apr 14 01:52 sample2
-rw-r--r--. 1 root root    0 Apr 14 01:52 sample3
-rw-r--r--. 1 root root    0 Apr 14 01:50 sample.txt

cat> コマンド

cat> コマンドは、大なり記号の後に内容を入力して、空でないファイルを作成します。

構文cat>追加するテキスト

次の例では、 cat>を使用しています その後に、新しいファイル test.txtを作成するためのコンテンツが続きます 、1行のコンテンツ:

[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:57 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
[root@server-01 testdir]# cat > test.txt
This is only a test.
[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:57 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:57 test.txt

> コマンド

構文> newfilename

標準のリダイレクト記号、> 、コンテンツなしで単一の新しいファイルを作成するか、既存のファイルを同じ名前の空のファイルに置き換えます。

注意: 誤って既存のファイルを上書きする可能性があるため、リダイレクト記号は注意して使用する必要があります。これらの変更は永続的です。以前のコンテンツを復元することはできません。

次の例では、>を使用しています 新しいファイルを作成するには、 example.txt

[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:59 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:58 test.txt
[root@server-01 testdir]# > example.txt
[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 2 root root 4.0K Apr 14 02:04 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:58 test.txt

次の例は、既存のファイルの内容を誤って書き換える方法を示しています。重要

[root@server-01 testdir]# cat Important
DON'T DELETE THIS TEXT.
[root@server-01 testdir]# > Important
[root@server-01 testdir]# cat Important
[root@server-01 testdir]#

注: 前述のように、リダイレクトシンボルは、前の例に示すように、失われたデータを回復するオプションなしで書き換えることができます。重要なファイルを書き直すと、壊滅的な問題が発生する可能性があります。

ただし、2つのリダイレクトシンボル>>を使用できます 、ファイルの最後にコンテンツを追加します。ファイルが存在しない場合は、>> ファイルを作成し、内容を追加します。ファイルがすでに存在する場合は、>> 新しいコンテンツをファイルの最後に追加します。

構文>>ファイル名

次の例では、>>を使用しています change.txtのコンテンツを追加します ファイルの最後に、 edit.txt >> コマンドは、 edit.txtの完全な書き換えを防ぎます 。

[root@server-01 testdir]# cat edit.txt
Examples
Are
[root@server-01 testdir]# cat change.txt
Great
[root@server-01 testdir]# cat change.txt >> edit.txt
[root@server-01 testdir]# cat edit.txt
Examples
Are
Great
ディレクトリを作成する

mkdir *を使用する 空のディレクトリを作成します。

構文 mkdir new-dirname

次の例では、 mkdirを使用しています 新しいディレクトリを作成するには、 folder1 およびfolder2

[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 2 root root 4.0K Apr 14 03:14 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
-rw-r--r--. 1 root root    0 Apr 14 02:10 Important
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:58 test.txt
[root@server-01 testdir]# mkdir folder1
[root@server-01 testdir]# mkdir folder2/
[root@server-01 testdir]# ls -lah
total 20K
drwxr-xr-x. 4 root root 4.0K Apr 14 03:15 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder2
-rw-r--r--. 1 root root    0 Apr 14 02:10 Important
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:58 test.txt

mkdirを使用することもできます 1つのコマンドで複数のディレクトリを作成するコマンド。

次の例では、 mkdirを使用しています 新しいディレクトリを作成するには、 folderA folderB 、および folderC

[root@server-01 testdir]# mkdir folderA folderB folderC
[root@server-01 testdir]# ls -lah
total 32K
drwxr-xr-x. 7 root root 4.0K Apr 14 03:16 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder2
drwxr-xr-x. 2 root root 4.0K Apr 14 03:16 folderA
drwxr-xr-x. 2 root root 4.0K Apr 14 03:16 folderB
drwxr-xr-x. 2 root root 4.0K Apr 14 03:16 folderC
-rw-r--r--. 1 root root    0 Apr 14 02:10 Important
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:58 test.txt
ファイルまたはディレクトリをコピーする

cp コマンドは、既存のファイルを新しいファイルにコピーします。

構文 cp orig-filename new-filename

次の例では、 cpを使用しています 新しいファイルを作成するコマンドsamplecopy.txt 、既存のファイルから、 sample.txt

[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 3 root root 4.0K Apr 14 03:19 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
[root@server-01 testdir]# cp sample.txt samplecopy.txt
[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 3 root root 4.0K Apr 14 03:29 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
-rw-r--r--. 1 root root    0 Apr 14 03:29 samplecopy.txt
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt

次の例では、 cp -rを使用しています 新しいディレクトリを作成するには、 copyfolder1 、既存のディレクトリから、 folder1

[root@server-01 testdir]# cp -r folder1 copyfolder1
[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 4 root root 4.0K Apr 14 03:32 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
drwxr-xr-x. 2 root root 4.0K Apr 14 03:32 copyfolder1
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
-rw-r--r--. 1 root root    0 Apr 14 03:29 samplecopy.txt
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
ファイルまたはディレクトリを移動する

mvを使用できます ファイルを別のディレクトリに移動するコマンド。

構文mvファイル名の宛先

次の例では、 mvを使用しています sample.txtを移動するコマンド 現在のディレクトリからfolder1 ディレクトリ:

[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 03:58 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root   19 Apr 14 03:49 edit.txt
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
[root@server-01 testdir]# mv sample.txt folder1/
[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 03:58 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root   19 Apr 14 03:49 edit.txt
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 folder1
[root@server-01 testdir]# cd folder1
[root@server-01 folder1]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 .
drwxr-xr-x. 3 root root 4.0K Apr 14 03:58 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt

ご覧のとおり、 sample.txt 前のディレクトリから移動し、 folder1に表示されるようになりました ディレクトリ。

mvを使用することもできます 既存のファイルまたはディレクトリの名前を変更するコマンド。

次の例では、 mvを使用しています デモの名前を変更するコマンド newdemoにファイルする :

[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 03:58 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root   19 Apr 14 03:49 edit.txt
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 folder1
[root@server-01 testdir]# mv demo newdemo
[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:11 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root   19 Apr 14 03:49 edit.txt
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 folder1
-rw-r--r--. 1 root root    0 Apr 14 01:57 newdemo

次の例では、 mvを使用しています デモを書き直す 最終でファイルする ファイル。このアクションは、デモを置き換えます 。

[root@server-01 testdir]# cat demo
This is a Newer Version of Demo.
[root@server-01 testdir]# cat final
Demo Replaced by Final Version.
[root@server-01 testdir]# mv final demo
mv: overwrite ‘demo’? y
[root@server-01 testdir]# cat demo
Demo Replaced by Final Version.
[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:26 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root   32 Apr 14 04:24 demo
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1

注意: mvを使用する 既存のファイルを上書きするコマンドは永続的です。前のファイルを復元することはできません。

ファイルを削除する

rmを使用する ファイルを削除するコマンド。

構文 *:rmファイル名

次の例では、 rmを使用しています 既存のファイルを削除するコマンド、デモ

[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:26 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root   32 Apr 14 04:24 demo
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1
[root@server-01 testdir]# rm demo
rm: remove regular file ‘demo’? y
[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:31 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1

注意: rmを使用する 既存のファイルを削除するコマンドは永続的です。前のファイルを復元することはできません。

空のディレクトリを削除します

rmdirを使用します 空のディレクトリを削除するコマンド。

構文 :rmdirディレクトリ名

次の例では、 rmdirを使用しています 空のディレクトリを削除するコマンド、 emptyfolder

[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 4 root root 4.0K Apr 14 04:35 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
drwxr-xr-x. 2 root root 4.0K Apr 14 04:35 emptyfolder1
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1
[root@server-01 testdir]# rmdir emptyfolder1
[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:36 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1
空でないディレクトリを削除する

rmdir 次の例に示すように、コマンドはコンテンツを含むディレクトリを削除できません。

 [root@server-01 testdir]# rmdir newfolder1
 rmdir: failed to remove ‘newfolder1’: Directory not empty

ただし、 rmは使用できます オプション-r コンテンツを含むディレクトリを削除します。

構文: :rm-rディレクトリ名

次の例では、 rm -rを使用しています 空でないディレクトリを削除するには、 newfolder1 、およびそのファイル、 sample.txt

[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:36 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1
[root@server-01 testdir]# rm -r newfolder1
rm: descend into directory ‘newfolder1’? y
rm: remove regular empty file ‘newfolder1/sample.txt’? y
rm: remove directory ‘newfolder1’? y
[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 04:43 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..

注意: rmを使用する 既存のディレクトリを削除するコマンドは永続的です。以前のディレクトリとコンテンツを復元することはできません。


Linux
  1. Linuxでコマンドをコピーおよび移動するためのプログレスバーを追加するにはどうすればよいですか?

  2. Linux ファイルとディレクトリの削除

  3. オブジェクト ファイルをコピーおよび変換するための Linux Objcopy コマンドの例

  1. Linuxターミナルでファイルをコピーする

  2. Linuxターミナルでファイルを移動する

  3. Linux のコピー、移動、削除、および強制終了コマンドを監視するための監査ルール

  1. Linuxでルートメール(メールボックス)ファイルを削除する方法

  2. Linuxでコマンドラインからファイルとディレクトリを削除する方法

  3. 「cp」および「mv」コマンドを使用してLinuxでファイルとディレクトリをコピー/移動する方法