mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
[Translated]sources/tech/20150911 10 Useful Linux Command Line Tricks for Newbies--Part 2.md
This commit is contained in:
parent
a001f8da46
commit
2ace123685
@ -1,251 +0,0 @@
|
|||||||
ictlyh Translating
|
|
||||||
10 Useful Linux Command Line Tricks for Newbies – Part 2
|
|
||||||
================================================================================
|
|
||||||
I remember when I first started using Linux and I was used to the graphical interface of Windows, I truly hated the Linux terminal. Back then I was finding the commands hard to remember and proper use of each one of them. With time I realised the beauty, flexibility and usability of the Linux terminal and to be honest a day doesn’t pass without using. Today, I would like to share some useful tricks and tips for Linux new comers to ease their transition to Linux or simply help them learn something new (hopefully).
|
|
||||||
|
|
||||||
![10 Linux Commandline Tricks for Newbies](http://www.tecmint.com/wp-content/uploads/2015/09/10-Linux-Commandline-Tricks.jpg)
|
|
||||||
|
|
||||||
10 Linux Commandline Tricks – Part 2
|
|
||||||
|
|
||||||
- [5 Interesting Command Line Tips and Tricks in Linux – Part 1][1]
|
|
||||||
- [5 Useful Commands to Manage Linux File Types – Part 3][2]
|
|
||||||
|
|
||||||
This article intends to show you some useful tricks how to use the Linux terminal like a pro with minimum amount of skills. All you need is a Linux terminal and some free time to test these commands.
|
|
||||||
|
|
||||||
### 1. Find the right command ###
|
|
||||||
|
|
||||||
Executing the right command can be vital for your system. However in Linux there are so many different command lines that they are often hard to remember. So how do you search for the right command you need? The answer is apropos. All you need to run is:
|
|
||||||
|
|
||||||
# apropos <description>
|
|
||||||
|
|
||||||
Where you should change the “description” with the actual description of the command you are looking for. Here is a good example:
|
|
||||||
|
|
||||||
# apropos "list directory"
|
|
||||||
|
|
||||||
dir (1) - list directory contents
|
|
||||||
ls (1) - list directory contents
|
|
||||||
ntfsls (8) - list directory contents on an NTFS filesystem
|
|
||||||
vdir (1) - list directory contents
|
|
||||||
|
|
||||||
On the left you can see the commands and on the right their description.
|
|
||||||
|
|
||||||
### 2. Execute Previous Command ###
|
|
||||||
|
|
||||||
Many times you will need to execute the same command over and over again. While you can repeatedly press the Up key on your keyboard, you can use the history command instead. This command will list all commands you entered since you launched the terminal:
|
|
||||||
|
|
||||||
# history
|
|
||||||
|
|
||||||
1 fdisk -l
|
|
||||||
2 apt-get install gnome-paint
|
|
||||||
3 hostname tecmint.com
|
|
||||||
4 hostnamectl tecmint.com
|
|
||||||
5 man hostnamectl
|
|
||||||
6 hostnamectl --set-hostname tecmint.com
|
|
||||||
7 hostnamectl -set-hostname tecmint.com
|
|
||||||
8 hostnamectl set-hostname tecmint.com
|
|
||||||
9 mount -t "ntfs" -o
|
|
||||||
10 fdisk -l
|
|
||||||
11 mount -t ntfs-3g /dev/sda5 /mnt
|
|
||||||
12 mount -t rw ntfs-3g /dev/sda5 /mnt
|
|
||||||
13 mount -t -rw ntfs-3g /dev/sda5 /mnt
|
|
||||||
14 mount -t ntfs-3g /dev/sda5 /mnt
|
|
||||||
15 mount man
|
|
||||||
16 man mount
|
|
||||||
17 mount -t -o ntfs-3g /dev/sda5 /mnt
|
|
||||||
18 mount -o ntfs-3g /dev/sda5 /mnt
|
|
||||||
19 mount -ro ntfs-3g /dev/sda5 /mnt
|
|
||||||
20 cd /mnt
|
|
||||||
...
|
|
||||||
|
|
||||||
As you will see from the output above, you will receive a list of all commands that you have ran. On each line you have number indicating the row in which you have entered the command. You can recall that command by using:
|
|
||||||
|
|
||||||
!#
|
|
||||||
|
|
||||||
Where # should be changed with the actual number of the command. For better understanding, see the below example:
|
|
||||||
|
|
||||||
!501
|
|
||||||
|
|
||||||
Is equivalent to:
|
|
||||||
|
|
||||||
# history
|
|
||||||
|
|
||||||
### 3. Use midnight Commander ###
|
|
||||||
|
|
||||||
If you are not used to using commands such cd, cp, mv, rm than you can use the midnight command. It is an easy to use visual shell in which you can also use mouse:
|
|
||||||
|
|
||||||
![Midnight Commander in Action](http://www.tecmint.com/wp-content/uploads/2015/09/mc-command.jpg)
|
|
||||||
|
|
||||||
Midnight Commander in Action
|
|
||||||
|
|
||||||
Thanks to the F1 – F12 keys, you can easy perform different tasks. Simply check the legend at the bottom. To select a file or folder click the “Insert” button.
|
|
||||||
|
|
||||||
In short the midnight command is called “mc“. To install mc on your system simply run:
|
|
||||||
|
|
||||||
$ sudo apt-get install mc [On Debian based systems]
|
|
||||||
|
|
||||||
----------
|
|
||||||
|
|
||||||
# yum install mc [On Fedora based systems]
|
|
||||||
|
|
||||||
Here is a simple example of using midnight commander. Open mc by simply typing:
|
|
||||||
|
|
||||||
# mc
|
|
||||||
|
|
||||||
Now use the TAB button to switch between windows – left and right. I have a LibreOffice file that I will move to “Software” folder:
|
|
||||||
|
|
||||||
![Midnight Commander Move Files](http://www.tecmint.com/wp-content/uploads/2015/09/Midnight-Commander-Move-Files.jpg)
|
|
||||||
|
|
||||||
Midnight Commander Move Files
|
|
||||||
|
|
||||||
To move the file in the new directory press F6 button on your keyboard. MC will now ask you for confirmation:
|
|
||||||
|
|
||||||
![Move Files to New Directory](http://www.tecmint.com/wp-content/uploads/2015/09/Move-Files-to-new-Directory.png)
|
|
||||||
|
|
||||||
Move Files to New Directory
|
|
||||||
|
|
||||||
Once confirmed, the file will be moved in the new destination directory.
|
|
||||||
|
|
||||||
Read More: [How to Use Midnight Commander File Manager in Linux][4]
|
|
||||||
|
|
||||||
### 4. Shutdown Computer at Specific Time ###
|
|
||||||
|
|
||||||
Sometimes you will need to shutdown your computer some hours after your work hours have ended. You can configure your computer to shut down at specific time by using:
|
|
||||||
|
|
||||||
$ sudo shutdown 21:00
|
|
||||||
|
|
||||||
This will tell your computer to shut down at the specific time you have provided. You can also tell the system to shutdown after specific amount of minutes:
|
|
||||||
|
|
||||||
$ sudo shutdown +15
|
|
||||||
|
|
||||||
That way the system will shut down in 15 minutes.
|
|
||||||
|
|
||||||
### 5. Show Information about Known Users ###
|
|
||||||
|
|
||||||
You can use a simple command to list your Linux system users and some basic information about them. Simply use:
|
|
||||||
|
|
||||||
# lslogins
|
|
||||||
|
|
||||||
This should bring you the following output:
|
|
||||||
|
|
||||||
UID USER PWD-LOCK PWD-DENY LAST-LOGIN GECOS
|
|
||||||
0 root 0 0 Apr29/11:35 root
|
|
||||||
1 bin 0 1 bin
|
|
||||||
2 daemon 0 1 daemon
|
|
||||||
3 adm 0 1 adm
|
|
||||||
4 lp 0 1 lp
|
|
||||||
5 sync 0 1 sync
|
|
||||||
6 shutdown 0 1 Jul19/10:04 shutdown
|
|
||||||
7 halt 0 1 halt
|
|
||||||
8 mail 0 1 mail
|
|
||||||
10 uucp 0 1 uucp
|
|
||||||
11 operator 0 1 operator
|
|
||||||
12 games 0 1 games
|
|
||||||
13 gopher 0 1 gopher
|
|
||||||
14 ftp 0 1 FTP User
|
|
||||||
23 squid 0 1
|
|
||||||
25 named 0 1 Named
|
|
||||||
27 mysql 0 1 MySQL Server
|
|
||||||
47 mailnull 0 1
|
|
||||||
48 apache 0 1 Apache
|
|
||||||
...
|
|
||||||
|
|
||||||
### 6. Search for Files ###
|
|
||||||
|
|
||||||
Searching for files can sometimes be not as easy as you think. A good example for searching for files is:
|
|
||||||
|
|
||||||
# find /home/user -type f
|
|
||||||
|
|
||||||
This command will search for all files located in /home/user. The find command is extremely powerful one and you can pass more options to it to make your search even more detailed. If you want to search for files larger than given size, you can use:
|
|
||||||
|
|
||||||
# find . -type f -size 10M
|
|
||||||
|
|
||||||
The above command will search from current directory for all files that are larger than 10 MB. Make sure not to run the command from the root directory of your Linux system as this may cause high I/O on your machine.
|
|
||||||
|
|
||||||
One of the most frequently used combinations that I use find with is “exec” option, which basically allows you to run some actions on the results of the find command.
|
|
||||||
|
|
||||||
For example, lets say that we want to find all files in a directory and change their permissions. This can be easily done with:
|
|
||||||
|
|
||||||
# find /home/user/files/ -type f -exec chmod 644 {} \;
|
|
||||||
|
|
||||||
The above command will search for all files in the specified directory recursively and will executed chmod command on the found files. I am sure you will find many more uses on this command in future, for now read [35 Examples of Linux ‘find’ Command and Usage][5].
|
|
||||||
|
|
||||||
### 7. Build Directory Trees with one Command ###
|
|
||||||
|
|
||||||
You probably know that you can create new directories by using the mkdir command. So if you want to create a new folder you will run something like this:
|
|
||||||
|
|
||||||
# mkdir new_folder
|
|
||||||
|
|
||||||
But what, if you want to create 5 subfolders within that folder? Running mkdir 5 times in a row is not a good solution. Instead you can use -p option like that:
|
|
||||||
|
|
||||||
# mkdir -p new_folder/{folder_1,folder_2,folder_3,folder_4,folder_5}
|
|
||||||
|
|
||||||
In the end you should have 5 folders located in new_folder:
|
|
||||||
|
|
||||||
# ls new_folder/
|
|
||||||
|
|
||||||
folder_1 folder_2 folder_3 folder_4 folder_5
|
|
||||||
|
|
||||||
### 8. Copy File into Multiple Directories ###
|
|
||||||
|
|
||||||
File copying is usually performed with the cp command. Copying a file usually looks like this:
|
|
||||||
|
|
||||||
# cp /path-to-file/my_file.txt /path-to-new-directory/
|
|
||||||
|
|
||||||
Now imagine that you need to copy that file in multiple directories:
|
|
||||||
|
|
||||||
# cp /home/user/my_file.txt /home/user/1
|
|
||||||
# cp /home/user/my_file.txt /home/user/2
|
|
||||||
# cp /home/user/my_file.txt /home/user/3
|
|
||||||
|
|
||||||
This is a bit absurd. Instead you can solve the problem with a simple one line command:
|
|
||||||
|
|
||||||
# echo /home/user/1/ /home/user/2/ /home/user/3/ | xargs -n 1 cp /home/user/my_file.txt
|
|
||||||
|
|
||||||
### 9. Deleting Larger Files ###
|
|
||||||
|
|
||||||
Sometimes files can grow extremely large. I have seen cases where a single log file went over 250 GB large due to poor administrating skills. Removing the file with rm utility might not be sufficient in such cases due to the fact that there is extremely large amount of data that needs to be removed. The operation will be a “heavy” one and should be avoided. Instead, you can go with a really simple solution:
|
|
||||||
|
|
||||||
# > /path-to-file/huge_file.log
|
|
||||||
|
|
||||||
Where of course you will need to change the path and the file names with the exact ones to match your case. The above command will simply write an empty output to the file. In more simpler words it will empty the file without causing high I/O on your system.
|
|
||||||
|
|
||||||
### 10. Run Same Command on Multiple Linux Servers ###
|
|
||||||
|
|
||||||
Recently one of our readers asked in our [LinuxSay forum][6], how to execute single command to multiple Linux boxes at once using SSH. He had his machines IP addresses looking like this:
|
|
||||||
|
|
||||||
10.0.0.1
|
|
||||||
10.0.0.2
|
|
||||||
10.0.0.3
|
|
||||||
10.0.0.4
|
|
||||||
10.0.0.5
|
|
||||||
|
|
||||||
So here is a simple solution of this issue. Collect the IP addresses of the servers in a one file called list.txt one under other just as shown above. Then you can run:
|
|
||||||
|
|
||||||
# for in $i(cat list.txt); do ssh user@$i 'bash command'; done
|
|
||||||
|
|
||||||
In the above example you will need to change “user” with the actual user with which you will be logging and “bash command” with the actual bash command you wish to execute. The method is better working when you are [using passwordless authentication with SSH key][7] to your machines as that way you will not need to enter the password for your user over and over again.
|
|
||||||
|
|
||||||
Note that you may need to pass some additional parameters to the SSH command depending on your Linux boxes setup.
|
|
||||||
|
|
||||||
### Conclusion ###
|
|
||||||
|
|
||||||
The above examples are really simple ones and I hope they have helped you to find some of the beauty of Linux and how you can easily perform different operations that can take much more time on other operating systems.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: http://www.tecmint.com/10-useful-linux-command-line-tricks-for-newbies/
|
|
||||||
|
|
||||||
作者:[Marin Todorov][a]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]:http://www.tecmint.com/author/marintodorov89/
|
|
||||||
[1]:http://www.tecmint.com/5-linux-command-line-tricks/
|
|
||||||
[2]:http://www.tecmint.com/manage-file-types-and-set-system-time-in-linux/
|
|
||||||
[3]:http://www.tecmint.com/history-command-examples/
|
|
||||||
[4]:http://www.tecmint.com/midnight-commander-a-console-based-file-manager-for-linux/
|
|
||||||
[5]:http://www.tecmint.com/35-practical-examples-of-linux-find-command/
|
|
||||||
[6]:http://www.linuxsay.com/
|
|
||||||
[7]:http://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
|
|
@ -0,0 +1,253 @@
|
|||||||
|
给新手的 10 个有用 Linux 命令行技巧 - 第二部分
|
||||||
|
================================================================================
|
||||||
|
我记得我第一次使用 Linux 的时候,我还习惯于 Windows 的图形界面,我真的很讨厌 Linux 终端。那时候我觉得命令难以记忆,不能正确使用它们。随着时间推移,我意识到了 Linux 终端的优美、灵活和可用性,说实话,我没有一天不使用它。今天,我很高兴和刚开始接触 Linux 的人一起来分享一些有用的技巧和提示,希望能帮助他们更好的向 Linux 过度,并帮助他们学到一些新的东西(希望如此)。
|
||||||
|
|
||||||
|
![给新手的 10 个命令行技巧](http://www.tecmint.com/wp-content/uploads/2015/09/10-Linux-Commandline-Tricks.jpg)
|
||||||
|
|
||||||
|
10 个 Linux 命令行技巧 - 第二部分
|
||||||
|
|
||||||
|
|
||||||
|
- [Linux 中 5 个有趣的命令行提示和技巧 - 第一部分][1]
|
||||||
|
- [管理 Linux 文件类型的 5 个有用命令 – 第三部分][2]
|
||||||
|
|
||||||
|
这篇文章希望向你展示一些不需要很高的技术而可以像一个高手一样使用 Linux 终端的有用技巧。你只需要一个 Linux 终端和一些自由时间来体会这些命令。
|
||||||
|
|
||||||
|
### 1. 找到正确的命令 ###
|
||||||
|
|
||||||
|
执行正确的命令对你的系统来说非常重要。然而在 Linux 中有很多通常难以记忆的不同的命令行。那么怎样才能找到你需要的正确命令呢?答案是 apropos。你只需要运行:
|
||||||
|
|
||||||
|
# apropos <description>
|
||||||
|
|
||||||
|
其中你要用真正描述你要查找的命令的语句代替 “description”。这里有一个例子:
|
||||||
|
|
||||||
|
# apropos "list directory"
|
||||||
|
|
||||||
|
dir (1) - list directory contents
|
||||||
|
ls (1) - list directory contents
|
||||||
|
ntfsls (8) - list directory contents on an NTFS filesystem
|
||||||
|
vdir (1) - list directory contents
|
||||||
|
|
||||||
|
左边你看到的是命令,右边是它们的描述。
|
||||||
|
|
||||||
|
### 2. 执行之前的命令 ###
|
||||||
|
|
||||||
|
很多时候你需要一遍又一遍执行相同的命令。尽管你可以重复按你键盘上的 Up 键,你也可以用 history 命令。这个命令会列出自从你上次启动终端以来所有输入过的命令:
|
||||||
|
|
||||||
|
# history
|
||||||
|
|
||||||
|
1 fdisk -l
|
||||||
|
2 apt-get install gnome-paint
|
||||||
|
3 hostname tecmint.com
|
||||||
|
4 hostnamectl tecmint.com
|
||||||
|
5 man hostnamectl
|
||||||
|
6 hostnamectl --set-hostname tecmint.com
|
||||||
|
7 hostnamectl -set-hostname tecmint.com
|
||||||
|
8 hostnamectl set-hostname tecmint.com
|
||||||
|
9 mount -t "ntfs" -o
|
||||||
|
10 fdisk -l
|
||||||
|
11 mount -t ntfs-3g /dev/sda5 /mnt
|
||||||
|
12 mount -t rw ntfs-3g /dev/sda5 /mnt
|
||||||
|
13 mount -t -rw ntfs-3g /dev/sda5 /mnt
|
||||||
|
14 mount -t ntfs-3g /dev/sda5 /mnt
|
||||||
|
15 mount man
|
||||||
|
16 man mount
|
||||||
|
17 mount -t -o ntfs-3g /dev/sda5 /mnt
|
||||||
|
18 mount -o ntfs-3g /dev/sda5 /mnt
|
||||||
|
19 mount -ro ntfs-3g /dev/sda5 /mnt
|
||||||
|
20 cd /mnt
|
||||||
|
...
|
||||||
|
|
||||||
|
正如你上面看到的,你会得到一个你运行过的命令的列表。每一行中有一个数字表示你在第几行输入了命令。你可以通过以下方法重新调用该命令:
|
||||||
|
|
||||||
|
!#
|
||||||
|
|
||||||
|
其中要用命令的实际编号代替 #。为了更好的理解,请看下面的例子:
|
||||||
|
|
||||||
|
!501
|
||||||
|
|
||||||
|
等价于:
|
||||||
|
|
||||||
|
# history
|
||||||
|
|
||||||
|
### 3. 使用 midnight 命令 ###
|
||||||
|
|
||||||
|
如果你不习惯使用类似 cd、cp、mv、rm 等命令,你可以使用 midnight 命令。它是一个简单的可视化 shell,你可以在上面使用鼠标:
|
||||||
|
|
||||||
|
|
||||||
|
![Midnight 命令](http://www.tecmint.com/wp-content/uploads/2015/09/mc-command.jpg)
|
||||||
|
|
||||||
|
Midnight 命令
|
||||||
|
|
||||||
|
多亏了 F1 到 F12 键,你可以轻易地执行不同任务。只需要在底部选择对应的命令。要选择文件或者目录,点击 “Insert” 按钮。
|
||||||
|
|
||||||
|
简而言之 midnight 就是所谓的 “mc”。要安装 mc,只需要运行:
|
||||||
|
|
||||||
|
$ sudo apt-get install mc [On Debian based systems]
|
||||||
|
|
||||||
|
----------
|
||||||
|
|
||||||
|
# yum install mc [On Fedora based systems]
|
||||||
|
|
||||||
|
下面是一个使用 midnight 命令器的简单例子。通过输入以下命令打开 mc:
|
||||||
|
|
||||||
|
# mc
|
||||||
|
|
||||||
|
现在使用 TAB 键选择不同的窗口 - 左和右。我有一个想要移动到 “Software” 目录的 LibreOffice 文件:
|
||||||
|
|
||||||
|
![Midnight 命令移动文件](http://www.tecmint.com/wp-content/uploads/2015/09/Midnight-Commander-Move-Files.jpg)
|
||||||
|
|
||||||
|
Midnight 命令移动文件
|
||||||
|
|
||||||
|
按 F6 按钮移动文件到新的目录。MC 会请求你确认:
|
||||||
|
|
||||||
|
![移动文件到新目录](http://www.tecmint.com/wp-content/uploads/2015/09/Move-Files-to-new-Directory.png)
|
||||||
|
|
||||||
|
移动文件到新目录
|
||||||
|
|
||||||
|
确认了之后,文件就会被移动到新的目标目录。
|
||||||
|
|
||||||
|
扩展阅读:[如何在 Linux 中使用 Midnight 命令文件管理器][4]
|
||||||
|
|
||||||
|
### 4. 在指定时间关闭计算机 ###
|
||||||
|
|
||||||
|
有时候你需要在结束工作几个小时后再关闭计算机。你可以通过使用下面的命令在指定时间关闭你的计算机:
|
||||||
|
|
||||||
|
$ sudo shutdown 21:00
|
||||||
|
|
||||||
|
这会告诉你在你指定的时间关闭计算机。你也可以告诉系统在指定分钟后关闭:
|
||||||
|
|
||||||
|
$ sudo shutdown +15
|
||||||
|
|
||||||
|
这表示计算机会在 15 分钟后关闭。
|
||||||
|
|
||||||
|
### 5. 显示已知用户的信息 ###
|
||||||
|
|
||||||
|
你可以使用一个简单的命令列出你 Linux 系统的用户以及一些关于它们的基本信息。
|
||||||
|
|
||||||
|
# lslogins
|
||||||
|
|
||||||
|
这会输出下面的结果:
|
||||||
|
|
||||||
|
UID USER PWD-LOCK PWD-DENY LAST-LOGIN GECOS
|
||||||
|
0 root 0 0 Apr29/11:35 root
|
||||||
|
1 bin 0 1 bin
|
||||||
|
2 daemon 0 1 daemon
|
||||||
|
3 adm 0 1 adm
|
||||||
|
4 lp 0 1 lp
|
||||||
|
5 sync 0 1 sync
|
||||||
|
6 shutdown 0 1 Jul19/10:04 shutdown
|
||||||
|
7 halt 0 1 halt
|
||||||
|
8 mail 0 1 mail
|
||||||
|
10 uucp 0 1 uucp
|
||||||
|
11 operator 0 1 operator
|
||||||
|
12 games 0 1 games
|
||||||
|
13 gopher 0 1 gopher
|
||||||
|
14 ftp 0 1 FTP User
|
||||||
|
23 squid 0 1
|
||||||
|
25 named 0 1 Named
|
||||||
|
27 mysql 0 1 MySQL Server
|
||||||
|
47 mailnull 0 1
|
||||||
|
48 apache 0 1 Apache
|
||||||
|
...
|
||||||
|
|
||||||
|
### 6. 查找文件 ###
|
||||||
|
### 6. Search for Files ###
|
||||||
|
|
||||||
|
查找文件有时候并不像你想象的那么简单。一个搜索文件的好例子是:
|
||||||
|
|
||||||
|
# find /home/user -type f
|
||||||
|
|
||||||
|
这个命令会搜索 /home/user 目录下的所有文件。find 命令真的很强大,你可以传递更多选项给它使得你的搜索更加详细。如果你想搜索比特定大小大的文件,可以使用:
|
||||||
|
|
||||||
|
# find . -type f -size 10M
|
||||||
|
|
||||||
|
上面的命令会搜索当前目录中所有大于 10M 的文件。确保不要在你 Linux 系统的根目录运行该命令,因为这可能导致你的机器 I/O 瓶颈。
|
||||||
|
|
||||||
|
我最经常和 find 命令一起使用的选项之一是 “exec”,这允许你对 find 命令的结果运行一些操作。
|
||||||
|
|
||||||
|
例如,假如我们想查找一个目录中的所有文件并更改权限。可以通过以下简单命令完成:
|
||||||
|
|
||||||
|
# find /home/user/files/ -type f -exec chmod 644 {} \;
|
||||||
|
|
||||||
|
上面的命令会递归搜索指定目录内的所有文件,并对找到的文件执行 chmod 命令。推荐你阅读 [35 个 Linux ‘find’ 命令的使用方法][5],我肯定你会发现这个命令更多的使用方法。
|
||||||
|
|
||||||
|
### 7. 用一个命令创建目录树 ###
|
||||||
|
|
||||||
|
你很可能知道可以使用 mkdir 命令创建新的目录。因此如果你想创建一个新的目录,你可能会运行:
|
||||||
|
|
||||||
|
# mkdir new_folder
|
||||||
|
|
||||||
|
但如果你想在该目录下创建 5 个子目录呢?运行 5 次 mkdir 命令并非是一个好的选择。相反你可以类似下面这样使用 -p 选项:
|
||||||
|
|
||||||
|
# mkdir -p new_folder/{folder_1,folder_2,folder_3,folder_4,folder_5}
|
||||||
|
|
||||||
|
最后你会在 new_folder 中有 5 个目录:
|
||||||
|
|
||||||
|
# ls new_folder/
|
||||||
|
|
||||||
|
folder_1 folder_2 folder_3 folder_4 folder_5
|
||||||
|
|
||||||
|
### 8. 复制文件到多个目录 ###
|
||||||
|
|
||||||
|
通常使用 cp 命令进行文件复制。复制文件通常看起来类似:
|
||||||
|
|
||||||
|
# cp /path-to-file/my_file.txt /path-to-new-directory/
|
||||||
|
|
||||||
|
现在假设你需要复制该文件到多个目录:
|
||||||
|
|
||||||
|
# cp /home/user/my_file.txt /home/user/1
|
||||||
|
# cp /home/user/my_file.txt /home/user/2
|
||||||
|
# cp /home/user/my_file.txt /home/user/3
|
||||||
|
|
||||||
|
这有点荒唐。相反,你可以用简单的一行命令解决问题:
|
||||||
|
|
||||||
|
# echo /home/user/1/ /home/user/2/ /home/user/3/ | xargs -n 1 cp /home/user/my_file.txt
|
||||||
|
|
||||||
|
### 9. 删除大文件 ###
|
||||||
|
|
||||||
|
有时候文件可能会变得很大。我看过由于缺乏管理技能一个日志文件就超过 250G 的例子。用 rm 命令可能不足以删除该文件,因为有大量的数据需要移除。应该避免这个很“笨重”的操作。相反,你可以使用一个简单的方法解决这个问题:
|
||||||
|
|
||||||
|
# > /path-to-file/huge_file.log
|
||||||
|
|
||||||
|
当然你需要根据你实际情况替换路径和文件名。上面的命令写一个空输出到该文件。用更简单的话说它会清空文件而不会导致你的系统产生大的 I/O 消耗。
|
||||||
|
|
||||||
|
### 10. 在多个 Linux 服务器上运行相同命令 ###
|
||||||
|
|
||||||
|
最近我们的一个读者在 [LinuxSay 论坛][6]提问说如何通过 ssh 在多个 Linux 服务器上执行一个命令。他机器的 IP 地址是:
|
||||||
|
|
||||||
|
10.0.0.1
|
||||||
|
10.0.0.2
|
||||||
|
10.0.0.3
|
||||||
|
10.0.0.4
|
||||||
|
10.0.0.5
|
||||||
|
|
||||||
|
这里有一个简单的解决方法。收集服务器的 IP 地址到文件 list.txt 中,像上面那样一行一个。然后运行:
|
||||||
|
|
||||||
|
# for in $i(cat list.txt); do ssh user@$i 'bash command'; done
|
||||||
|
|
||||||
|
上面的命令中你需要用实际登录的用户替换 “user”,用你希望执行的实际命令替换 “bash command”。这个方法非常适用于通过[使用 SSH 密钥进行无密码验证][7],因为这样你不需要每次都为用户输入密码。
|
||||||
|
|
||||||
|
注意取决于你 Linux 系统的设置,你可能还需要传递一些额外的参数给 SSH 命令。
|
||||||
|
|
||||||
|
### 总结 ###
|
||||||
|
|
||||||
|
上面的例子都很简单,我希望它们能帮助你发现 Linux 的优美之处,你如何能简单实现在其它操作系统上需要更多时间的不同操作。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: http://www.tecmint.com/10-useful-linux-command-line-tricks-for-newbies/
|
||||||
|
|
||||||
|
作者:[Marin Todorov][a]
|
||||||
|
译者:[ictlyh](http://mutouxiaogui.cn/blog/)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]:http://www.tecmint.com/author/marintodorov89/
|
||||||
|
[1]:http://www.tecmint.com/5-linux-command-line-tricks/
|
||||||
|
[2]:http://www.tecmint.com/manage-file-types-and-set-system-time-in-linux/
|
||||||
|
[3]:http://www.tecmint.com/history-command-examples/
|
||||||
|
[4]:http://www.tecmint.com/midnight-commander-a-console-based-file-manager-for-linux/
|
||||||
|
[5]:http://www.tecmint.com/35-practical-examples-of-linux-find-command/
|
||||||
|
[6]:http://www.linuxsay.com/
|
||||||
|
[7]:http://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
|
Loading…
Reference in New Issue
Block a user