mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
commit
04e86b08f0
@ -1,202 +0,0 @@
|
||||
warmfrog is translating
|
||||
Linux for Beginners: Moving Things Around
|
||||
======
|
||||
|
||||
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/filesystem-linux.jpg?itok=NQCoYl1f)
|
||||
|
||||
In previous installments of this series, [you learned about directories][1] and how [permissions to access directories work][2]. Most of what you learned in those articles can be applied to files, except how to make a file executable.
|
||||
|
||||
So let's deal with that before moving on.
|
||||
|
||||
### No _.exe_ Needed
|
||||
|
||||
In other operating systems, the nature of a file is often determined by its extension. If a file has a _.jpg_ extension, the OS guesses it is an image; if it ends in _.wav_ , it is an audio file; and if it has an _.exe_ tacked onto the end of the file name, it is a program you can execute.
|
||||
|
||||
This leads to serious problems, like trojans posing as documents. Fortunately, that is not how things work in Linux. Sure, you may see occasional executable file endings in _.sh_ that indicate they are runnable shell scripts, but this is mostly for the benefit of humans eyeballing files, the same way when you use `ls --color`, the names of executable files show up in bright green.
|
||||
|
||||
The fact is most applications have no extension at all. What determines whether a file is really program is the _x_ (for _executable_ ) bit. You can make any file executable by running
|
||||
```
|
||||
chmod a+x some_program
|
||||
|
||||
```
|
||||
|
||||
regardless of its extension or lack thereof. The `x` in the command above sets the _x_ bit and the `a` says you are setting it for _all_ users. You could also set it only for the group of users that own the file (`g+x`), or for only one user, the owner (`u+x`).
|
||||
|
||||
Although we will be covering creating and running scripts from the command line later in this series, know that you can run a program by writing the path to it and then tacking on the name of the program on the end:
|
||||
```
|
||||
path/to/directory/some_program
|
||||
|
||||
```
|
||||
|
||||
Or, if you are currently in the same directory, you can use:
|
||||
```
|
||||
./some_program
|
||||
|
||||
```
|
||||
|
||||
There are other ways of making your program available from anywhere in the directory tree (hint: look up the `$PATH` environment variable), but you will be reading about those when we talk about shell scripting.
|
||||
|
||||
### Copying, Moving, Linking
|
||||
|
||||
Obviously, there are more ways of modifying and handling files from the command line than just playing around with their permissions. Most applications will create a new file if you still try to open a file that doesn't exist. Both
|
||||
```
|
||||
nano test.txt
|
||||
|
||||
```
|
||||
|
||||
and
|
||||
```
|
||||
vim test.txt
|
||||
|
||||
```
|
||||
|
||||
([nano][3] and [vim][4] being to popular command line text editors) will create an empty _test.txt_ file for you to edit if _test.txt_ didn't exist beforehand.
|
||||
|
||||
You can also create an empty file by _touching_ it:
|
||||
```
|
||||
touch test.txt
|
||||
|
||||
```
|
||||
|
||||
Will create a file, but not open it in any application.
|
||||
|
||||
You can use `cp` to make a copy of a file in another location or under a new name:
|
||||
```
|
||||
cp test.txt copy_of_test.txt
|
||||
|
||||
```
|
||||
|
||||
You can also copy a whole bunch of files:
|
||||
```
|
||||
cp *.png /home/images
|
||||
|
||||
```
|
||||
|
||||
The instruction above copies all the PNG files in the current directory into an _images/_ directory hanging off of your home directory. The _images/_ directory has to exist before you try this, or `cp` will show an error. Also, be warned that, if you copy a file to a directory that contains another file with the same name, `cp` will silently overwrite the old file with the new one.
|
||||
|
||||
You can use
|
||||
```
|
||||
cp -i *.png /home/images
|
||||
|
||||
```
|
||||
|
||||
If you want `cp` to warn you of any dangers (the `-i` options stands for _interactive_ ).
|
||||
|
||||
You can also copy whole directories, but you need the `-r` option for that:
|
||||
```
|
||||
cp -rv directory_a/ directory_b
|
||||
|
||||
```
|
||||
|
||||
The `-r` option stands for _recursive_ , meaning that `cp` will drill down into _directory_a_ , copying over all the files and subdirectories contained within. I personally like to include the `-v` option, as it makes `cp` _verbose_ , meaning that it will show you what it is doing instead of just copying silently and then exiting.
|
||||
|
||||
The `mv` command moves stuff. That is, it changes files from one location to another. In its simplest form, `mv` looks a lot like `cp`:
|
||||
```
|
||||
mv test.txt new_test.txt
|
||||
|
||||
```
|
||||
|
||||
The command above makes _new_test.txt_ appear and _test.txt_ disappear.
|
||||
```
|
||||
mv *.png /home/images
|
||||
|
||||
```
|
||||
|
||||
Moves all the PNG files in the current directory to a directory called _images/_ hanging of your home directory. Again you have to be careful you do not overwrite existing files by accident. Use
|
||||
```
|
||||
mv -i *.png /home/images
|
||||
|
||||
```
|
||||
|
||||
the same way you would with `cp` if you want to be on the safe side.
|
||||
|
||||
Apart from moving versus copying, another difference between `mv` and `cp`is when you move a directory:
|
||||
```
|
||||
mv directory_a/ directory_b
|
||||
|
||||
```
|
||||
|
||||
No need for a recursive flag here. This is because what you are really doing is renaming the directory, the same way in the first example, you were renaming the file*. In fact, even when you "move" a file from one directory to another, as long as both directories are on the same storage device and partition, you are renaming the file.
|
||||
|
||||
You can do an experiment to prove it. `time` is a tool that lets you measure how long a command takes to execute. Look for a hefty file, something that weighs several hundred MBs or even some GBs (say, something like a long video) and try copying it from one directory to another like this:
|
||||
```
|
||||
$ time cp hefty_file.mkv another_directory/
|
||||
real 0m3,868s
|
||||
user 0m0,016s
|
||||
sys 0m0,887s
|
||||
|
||||
```
|
||||
|
||||
In bold is what you have to type into the terminal and below what `time` outputs. The number to focus on is the one on the first line, _real_ time. It takes nearly 4 seconds to copy the 355 MBs of _hefty_file.mkv_ to _another_directory/_.
|
||||
|
||||
Now let's try moving it:
|
||||
```
|
||||
$ time mv hefty_file.mkv another_directory/
|
||||
real 0m0,004s
|
||||
user 0m0,000s
|
||||
sys 0m0,003s
|
||||
|
||||
```
|
||||
|
||||
Moving is nearly instantaneous! This is counterintuitive, since it would seem that `mv` would have to copy the file and then delete the original. That is two things `mv` has to do versus `cp`'s one. But, somehow, `mv` is 1000 times faster.
|
||||
|
||||
That is because the file system's structure, with all its tree of directories, only exists for the users convenience. At the beginning of each partition there is something called a _partition table_ that tells the operating system where to find each file on the actual physical disk. On the disk, data is not split up into directories or even files. [There are tracks, sectors and clusters instead][5]. When you "move" a file within the same partition, what the operating system does is just change the entry for that file in the partition table, but it still points to the same cluster of information on the disk.
|
||||
|
||||
Yes! Moving is a lie! At least within the same partition that is. If you try and move a file to a different partition or a different device, `mv` is still fast, but is noticeably slower than moving stuff around within the same partition. That is because this time there is actually copying and erasing of data going on.
|
||||
|
||||
### Renaming
|
||||
|
||||
There are several distinct command line `rename` utilities around. None are fixtures like `cp` or `mv` and they can work in slightly different ways. What they all have in common is that they are used to change _parts_ of the names of files.
|
||||
|
||||
In Debian and Ubuntu, the default `rename` utility uses [regular expressions][6] (patterns of strings of characters) to mass change files in a directory. The instruction:
|
||||
```
|
||||
rename 's/\.JPEG$/.jpg/' *
|
||||
|
||||
```
|
||||
|
||||
will change all the extensions of files with the extension _JPEG_ to _jpg_. The file _IMG001.JPEG_ becomes _IMG001.jpg_ , _my_pic.JPEG_ becomes _my_pic.jpg_ , and so on.
|
||||
|
||||
Another version of `rename` available by default in Manjaro, a derivative of Arch, is much simpler, but arguably less powerful:
|
||||
```
|
||||
rename .JPEG .jpg *
|
||||
|
||||
```
|
||||
|
||||
This does the same renaming as you saw above. In this version, `.JPEG` is the string of characters you want to change, `.jpg` is what you want to change it to, and `*` represents all the files in the current directory.
|
||||
|
||||
The bottom line is that you are better off using `mv` if all you want to do is rename one file or directory, and that's because `mv` is realiably the same in all distributions everywhere.
|
||||
|
||||
### Learning more
|
||||
|
||||
Check out the both `mv` and `cp`'s _man_ pages to learn more. Run
|
||||
```
|
||||
man cp
|
||||
|
||||
```
|
||||
|
||||
or
|
||||
```
|
||||
man mv
|
||||
|
||||
```
|
||||
|
||||
to read about all the options these commands come with and which make them more powerful and safer to use.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.linux.com/blog/2018/8/linux-beginners-moving-things-around
|
||||
|
||||
作者:[Paul Brown][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.linux.com/users/bro66
|
||||
[1]: https://www.linux.com/blog/learn/2018/5/manipulating-directories-linux
|
||||
[2]: https://www.linux.com/blog/learn/intro-to-linux/2018/7/users-groups-and-other-linux-beasts-part-2
|
||||
[3]: https://www.nano-editor.org/
|
||||
[4]: https://www.vim.org/
|
||||
[5]: https://en.wikipedia.org/wiki/Disk_sector
|
||||
[6]: https://en.wikipedia.org/wiki/Regular_expression
|
@ -0,0 +1,202 @@
|
||||
Linux 初学者: 移动文件
|
||||
=====================
|
||||
|
||||
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/filesystem-linux.jpg?itok=NQCoYl1f)
|
||||
|
||||
在之前的该系列的部分中,[你学习了有关目录][1]和[访问目录的权限是如何工作的][2]。你在这些文章中学习的大多数都可应用于文件,除了如何让一个文件变得可执行。
|
||||
|
||||
因此让我们在开始之前先解决这个问题。
|
||||
|
||||
### 不需要 .exe 文件
|
||||
|
||||
在其他操作系统中,一个文件的性质通常由它的后缀决定。如果一个文件有一个 _.jpg_ 扩展,操作系统会认为它是一幅图像;如果它以 _.wav_ 结尾, 它是一个音频文件; 如果它在文件名末尾 以 _.exe_ 结尾, 它就是一个你可以执行的程序。
|
||||
|
||||
这导致了严重的问题,像木马伪装成文件。幸运的是,在 Linux 下事务不是这样运行的。可以确定的是,你可能会看到有些可执行文件是以 _.sh_ 结尾暗示他们是可执行的脚本,但是这大部分是为了利于人类识别的文件,和你使用 `ls --color` 的方式相同,可执行文件的名字以亮绿色显示。
|
||||
|
||||
事实上大多数应用根本没有扩展。决定一个文件是否是一个真正程序的是 _x_ (用于_可执行的_) 位。你可以通过运行以下命令使任何文件变得可执行:
|
||||
```
|
||||
chmod a+x some_program
|
||||
|
||||
```
|
||||
|
||||
而不管它的扩展名是什么或者是否存在。在上面命令中的 `x` 设置了 _x_ 位,`a` 说明你为_所有_用户设置它。你同样可以为一组用户设置成拥有这个文件 (`g+x`),或者设置为只有一个用户,拥有着 (`u+x`)。
|
||||
|
||||
尽管我们会在该系列之后部分包含从命令行创建和运行脚本的内容,并学习到你可以通过输入它的路径并在结尾加上程序名的方式运行一个程序:
|
||||
```
|
||||
path/to/directory/some_program
|
||||
|
||||
```
|
||||
|
||||
或者,如果你当前在相同目录,你可以使用:
|
||||
```
|
||||
./some_program
|
||||
|
||||
```
|
||||
|
||||
还有其他方式可以使你的程序在目录树的任意位置运行 (提示:查询 `$PATH` 环境变量),但是当我们讨论 shell 脚本的时候你会读到这些。
|
||||
|
||||
### 复制,移动,链接
|
||||
|
||||
明显地,有更多的方式来从命令行修改和处理文件,而不仅仅是处理它们的权限。当你试图打开一个不存在的文件是,大多数应用会创建一个新文件。如果 _test.txt_ 当前并不存在,下列命令
|
||||
|
||||
```
|
||||
nano test.txt
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
vim test.txt
|
||||
|
||||
```
|
||||
|
||||
([nano][3] 和 [vim][4] 是流行的命令行文本编辑器) 都将为你创建一个空的 _test.txt_ 文件来编辑。
|
||||
|
||||
你可以通过 “触摸” (touching, 触摸)来创建一个空的文件:
|
||||
```
|
||||
touch test.txt
|
||||
|
||||
```
|
||||
|
||||
会创建一个文件,但是不会在任何应用中打开它。
|
||||
|
||||
你可以使用 `cp` 来拷贝一个文件到另一个位置或者使用一个不同的名字:
|
||||
```
|
||||
cp test.txt copy_of_test.txt
|
||||
|
||||
```
|
||||
|
||||
你也可以拷贝一堆文件:
|
||||
```
|
||||
cp *.png /home/images
|
||||
|
||||
```
|
||||
|
||||
上面的命令拷贝当前目录下的所有 PNG 文件到相对你的 home 目录下的 _images/_ 目录。在你尝试之前 _images/_ 目录必须存在, 不然 `cp` 将显示一个错误。同样的,警惕,当你复制一个文件到一个已经包含相同名字的文件的目录时, `cp` 会静默地用新文件覆盖老的文件。
|
||||
|
||||
你可以使用
|
||||
```
|
||||
cp -i *.png /home/images
|
||||
|
||||
```
|
||||
|
||||
如果你想要 `cp` 命令在有任何危险时警告你 (`-i` 选项代表 _交互式的_)。
|
||||
|
||||
你同样可以复制整个目录,但是为了做到这样,你需要 `-r` 选项:
|
||||
```
|
||||
cp -rv directory_a/ directory_b
|
||||
|
||||
```
|
||||
|
||||
`-r` 选项代表 _递归_,意味着 `cp` 会向下探索目录 _directory_a_,复制所有的文件和子目录下内部包含的。我个人喜欢包含 `-v` 选项,因为它使 `cp` 冗长而啰嗦,意味着它会显示你当前它正在做什么而不是仅仅静默的复制然后存在。
|
||||
|
||||
`mv` 命令移动东西。也就是说,它移动文件从一个位置到另一个位置。最简单的形式,`mv` 表现的更像 `cp`:
|
||||
```
|
||||
mv test.txt new_test.txt
|
||||
|
||||
```
|
||||
|
||||
上面的命令使 _new_test.txt_ 出现, _test.txt_ 消失。
|
||||
```
|
||||
mv *.png /home/images
|
||||
|
||||
```
|
||||
|
||||
移动当前目录下所有的 PNG 文件到相对于你的 home 目录的 _images/_ 目录。同样的你必须小心你没有意外的覆盖已存在的文件。使用
|
||||
```
|
||||
mv -i *.png /home/images
|
||||
|
||||
```
|
||||
|
||||
如果你想站在安全的角度,你可以使用与 `cp` 相同的方式。
|
||||
|
||||
除了移动与拷贝的不同外,另一个 `mv` 和 `cp` 之间的不同是当你移动目录时:
|
||||
```
|
||||
mv directory_a/ directory_b
|
||||
|
||||
```
|
||||
|
||||
不需要添加递归的标志。这是因为你实际做的是重命名一个目录,与第一个例子相同,你做的是重命名文件。实际上,即使你 “移动” 一个文件从一个目录到另一个目录,只要两个目录在相同的存储设备和分区,你就是在重命名文件。
|
||||
|
||||
你可以做一个实验来证明。 `time` 是一个工具来让你测量一个命令花费多久来执行。找一个非常大的文件,可以是几百 MBs 甚至 几 GBs (例如一个长视频),像下方这样尝试拷贝到另一个目录:
|
||||
```
|
||||
$ time cp hefty_file.mkv another_directory/
|
||||
real 0m3,868s
|
||||
user 0m0,016s
|
||||
sys 0m0,887s
|
||||
|
||||
```
|
||||
|
||||
黑体是你必须输入命令行的,下面是 `time` 的输出。需要关注的是第一行, _real_ 时间。它花费了几乎 4 秒来拷贝 355 MBs 的 _hefty_file.mkv_ 到 _another_directory/_ 目录。
|
||||
|
||||
现在让我们尝试移动它:
|
||||
```
|
||||
$ time mv hefty_file.mkv another_directory/
|
||||
real 0m0,004s
|
||||
user 0m0,000s
|
||||
sys 0m0,003s
|
||||
|
||||
```
|
||||
|
||||
移动几乎是瞬时的!这是违反直觉的,因为看起来 `mv` 必须复制这个文件然后删除原来的。这是 `mv` 对比 `cp` 命令必须做的两件事。但是,实际上,`mv` 扩了 1000 倍。
|
||||
|
||||
这是因为文件系统结构中,它的所有目录树,只为了让用户便利而存在。在每个分区的开始,有一个称作 _分区表_ 的东西告诉操作系统在实际的物理磁盘上去哪找每个文件。在磁盘上,数据没有分为目录甚至是文件。[作为替代的是轨道,扇区和簇][5]。当你在相同分区 “移动” 一个文件时,操作系统实际做的仅仅是在分区表中改变了那个文件的入口,但它仍然指向磁盘上相同的簇信息。
|
||||
|
||||
是的!移动是一个谎言!至少在相同分区下是。如果你试图移动一个文件到一个不同的分区或者不同的设备, `mv` 仍然很快,但可以察觉到它比在相同分区下移动文件慢了。这是因为实际上发生了复制和清除数据。
|
||||
|
||||
### 重命名
|
||||
|
||||
有几个不同的命令行 `rename` 工具。没有一个像 `cp` 和 `mv` 那样固定并且他们工作的方式都有一点不同。他们相同的一点是他们被用来改变文件名的部分。
|
||||
|
||||
在 Debian 和 Ubuntu 中, 默认的 `rename` 工具使用 [正则表达式][6] (字符组成的字符串图案)来大量的改变目录中的文件。命令:
|
||||
```
|
||||
rename 's/\.JPEG$/.jpg/' *
|
||||
|
||||
```
|
||||
|
||||
将改变所有扩展为 _JPEG_ 的文件为 _jpg_ 。文件 _IMG001.JPEG_ 变成 _IMG001.jpg_, _my_pic.JPEG_ 变成 _my_pic.jpg_ , 等等。
|
||||
|
||||
另一个 `rename` 版本默认在 Manjaro 上可获得,一个 Arch 的衍生版,更简单,但是可能没有那么强大:
|
||||
``` rename .JPEG .jpg *
|
||||
|
||||
```
|
||||
|
||||
这和你之前看到的上面做相同的重命名操作。在这个版本,`.JPEG` 是你想改变的字符组成的字符串,`.jpg` 是你想要改变成为的,`*` 表示当前目录下的所有文件。
|
||||
|
||||
基本原则是如果你所做的仅仅是重命名一个文件或者目录,你最好用 `mv`,这是因为 `mv` 在所有分发版上都是可靠一致的。
|
||||
|
||||
### 了解更多
|
||||
|
||||
查看 `mv` 和 `cp` 的 man 页面了解更多。运行
|
||||
```
|
||||
man cp
|
||||
|
||||
```
|
||||
|
||||
或者
|
||||
```
|
||||
man mv
|
||||
|
||||
```
|
||||
|
||||
来阅读这些命令自带的所有选项,这些使他们使用起来更强大和安全。
|
||||
|
||||
----------------------------------------------------------------------------
|
||||
|
||||
via: https://www.linux.com/blog/2018/8/linux-beginners-moving-things-around
|
||||
|
||||
作者:[Paul Brown][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[warmfrog](https://github.com/warmfrog)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.linux.com/users/bro66
|
||||
[1]: https://www.linux.com/blog/learn/2018/5/manipulating-directories-linux
|
||||
[2]: https://www.linux.com/blog/learn/intro-to-linux/2018/7/users-groups-and-other-linux-beasts-part-2
|
||||
[3]: https://www.nano-editor.org/
|
||||
[4]: https://www.vim.org/
|
||||
[5]: https://en.wikipedia.org/wiki/Disk_sector
|
||||
[6]: https://en.wikipedia.org/wiki/Regular_expression
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user