Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2019-10-11 10:32:12 +08:00
commit 6bf2fbab86
4 changed files with 605 additions and 115 deletions

View File

@ -0,0 +1,115 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-11445-1.html)
[#]: 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 提供了许多用于查找、计数和重命名文件的命令。这有一些有用的选择。
![](https://img.linux.net.cn/data/attachment/album/201910/11/101136ei4sslezne7esyis.jpg)
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` 会递归到子目录中,并将这些子目录中的文件与当前目录中的文件一起计数。在此命令中,我们计数一个特定用户(`username`)的家目录中的文件。根据家目录的权限,这可能需要使用 `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 命令行提供了许多用于处理文件的有用选择。请提出你认为特别有用的其他命令。
--------------------------------------------------------------------------------
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)
校对:[wxy](https://github.com/wxy)
本文由 [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://images.idgesg.net/images/article/2019/09/file_key-100811696-large.jpg
[2]: https://creativecommons.org/licenses/by/2.0/legalcode
[4]: https://www.facebook.com/NetworkWorld/
[5]: https://www.linkedin.com/company/network-world

View File

@ -0,0 +1,76 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Things You Should Know If You Want to Become a Machine Learning Engineer)
[#]: via: (https://opensourceforu.com/2019/10/things-you-should-know-if-you-want-to-become-a-machine-learning-engineer/)
[#]: author: (Samantha https://opensourceforu.com/author/samantha/)
Things You Should Know If You Want to Become a Machine Learning Engineer
======
[![][1]][2]
_Machine learning is the next big thing in the market. Have you seen machines that do activities without any human involvement? This is what machine learning engineers do. They develop machines and systems which can learn and apply knowledge._
**How is artificial intelligence changing the job scenario for machine learning engineers?**
Artificial intelligence and machine learning have been successful in touching almost every aspect of our daily life. It may be the voice-activated virtual assistants like Siri and Alexa, or Predictive technologies used by companies like Netflix and Amazon for a better understanding of the customers.
Artificial intelligence makes the computer do the tasks which earlier needed human intelligence and machine learning is about building the algorithm for the machines which helps them to identify patterns and thus give a better insight into the data. Countries around the world are continuously working on strategies and initiatives to guide the development of artificial intelligence.
Lately, organizations from almost every sector are investing in AI tools and techniques, thus boosting their companies. Currently, AI investments are being dominated by large tech companies like Baidu, Microsoft, Google, Apple, Facebook, and so on. And almost 10%-30% of non-tech companies are adopting artificial intelligence, depending upon their industry.
There has been a considerable advancement in the automobile industry with the implementation of Artificial intelligence with vehicles. Self-driving cars were something impossible without IoT working closely with AI. Then there is the Facial recognition feature by Google, which helps to identify a person using digital images or patterns. These technologies are changing the way people have expected their life to be.
As per a recent [_study_][3], Artificial intelligence will be creating almost 58 million new jobs by 2022, giving a major shift in quality, location, and permanency the new jobs. BY 2025 machines will be taking over the work tasks that are being performed by humans by almost 71%, with the human workforce focusing more on productive tasks. This creates the need for reskilling and upskilling of the current workforce.
In a current report, the top decision-makers of IT/ITES observed that Machine learning and other AI-powered solutions would play a major role in shaping future workplaces. With the latest technological advancements, the tech companies are on the lookout for talents equipped with a better understanding of these technologies.
Here are some of the skills needed for becoming a machine learning engineer.
**Programming skills:**
Machine learning calls for a stronghold over programming and software development skills. Its all about creating dynamic algorithms. Being clear with the fundamentals of analysis and design can be an added advantage for you. Here are the skills that you should be acquainted with:
* **Fundamentals of Programming and CS: **Machine learning involves computation of huge sets of data which requires knowledge on the fundamentals concepts such as computer architecture, data structures, algorithms, etc. The basics of stacks, b-trees, sort logos, or the parallel programming problems come in handy when we talk about the fundamentals.
* **Software design: **Being a machine learning engineer, you will be creating algorithms and systems to integrate with existing ecosystems other software components. And for this, a stronghold over in Application Programming Interfaces (APIs) like web APIs, static and dynamic libraries, etc. are essential for sustenance in the future.
* **Programming languages: **Machine learning is known for its versatility and is not bound to any specific language. All it needs is the required components and features, and you can virtually use any language if it satisfies the above condition. ML libraries have got different programming languages, and each language can be used for a different task.
* **Python: **One of the popular languages used among machine learning engineers is Python. It has got many useful libraries like NumPy, SciPy, and Pandals which help in the efficient processing of data and better scientific computing. It has got some specialized libraries like Scikit-learn, Theano, and TensorFlow, which allow learning algorithms using different computing platforms.
* **R Language: **Developed by Ross Ihaka and Robert Gentleman, this one of the best languages used for machine learning tasks. Coming with a large number of algorithms and statistical models, it is specially tailored for data mining and statistical computing.
* **C/C++:** C/C++ use is pretty much lower when we talk about the programming languages needed for machine learning. But it cannot be as it is used to program the infrastructure and mechanics of machine learning. In fact, a number of ML libraries are actually developed in C/C++ and wrapped around with API calls to make it available for other languages.
Although the language is a bit different from traditional languages, it is not difficult to learn.
**Basic skills needed:**
Machine learning is a combination of math, data science, and software engineering. And no matter how many certifications you have got, but you should be well acquainted with these basic skills to be master in your domain:
* **Data modeling:**
Data modeling is a process used to estimate the structure of a dataset, for finding patterns and at times when data is nonexistent. In machine learning, we have to analyze unstructured data, which relies wholly on data modeling. Data modeling and evaluation concepts are needed for creating sound algorithms.
* **Statistics:**
Statistics is mainly the creation of models from data. Most of the machine learning algorithms are building upon statistical models. It has got various other branches which are also used in the process like analysis of variance and hypothesis testing.
Along with these two, there is one more basic skill, which is of utmost importance Probability. The principles of probability and its derivative techniques like Markov Decision Processes and Bayes Nets help in dealing with the uncertainties and make reliable predictions.
These are many skills which are needed to become a machine learning engineer, and many institutes provide professional [_Machine learning certification course_][4]. They are playing a major role in the rise of AI and efficient machine learning engineers by guiding the participants through the latest advancements and technical approaches in artificial intelligence technologies.
--------------------------------------------------------------------------------
via: https://opensourceforu.com/2019/10/things-you-should-know-if-you-want-to-become-a-machine-learning-engineer/
作者:[Samantha][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://opensourceforu.com/author/samantha/
[b]: https://github.com/lujun9972
[1]: https://i1.wp.com/opensourceforu.com/wp-content/uploads/2016/12/Machine-Learning-Framework.jpg?resize=696%2C611&ssl=1 (machine-learning-framework)
[2]: https://i1.wp.com/opensourceforu.com/wp-content/uploads/2016/12/Machine-Learning-Framework.jpg?fit=1389%2C1219&ssl=1
[3]: https://www.forbes.com/sites/amitchowdhry/2018/09/18/artificial-intelligence-to-create-58-million-new-jobs-by-2022-says-report/#7830694a4d4b
[4]: https://www.simplilearn.com/big-data-and-analytics/machine-learning-certification-training-course

View File

@ -0,0 +1,414 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (The Emacs Series ht.el: The Hash Table Library for Emacs)
[#]: via: (https://opensourceforu.com/2019/10/the-emacs-series-ht-el-the-hash-table-library-for-emacs/)
[#]: author: (Shakthi Kannan https://opensourceforu.com/author/shakthi-kannan/)
The Emacs Series ht.el: The Hash Table Library for Emacs
======
[![][1]][2]
_In this article, we explore the various hash table functions and macros provided by the ht.el library._
The ht.el hash table library for Emacs has been written by Wilfred Hughes. The latest tagged release is version 2.2 and the software is released under the GNU General Public License v3. The source code is available at _<https://github.com/Wilfred/ht.el>_. It provides a comprehensive list of hash table operations and a very consistent API. For example, any mutation function will always return nil.
**Installation**
The Milkypostmans Emacs Lisp Package Archive (MELPA) and Marmalade repositories have ht.el available for installation. You can add the following command to your Emacs init.el configuration file:
```
(require package)
(add-to-list package-archives (“melpa” . “https://melpa.org/packages/”) t)
```
You can then run _M-x package &lt;RET&gt; ht &lt;RET&gt;_ to install the _ht.el_ library. If you are using Cask, then you simply add the following code to your Cask file:
```
(depends-on “ht”)
```
You will need the ht library in your Emacs environment before using the API functions.
```
(require ht)
```
**Usage**
Let us now explore the various API functions provided by the _ht.el_ library. The _ht-create_ function will return a hash table that can be assigned to a hash table variable. You can also verify that the variable is a hash table using the type-of function as shown below:
```
(let ((greetings (ht-create)))
(type-of greetings))
hash-table
```
You can add an item to the hash table using the ht-set! function, which takes the hash table, key and value as arguments. The entries in the hash table can be listed using the _ht-items_ function as illustrated below:
```
(ht-set! hash-table key value) ;; Syntax
(ht-items hash-table) ;; Syntax
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-items greetings))
((“Adam” “Hello Adam!”))
```
The keys present in a hash table can be retrieved using the _ht-keys_ function, while the values in a hash table can be obtained using the _ht-values_ function, as shown in the following examples:
```
(ht-keys hash-table) ;; Syntax
(ht-values hash-table) ;; Syntax
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-keys greetings))
(“Adam”)
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-values greetings))
(“Hello Adam!”)
```
The “ht-clear!” function can be used to clear all the items in a hash-table. For example:
```
(ht-clear! hash-table) ;; Syntax
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-clear! greetings)
(ht-items greetings))
nil
```
An entire hash table can be copied to another hash table using the _ht-copy_ API as shown below:
```
(ht-copy hash-table) ;; Syntax
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-items (ht-copy greetings)))
((“Adam” “Hello Adam!”))
```
The _ht-merge_ function can combine two different hash tables into one. In the following example, the items in the _english_ and _numbers_ hash tables are merged together.
```
(ht-merge hash-table1 hash-table2) ;; Syntax
(let ((english (ht-create))
(numbers (ht-create)))
(ht-set! english “a” “A”)
(ht-set! numbers “1” “One”)
(ht-items (ht-merge english numbers)))
((“1” “One”) (“a” “A”))
```
You can make modifications to an existing hash table. For example, you can remove an item in the hash table using the _ht-remove!_ function, which takes as input a hash table and a key as shown below:
```
(ht-remove hash-table key) ;; Syntax
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-set! greetings “Eve” “Hello Eve!”)
(ht-remove! greetings “Eve”)
(ht-items greetings))
((“Adam” “Hello Adam!”))
```
You can do an in-place modification to an item in the hash table using the _ht-update!_ function. An example is given below:
```
(ht-update! hash-table key value) ;; Syntax
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-update! greetings (ht (“Adam” “Howdy Adam!”)))
(ht-items greetings))
((“Adam” “Howdy Adam!”))
```
A number of predicate functions are available in _ht.el_ that can be used to check for conditions in a hash table. The _ht_? function checks to see if the input argument is a hash table. It returns t if the argument is a hash table and _nil_ otherwise.
```
(ht? hash-table) ;; Syntax
(ht? nil)
nil
(let ((greetings (ht-create)))
(ht? greetings))
t
```
You can verify if a key is present in a hash table using the _ht-contains_? API, which takes a hash table and key as arguments. It returns t if the item exists in the hash table. Otherwise, it simply returns _nil_.
```
(ht-contains? hash-table key) ;; Syntax
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-contains? greetings “Adam”))
t
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-contains? greetings “Eve”))
nil
```
The _ht-empty?_ function can be used to check if the input hash-table is empty or not. A couple of examples are shown below:
```
(ht-empty? hash-table) ;; Syntax
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-empty? greetings))
nil
(let ((greetings (ht-create)))
(ht-empty? greetings))
t
```
The equality check can be used on a couple of hash tables to verify if they are the same, using the _ht-equal_? function as illustrated below:
```
(ht-equal? hash-table1 hash-table2) ;; Syntax
(let ((english (ht-create))
(numbers (ht-create)))
(ht-set! english “a” “A”)
(ht-set! numbers “1” “One”)
(ht-equal? english numbers))
nil
```
A few of the ht.el library functions accept a function as an argument and apply it to the items of the list. For example, the ht-map function takes a function with a key and value as arguments, and applies the function to each item in the hash table. For example:
```
(ht-map function hash-table) ;; Syntax
(let ((numbers (ht-create)))
(ht-set! numbers 1 “One”)
(ht-map (lambda (x y) (* x 2)) numbers))
(2)
```
You can also use the _ht-each_ API to iterate through each item in the hash-table. In the following example, the sum of all the values is calculated and finally printed in the output.
```
(ht-each function hash-table) ;; Syntax
(let ((numbers (ht-create))
(sum 0))
(ht-set! numbers “A” 1)
(ht-set! numbers “B” 2)
(ht-set! numbers “C” 3)
(ht-each (lambda (key value) (setq sum (+ sum value))) numbers)
(print sum))
6
```
The _ht-select_ function can be used to match and pick a specific set of items in the list. For example:
```
(ht-select function hash-table) ;; Syntax
(let ((numbers (ht-create)))
(ht-set! numbers 1 “One”)
(ht-set! numbers 2 “Two”)
(ht-items (ht-select (lambda (x y) (= x 2)) numbers)))
((“2” “Two”))
```
You can also reject a set of values by passing a filter function to the _ht-reject API_, and retrieve those items from the hash table that do not match the predicate function. In the following example, key 2 is rejected and the item with key 1 is returned.
```
(ht-reject function hash-table) ;; Syntax
(let ((numbers (ht-create)))
(ht-set! numbers 1 “One”)
(ht-set! numbers 2 “Two”)
(ht-items (ht-reject (lambda (x y) (= x 2)) numbers)))
((“1” “One”))
```
If you want to mutate the existing hash table and remove the items that match a filter function, you can use the _ht-reject_! function as shown below:
```
(ht-reject! function hash-table) ;; Syntax
(let ((numbers (ht-create)))
(ht-set! numbers 1 “One”)
(ht-set! numbers 2 “Two”)
(ht-reject! (lambda (x y) (= x 2)) numbers)
(ht-items numbers))
((“1” “One”))
```
The _ht-find_ function accepts a function and a hash table, and returns the items that satisfy the input function. For example:
```
(ht-find function hash-table) ;; Syntax
(let ((numbers (ht-create)))
(ht-set! numbers 1 “One”)
(ht-set! numbers 2 “Two”)
(ht-find (lambda (x y) (= x 2)) numbers))
(2 “Two”)
```
You can retrieve the items in the hash table using a specific set of keys with the _ht-select-keys_ API, as illustrated below:
```
(ht-select-keys hash-table keys) ;; Syntax
(let ((numbers (ht-create)))
(ht-set! numbers 1 “One”)
(ht-set! numbers 2 “Two”)
(ht-items (ht-select-keys numbers (1))))
((1 “One”))
```
The following two examples are more comprehensive in using the hash table library functions. The _say-hello_ function returns a greeting based on the name as shown below:
```
(defun say-hello (name)
(let ((greetings (ht-create)))
(ht-set! greetings “Adam” “Hello Adam!”)
(ht-set! greetings “Eve” “Hello Eve!”)
(ht-get greetings name “Hello stranger!”)))
(say-hello “Adam”)
“Hello Adam!”
(say-hello “Eve”)
“Hello Eve!”
(say-hello “Bob”)
“Hello stranger!”
```
The _ht_ macro returns a hash table and we create nested hash tables in the following example:
```
(let ((alphabets (ht (“Greek” (ht (1 (ht (letter “α”)
(name “alpha”)))
(2 (ht (letter “β”)
(name “beta”)))))
(“English” (ht (1 (ht (letter “a”)
(name “A”)))
(2 (ht (letter “b”)
(name “B”))))))))
(ht-get* alphabets “Greek” 1 letter))
“α”
```
**Testing**
The _ht.el_ library has built-in tests that you can execute to validate the API functions. You first need to clone the repository using the following commands:
```
$ git clone git@github.com:Wilfred/ht.el.git
Cloning into ht.el...
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
Receiving objects: 100% (471/471), 74.58 KiB | 658.00 KiB/s, done.
remote: Total 471 (delta 0), reused 1 (delta 0), pack-reused 470
Resolving deltas: 100% (247/247), done.
```
If you do not have Cask, install the same using the instructions provided in the _README_ file at _<https://github.com/cask/cask>_.
You can then change the directory into the cloned _ht.el_ folder and run _cask install_. This will locally install the required dependencies for running the tests.
```
$ cd ht.el/
$ cask install
Loading package information... Select coding system (default utf-8):
done
Package operations: 4 installs, 0 removals
- Installing [ 1/4] dash (2.12.0)... done
- Installing [ 2/4] ert-runner (latest)... done
- Installing [ 3/4] cl-lib (latest)... already present
- Installing [ 4/4] f (latest)... already present
```
A _Makefile_ exists in the top-level directory and you can simply run make to run the tests, as shown below:
```
$ make
rm -f ht.elc
make unit
make[1]: Entering directory /home/guest/ht.el
cask exec ert-runner
.........................................
Ran 41 tests in 0.016 seconds
make[1]: Leaving directory /home/guest/ht.el
make compile
make[1]: Entering directory /home/guest/ht.el
cask exec emacs -Q -batch -f batch-byte-compile ht.el
make[1]: Leaving directory /home/guest/ht.el
make unit
make[1]: Entering directory /home/guest/ht.el
cask exec ert-runner
.........................................
Ran 41 tests in 0.015 seconds
make[1]: Leaving directory /home/guest/ht.el
make clean-elc
make[1]: Entering directory /home/guest/ht.el
rm -f ht.elc
make[1]: Leaving directory /home/guest/ht.el
```
You are encouraged to read the ht.el _README_ file from the GitHub repository at _<https://github.com/Wilfred/ht.el/blob/master/README.md>_ for more information.
--------------------------------------------------------------------------------
via: https://opensourceforu.com/2019/10/the-emacs-series-ht-el-the-hash-table-library-for-emacs/
作者:[Shakthi Kannan][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://opensourceforu.com/author/shakthi-kannan/
[b]: https://github.com/lujun9972
[1]: https://i0.wp.com/opensourceforu.com/wp-content/uploads/2019/10/GPL-3.jpg?resize=696%2C351&ssl=1 (GPL 3)
[2]: https://i0.wp.com/opensourceforu.com/wp-content/uploads/2019/10/GPL-3.jpg?fit=998%2C503&ssl=1

View File

@ -1,115 +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/)
处理 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