文件操作的命令指南
1. 概述
在 Linux 中,一切都是文件 。因此,文件操作——创建文件、删除目录等——是 Linux 中非常常见的操作。
在本教程中,让我们看看一些有用的文件操作命令并学习如何使用它们。
请注意,我们将只关注文件本身的操作,而不是它们的内容。
2. touch命令
** 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命令还可以帮助我们更新文件的访问时间和修改时间。
我们可以使用*-a选项来更新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命令非常相似。
让我们看一个如何一次性创建三个目录的示例:
$ 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命令的一个非常方便的选项是*-p*。-p选项允许我们根据需要创建父目录,并且在目录存在时不抱怨。
让我们在现有的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创建的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命令通常以静默方式工作。因此,我们在**执行rm命令时应该非常小心,尤其是使用*-r和-f*选项时。**一旦文件或目录被删除,很难再次恢复它们。
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
当然,我们可以通过文件 globbing 来做同样的事情:
$ cp *.txt targetDir
文件复制的另一个日常用法是将一些源目录及其下的所有内容复制到目标目录。 为此,我们传递-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命令的另一个非常有用的选项是*–preserve*。
我们可以将–preserve*选项与我们想要保留的属性一起传递。*
默认模式下,所有权和时间戳将被保留。
假设我们有一个文件“ guestFile ”:
$ ls -l
total 0
-rw-r--r-- 1 guest guest 0 Oct 27 18:35 guestFile
ls结果表明:
- 该文件由用户来宾和组来宾拥有
- 文件修改时间为2019-10-27 18:35
现在我们换成另一个用户,例如用户root。
$ su root
Password:
root#
然后,我们将文件复制两次;一次使用和一次不使用*–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的命令语法与cp类似。
我们来看看如何使用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和*newDir。*实际上,我们只是重命名了文件和目录。
重命名文件或目录是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