便利なファイル操作コマンドのガイド
1. 概要
Linuxでは、すべてがファイルです。 したがって、ファイル操作–ファイルの作成、ディレクトリの削除など。 –Linuxでは非常に一般的な操作です。
このチュートリアルでは、いくつかの便利なファイル操作コマンドを見て、それらの使用方法を学びましょう。
内容ではなく、ファイル自体の操作にのみ焦点を当てることに注意してください。
2. touchコマンド
touch コマンドを使用すると、コンテンツなしでファイルを作成できます。
空のファイルを作成するには、ファイル名をタッチするだけです:
$ ls -l
total 0
$ touch file1.txt file2.txt file3.txt
$ ls -l
total 0
-rw-r--r-- 1 kent kent 0 Oct 27 10:50 file1.txt
-rw-r--r-- 1 kent kent 0 Oct 27 10:50 file2.txt
-rw-r--r-- 1 kent kent 0 Oct 27 10:50 file3.txt
空のファイルを作成する場合を除いて、 touchコマンドは、ファイルのアクセス時間と変更時間を更新するのに役立ちます。
–オプションを使用して、file.txtのアクセス時間を更新できます。
$ touch -a --date='1992-08-08 17:17:59' file1.txt
$ ls -u --full-time
total 0
-rw-r--r-- 1 kent kent 0 1992-08-08 17:17:59.000000000 +0200 file1.txt
-rw-r--r-- 1 kent kent 0 2019-10-27 10:50:53.007703818 +0100 file2.txt
-rw-r--r-- 1 kent kent 0 2019-10-27 10:50:53.007703818 +0100 file3.txt
上記の例では、ファイルリストにアクセス時間を表示するために、lsコマンドに-uオプションを指定しました。
-m オプションは、ファイルの変更時間を変更するためのものです。
たとえば、file3.txtの変更時刻を70年代のある時点に変更してみましょう。
$ touch -m --date='1976-11-18 18:19:59' file3.txt
$ ls --full-time
total 0
-rw-r--r-- 1 kent kent 0 2019-10-27 10:50:53.007703818 +0100 file1.txt
-rw-r--r-- 1 kent kent 0 2019-10-27 10:50:53.007703818 +0100 file2.txt
-rw-r--r-- 1 kent kent 0 1976-11-18 18:19:59.000000000 +0100 file3.txt
3. mkdirコマンド
touch は、空のファイルを作成するのに便利です。 ディレクトリに対して同じことを行うには、mkdirを使用します。
mkdir コマンドの構文は、上記のtouchコマンドと非常によく似ています。
ワンショットで3つのディレクトリを作成する方法の例を見てみましょう。
$ mkdir one two three
$ ls -l
total 0
drwxr-xr-x 2 kent kent 40 Oct 27 11:22 one/
drwxr-xr-x 2 kent kent 40 Oct 27 11:22 three/
drwxr-xr-x 2 kent kent 40 Oct 27 11:22 two/
mkdir コマンドは、作成しようとしているディレクトリがすでに存在する場合に文句を言います。
たとえば、「one」:という別のディレクトリを作成しようとすると、エラーが発生します。
$ mkdir one
mkdir: cannot create directory ‘one’: File exists
の非常に便利なオプション mkdir コマンドは
既存のoneディレクトリの下にいくつかのサブディレクトリを作成しましょう–結果をtreeで一覧表示します。
$ mkdir -p one/one.1/one.1.1 one/one.2/one.2.1
$ tree -d
.
├── one
│ ├── one.1
│ │ └── one.1.1
│ └── one.2
│ └── one.2.1
├── three
└── two
4. rmコマンド
rm コマンドは、ファイルとディレクトリを作成するのとは逆に、それらを削除します。
rm を使用してファイルを削除するのは簡単です。削除するファイル名を、rmコマンドの後に追加するだけです。
たとえば、touchで以前に作成した3つの。txtファイルを削除するとします。
$ ls
file1.txt file2.txt file3.txt
$ rm file1.txt file2.txt file3.txt
$ ls
total 0
デフォルトでは、rmはディレクトリを削除しません。 -d オプションを使用して、空のディレクトリを削除できます。
次の例では、作成したばかりのディレクトリを削除してみましょう。 two :
$ rm -d two
$ tree -d
.
├── one
│ ├── one.1
│ │ └── one.1.1
│ └── one.2
│ └── one.2.1
└── three
ディレクトリoneに同じコマンドを適用すると、ディレクトリ one が空ではなく、サブディレクトリがあるため、エラーが発生します。
$ rm -d one
rm: cannot remove 'one': Directory not empty
ディレクトリとその内容を再帰的に削除する場合は、rmの-rオプションを使用する必要があります。
$ rm -r one
$ tree -d
.
└── three
rm コマンドは、ファイルが書き込み保護されている場合、削除の確認を求めるプロンプトを表示します。
$ ls -l
total 0
drwxr-xr-x 2 kent kent 80 Oct 27 16:55 ./
drwxr-xr-x 5 kent kent 100 Oct 27 11:56 ../
-r--r--r-- 1 kent kent 0 Oct 27 16:55 readOnly1
-r--r--r-- 1 kent kent 0 Oct 27 16:54 readOnly2
$ rm readOnly1
rm: remove write-protected regular empty file 'readOnly1'? y
$ ls -l
total 0
-r--r--r-- 1 kent kent 0 Oct 27 16:54 readOnly2
ただし、 -fオプションはこのマイナーな保護を無効にし、ファイルを強制的に削除します。
-fオプションを使用して他の書き込み保護されたファイルを削除しましょう。
$ rm -f readOnly2
$ ls -l
total 0
rmコマンドは通常はサイレントに機能します。 したがって、特に-rおよび-fオプションを使用してrmコマンドを実行するときは、細心の注意を払う必要があります。ファイルまたはディレクトリが削除されると、それらを再度回復するのは非常に困難です。
5. cpコマンド
cp コマンドは、ファイルまたはディレクトリをコピーするために使用されます。
cp:を使用してファイルfile1.txtのコピーを作成する方法を見てみましょう。
$ ls
file1.txt file2.txt
$ cp file1.txt file1_cp.txt
$ ls
file1_cp.txt file1.txt file2.txt
Linuxで複数のファイルをコピーしたいことがよくあります。 これを行うには、ファイルの名前に続いて宛先ディレクトリをcpコマンドに渡します。
$ tree -F
.
├── targetDir/
├── file1_cp.txt
├── file1.txt
└── file2.txt
1 directory, 3 files
$ cp file1.txt file2.txt file1_cp.txt targetDir
$ tree
.
├── targetDir/
│ ├── file1_cp.txt
│ ├── file1.txt
│ └── file2.txt
├── file1_cp.txt
├── file1.txt
└── file2.txt
1 directory, 6 files
もちろん、ファイルグロブでも同じことができます。
$ cp *.txt targetDir
ファイルコピーのもう1つの日常的な使用法は、ソースディレクトリとその下のすべてのコンテンツをターゲットディレクトリにコピーすることです。
これを行うには、 -Rオプションを渡すと、cpはソースディレクトリを再帰的にコピーします。
$ tree -F
.
└── srcDir/
├── dir1/
│ └── file1.txt
└── dir2/
└── file2.txt
3 directories, 2 files
$ cp -R srcDir targetDir
$ tree -F
.
├── srcDir/
│ ├── dir1/
│ │ └── file1.txt
│ └── dir2/
│ └── file2.txt
└── targetDir/
├── dir1/
│ └── file1.txt
└── dir2/
└── file2.txt
6 directories, 4 files
cp コマンドのもう1つの非常に便利なオプションは、 –preserve。です。
–preserve オプションを、保持する属性とともに渡すことができます。
デフォルトのモードでは、所有権とタイムスタンプが保持されます。
ファイル「guestFile」があるとします。
$ ls -l
total 0
-rw-r--r-- 1 guest guest 0 Oct 27 18:35 guestFile
ls の結果は、次のことを示しています。
- ファイルはユーザーguestとグループguestによって所有されています
- ファイルの変更時刻は2019-10-2718:35でした
次に、別のユーザー、たとえばユーザーに変更します
$ su root
Password:
root#
次に、ファイルを2回コピーします。 –preserve オプションがある場合とない場合:
root# cp guestFile withoutPreserve
root# cp --preserve guestFile withPreserve
root# ls -l
total 0
-rw-r--r-- 1 guest guest 0 Oct 27 18:35 guestFile
-rw-r--r-- 1 root root 0 Oct 27 18:46 withoutPreserve
-rw-r--r-- 1 guest guest 0 Oct 27 18:35 withPreserve
したがって、 –preserve オプションがないと、元の所有権とタイムスタンプは保持されませんでした。 オプションを使用している間、これらの属性は保持されました。
6. mvコマンド
mv コマンドを使用して、ファイルまたはディレクトリを移動できます。 のコマンド構文 mv と類似しています
mvコマンドを使用してファイルまたはディレクトリを移動する方法を見てみましょう。
$ tree -F
.
├── oldDir/
└── oldFile
1 directory, 1 file
$ mv oldFile newFile
$ mv oldDir newDir
$ tree -F
.
├── newDir/
└── newFile
1 directory, 1 file
だから、私たちは移動しました oldFile そしてその oldDir に newFile そしてその
ファイルまたはディレクトリの名前を変更することは、mvコマンドの一般的な使用法です。
複数のファイルをターゲットディレクトリに移動することもできます。
$ tree -F
.
├── srcFile1
├── srcFile2
├── srcFile3
└── targetDir/
1 directory, 3 files
$ mv srcFile1 srcFile2 srcFile3 targetDir
$ tree -F
.
└── targetDir/
├── srcFile1
├── srcFile2
└── srcFile3
1 directory, 3 files
cp コマンドのセクションで学習したように、mvコマンドにもファイルグロブを使用できます。
次のコマンドは、今試したものと同等です。
$ mv srcFile* targetDir
7. 結論
このチュートリアルでは、いくつかの日常的なファイル操作コマンドについて例を挙げて説明しました。
これらの便利な便利なコマンドを使用して、Linuxでファイルを効率的に操作します。