mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
translated
This commit is contained in:
parent
38e74af2d6
commit
9e74a7a39d
@ -1,118 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (3 quick tips for working with Linux files)
|
||||
[#]: via: (https://www.networkworld.com/article/3440035/3-quick-tips-for-working-with-linux-files.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
3 quick tips for working with Linux files
|
||||
======
|
||||
Linux provides lots of commands for finding, counting, and renaming files. Here's a look at some useful choices.
|
||||
[GotCredit][1] [(CC BY 2.0)][2]
|
||||
|
||||
Linux provides a wide variety of commands for working with files — commands that can save you time and make your work a lot less tedious.
|
||||
|
||||
### Finding files
|
||||
|
||||
When you're looking for files, the **find** command is probably going to be the first command to come to mind, but sometimes a well-crafted **ls** command works even better. Want to remember what you called that script you were working on last night before you fled the office and drove home? Easy! Use an **ls** command with the **-ltr** options. The last files listed will be the ones most recently created or updated.
|
||||
|
||||
```
|
||||
$ ls -ltr ~/bin | tail -3
|
||||
-rwx------ 1 shs shs 229 Sep 22 19:37 checkCPU
|
||||
-rwx------ 1 shs shs 285 Sep 22 19:37 ff
|
||||
-rwxrw-r-- 1 shs shs 1629 Sep 22 19:37 test2
|
||||
```
|
||||
|
||||
A command like this one will list only the files that were updated today:
|
||||
|
||||
```
|
||||
$ ls -al --time-style=+%D | grep `date +%D`
|
||||
drwxr-xr-x 60 shs shs 69632 09/23/19 .
|
||||
drwxrwxr-x 2 shs shs 8052736 09/23/19 bin
|
||||
-rw-rw-r-- 1 shs shs 506 09/23/19 stats
|
||||
```
|
||||
|
||||
If the files you're looking for might not be in the current directory, the **find** command is going to provide better options than **ls**, but it can also result in a lot more output than you want to peruse. In this command, we're avoiding searching in directories that do _not_ begin with dots (many of those get updates all the time), specifying that we want to find files (i.e., not directories) and requesting that we only be shown files that were updated within the last day (-mtime -1).
|
||||
|
||||
```
|
||||
$ find . -not -path '*/\.*' -type f -mtime -1 -ls
|
||||
917517 0 -rwxrw-r-- 1 shs shs 683 Sep 23 11:00 ./newscript
|
||||
```
|
||||
|
||||
Notice how the **-not** option reverses the **-path** specification, so our search doesn't dive into subdirectories that begin with dots.
|
||||
|
||||
If you want to find only the largest files and directories, you can use a command like this **du** command that lists the contents of the current directory by size. Pipe the output to **tail** to see only the largest few.
|
||||
|
||||
```
|
||||
$ du -kx | egrep -v "\./.+/" | sort -n | tail -5
|
||||
918984 ./reports
|
||||
1053980 ./notes
|
||||
1217932 ./.cache
|
||||
31470204 ./photos
|
||||
39771212 .
|
||||
```
|
||||
|
||||
The **-k** option gets **du** to list file sizes in blocks, while **x** keeps it from traversing directories that are on other file systems (e.g., referenced through symbolic links). The fact that the **du** listing starts with the file sizes allows the sort by size (sort -n) to work.
|
||||
|
||||
### Counting files
|
||||
|
||||
Counting files in any particular directory is fairly easy with the **find** command. You just have to remember that find will recurse into subdirectories and will count the files in those subdirectories along with those in the current directory. In this command, we are counting files in one particular user's home directory. Depending on permissions on home directories, this may require the use of **sudo**. Remember that the first argument is the starting point for the search — in this case, the specified user's home directory.
|
||||
|
||||
```
|
||||
$ find ~username -type f 2>/dev/null | wc -l
|
||||
35624
|
||||
```
|
||||
|
||||
Note that we're sending error output from the **find** command above to the bit bucket to avoid trying to search directories like ~username/.cache that we likely cannot search and the content of which is probably not of interest.
|
||||
|
||||
When needed, you can constrain **find** to a single directory using the **maxdepth 1** option:
|
||||
|
||||
```
|
||||
$ find /home/shs -maxdepth 1 -type f | wc -l
|
||||
387
|
||||
```
|
||||
|
||||
### Renaming files
|
||||
|
||||
Files are easy to rename with the **mv** command, but sometimes you will want to rename large collections of files and likely won't want to spend a lot of time doing it. To change all the blanks that you might find in file names in the current directory to underscores, for example, you could use a command like this:
|
||||
|
||||
```
|
||||
$ rename 's/ /_/g' *
|
||||
```
|
||||
|
||||
The **g** in this command, as you likely suspect, means "global." That means the command will change _all_ blanks in a file name to underscores, not just the first one.
|
||||
|
||||
To drop the .txt extension from text files, you could use a command like this:
|
||||
|
||||
```
|
||||
$ rename 's/.txt//g' *
|
||||
```
|
||||
|
||||
### Wrap-up
|
||||
|
||||
The Linux command line provides a lot of useful options for manipulating files. Please suggest other commands that you find especially useful.
|
||||
|
||||
**[ Two-Minute Linux Tips: [Learn how to master a host of Linux commands in these 2-minute video tutorials][3] ]**
|
||||
|
||||
Join the Network World communities on [Facebook][4] and [LinkedIn][5] to comment on topics that are top of mind.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3440035/3-quick-tips-for-working-with-linux-files.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.flickr.com/photos/gotcredit/33756797815/in/photolist-TqYpVr-ot3MbP-8GVk75-bDgdSV-d8UqyY-8A1Nvm-bDgHMT-3StdY-c3CSTq-9gXm8m-piEdt6-9Jme84-ao7jBT-9gUejH-9gpPtR-XzrMMD-bqn8Qs-bDa1AK-oV87g2-bqn8SE-7hKg3v-CyDj5-bDgHKF-ppTzHf-84Czrj-dWf3MY-eDXW3i-5nTPZb-oaFrev-bqf6Rw-58EpAQ-5bd2t8-9eyUFb-5zNBi9-6geKFz-ngaqHa-6zDJtt-bvJrAQ-28v4k1Y-6s2qrs-3fPsLz-hDNitm-4nfhZC-7dZYt1-PUTxVi-4nuP2y-bDgdVg-96HPjm-bce6J8-5Mnhy
|
||||
[2]: https://creativecommons.org/licenses/by/2.0/legalcode
|
||||
[3]: https://www.youtube.com/playlist?list=PL7D2RMSmRO9J8OTpjFECi8DJiTQdd4hua
|
||||
[4]: https://www.facebook.com/NetworkWorld/
|
||||
[5]: https://www.linkedin.com/company/network-world
|
@ -0,0 +1,115 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (3 quick tips for working with Linux files)
|
||||
[#]: via: (https://www.networkworld.com/article/3440035/3-quick-tips-for-working-with-linux-files.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
处理 Linux 文件的 3 个提示
|
||||
======
|
||||
Linux 提供了许多用于查找、计数和重命名文件的命令。这有一些有用的选择。
|
||||
[GotCredit][1] [(CC BY 2.0)][2]
|
||||
|
||||
Linux 提供了多种用于处理文件的命令,这些命令可以节省你的时间,并使你的工作不那么繁琐。
|
||||
|
||||
### 查找文件
|
||||
|
||||
当你查找文件时,**find** 可能会是第一个想到的命令,但是有时精心设计的 **ls** 命令会更好。想知道你昨天离开办公室回家前调用的脚本么?简单!使用 **ls** 命令并加上 **-ltr** 选项。最后一个列出的将是最近创建或更新的文件。
|
||||
|
||||
```
|
||||
$ ls -ltr ~/bin | tail -3
|
||||
-rwx------ 1 shs shs 229 Sep 22 19:37 checkCPU
|
||||
-rwx------ 1 shs shs 285 Sep 22 19:37 ff
|
||||
-rwxrw-r-- 1 shs shs 1629 Sep 22 19:37 test2
|
||||
```
|
||||
|
||||
像这样的命令将仅列出今天更新的文件:
|
||||
|
||||
```
|
||||
$ ls -al --time-style=+%D | grep `date +%D`
|
||||
drwxr-xr-x 60 shs shs 69632 09/23/19 .
|
||||
drwxrwxr-x 2 shs shs 8052736 09/23/19 bin
|
||||
-rw-rw-r-- 1 shs shs 506 09/23/19 stats
|
||||
```
|
||||
|
||||
如果你要查找的文件可能不在当前目录中,那么 **find** 将比 **ls** 提供更好的选项,但它可能会输出比你想要的更多。在此命令中,我们_不_搜索以点开头的目录(它们很多一直在更新),指定我们要查找的文件(即不是目录),并要求仅显示最近一天 (-mtime -1) 更新的文件。
|
||||
|
||||
```
|
||||
$ find . -not -path '*/\.*' -type f -mtime -1 -ls
|
||||
917517 0 -rwxrw-r-- 1 shs shs 683 Sep 23 11:00 ./newscript
|
||||
```
|
||||
|
||||
注意 **-not ** 选项反转了 **-path** 的行为,因此我们不会搜索以点开头的子目录。
|
||||
|
||||
如果只想查找最大的文件和目录,那么可以使用类似 **du** 这样的命令,它会按大小列出当前目录的内容。将输出通过管道传输到 **tail**,仅查看最大的几个。
|
||||
|
||||
```
|
||||
$ du -kx | egrep -v "\./.+/" | sort -n | tail -5
|
||||
918984 ./reports
|
||||
1053980 ./notes
|
||||
1217932 ./.cache
|
||||
31470204 ./photos
|
||||
39771212 .
|
||||
```
|
||||
|
||||
**-k** 选项让 **du** 以块列出文件大小,而 **x** 可防止其遍历其他文件系统上的目录(例如,通过符号链接引用)。事实上,**du** 会先列出文件大小,这样可以按照大小排序(sort -n)。
|
||||
|
||||
### 文件计数
|
||||
|
||||
使用 **find** 命令可以很容易地计算任何特定目录中的文件。你只需要记住,find 会递归到子目录中,并将这些子目录中的文件与当前目录中的文件一起计数。在此命令中,我们计数一个特定用户的家目录中的文件。根据家目录的权限,这可能需要使用 **sudo**。请记住,第一个参数是搜索的起点。这里指定的是用户的家目录。
|
||||
|
||||
```
|
||||
$ find ~username -type f 2>/dev/null | wc -l
|
||||
35624
|
||||
```
|
||||
|
||||
请注意,我们正在将上面 **find** 命令的错误输出发送到 /dev/null,以避免搜索类似 \~username/.cache 这类无法搜索并且对它的内容也不感兴趣的文件夹
|
||||
|
||||
必要时,你可以使用 **maxdepth 1** 选项将 **find** 限制在单个目录中:
|
||||
|
||||
```
|
||||
$ find /home/shs -maxdepth 1 -type f | wc -l
|
||||
387
|
||||
```
|
||||
|
||||
### 重命名文件
|
||||
|
||||
使用 **mv** 命令可以很容易地重命名文件,但是有时你会想重命名大量文件,并且不想花费大量时间。例如,要将你在当前目录的文件名中找到的所有空格更改为下划线,你可以使用如下命令:
|
||||
|
||||
```
|
||||
$ rename 's/ /_/g' *
|
||||
```
|
||||
|
||||
如你怀疑的那样,此命令中的 **g** 表示“全局”。这意味着该命令会将文件名中的_所有_空格更改为下划线,而不仅仅是第一个。
|
||||
|
||||
要从文本文件中删除 .txt 扩展名,可以使用如下命令:
|
||||
|
||||
```
|
||||
$ rename 's/.txt//g' *
|
||||
```
|
||||
|
||||
### 总结
|
||||
|
||||
Linux 命令行提供了许多用于处理文件的有用选择。请提出你认为特别有用的其他命令。
|
||||
|
||||
在 [Facebook][4] 和 [LinkedIn][5] 加入 Network World 社区,评论热门主题。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3440035/3-quick-tips-for-working-with-linux-files.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.flickr.com/photos/gotcredit/33756797815/in/photolist-TqYpVr-ot3MbP-8GVk75-bDgdSV-d8UqyY-8A1Nvm-bDgHMT-3StdY-c3CSTq-9gXm8m-piEdt6-9Jme84-ao7jBT-9gUejH-9gpPtR-XzrMMD-bqn8Qs-bDa1AK-oV87g2-bqn8SE-7hKg3v-CyDj5-bDgHKF-ppTzHf-84Czrj-dWf3MY-eDXW3i-5nTPZb-oaFrev-bqf6Rw-58EpAQ-5bd2t8-9eyUFb-5zNBi9-6geKFz-ngaqHa-6zDJtt-bvJrAQ-28v4k1Y-6s2qrs-3fPsLz-hDNitm-4nfhZC-7dZYt1-PUTxVi-4nuP2y-bDgdVg-96HPjm-bce6J8-5Mnhy
|
||||
[2]: https://creativecommons.org/licenses/by/2.0/legalcode
|
||||
[4]: https://www.facebook.com/NetworkWorld/
|
||||
[5]: https://www.linkedin.com/company/network-world
|
Loading…
Reference in New Issue
Block a user