对高级 Linux 用户有用的 20 个命令
31. Command: rm
The command ‘rm‘ stands for remove. rm is used to remove files (s) and directories.
?
Removing a directory?
root@tecmint:~# rm PassportApplicationForm_Main_English_V1.0rm: cannot remove `PassportApplicationForm_Main_English_V1.0': Is a directory?
?
The directory can’t be removed simply by ‘rm‘ command, you have to use ‘-rf‘ switch along with ‘rm‘.
root@tecmint:~# rm -rf PassportApplicationForm_Main_English_V1.0
?
Warning: “rm -rf” command is a destructive command if accidently you make it to the wrong directory. Once you ‘rm -rf‘ a directory all the files and the directory itself is lost forever, all of a sudden. Use it with caution.
?
32. Command: echoecho as the name suggest echoes a text on the standard output. It has nothing to do with shell, nor does shell reads the output of echo command. However in an interactive script, echo passes the message to the user through terminal. It is one of the command that is commonly used in scripting, interactive scripting.
root@tecmint:~# echo "Tecmint.com is a very good website" Tecmint.com is a very good website
?
creating a small interactive script1. create a file, named ‘interactive_shell.sh‘ on desktop. (Remember ‘.sh‘ extension is must).
2. copy and paste the below script, exactly same, as below.
#!/bin/bash echo "Please enter your name:" read name echo "Welcome to Linux $name"
?
Next, set execute permission and run the script.
root@tecmint:~# chmod 777 interactive_shell.sh
?
root@tecmint:~# ./interactive_shell.shPlease enter your name:Ravi SaiveWelcome to Linux Ravi Saive
?
Note: ‘#!/bin/bash‘ tells the shell that it is an script an it is always a good idea to include it at the top of script. ‘read‘ reads the given input.
?
33. Command: passwdThis is an important command that is useful for changing own password in terminal. Obviously you need to know your current passowrd for Security reason.
root@tecmint:~# passwd Changing password for tecmint. (current) UNIX password: ******** Enter new UNIX password: ********Retype new UNIX password: ********Password unchanged [Here was passowrd remians unchanged, i.e., new password=old password]Enter new UNIX password: #####Retype new UNIX password:#####
?
34. Command: lprThis command print files named on command line, to named printer.
root@tecmint:~# lpr -P deskjet-4620-series 1-final.pdf
?
Note: The ‘lpq‘ command lets you view the status of a printer (whether it’s up or not), and the jobs (files) waiting to be printed.
?
35. Command: cmpcompare two files of any type and writes the results to the standard output. By default, ‘cmp‘ Returns 0 if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported.
?
To provide examples for this command, lets consider two files:
?
file1.txtroot@tecmint:~# cat file1.txtHi My name is Tecmint
?
file2.txtroot@tecmint:~# cat file2.txtHi My name is tecmint [dot] com
?
Now, let’s compare two files and see output of the command.
root@tecmint:~# cmp file1.txt file2.txt file1.txt file2.txt differ: byte 15, line 1
?
36. Command: wgetWget is a free utility for non-interactive (i.e., can work in background) download of files from the Web. It supports HTTP, HTTPS, FTP protocols and HTTP proxies.
?
Download ffmpeg using wgetroot@tecmint:~# wget http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2--2013-05-22 18:54:52-- http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2Resolving downloads.sourceforge.net (downloads.sourceforge.net)... 216.34.181.59Connecting to downloads.sourceforge.net (downloads.sourceforge.net)|216.34.181.59|:80... connected.HTTP request sent, awaiting response... 302 FoundLocation: http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 [following]--2013-05-22 18:54:54-- http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2Resolving kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)... 92.46.53.163Connecting to kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)|92.46.53.163|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 275557 (269K) [application/octet-stream]Saving to: ‘ffmpeg-php-0.6.0.tbz2’100%[===========================================================================>] 2,75,557 67.8KB/s in 4.0s 2013-05-22 18:55:00 (67.8 KB/s) - ‘ffmpeg-php-0.6.0.tbz2’ saved [275557/275557]
?
37. Command: mountMount is an important command which is used to mount a filesystem that don’t mount itself. You need root permission to mount a device.
?
First run ‘lsblk‘ after plugging-in your filesystem and identify your device and note down you device assigned name.
root@tecmint:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 931.5G 0 disk ├─sda1 8:1 0 923.6G 0 part / ├─sda2 8:2 0 1K 0 part └─sda5 8:5 0 7.9G 0 part [SWAP] sr0 11:0 1 1024M 0 rom sdb 8:16 1 3.7G 0 disk └─sdb1 8:17 1 3.7G 0 part
?
From this screen it was clear that I plugged in a 4 GB pendrive thus ‘sdb1‘ is my filesystem to be mounted. Become a root to perform this operation and change to /dev directory where all the file system is mounted.
root@tecmint:~# suPassword:root@tecmint:~# cd /dev?
Create a directory named anything but should be relevent for reference.
root@tecmint:~# mkdir usb
?
Now mount filesystem ‘sdb1‘ to directory ‘usb‘.
root@tecmint:~# mount /dev/sdb1 /dev/usb
?
Now you can navigate to /dev/usb from terminal or X-windows system and acess file from the mounted directory.
Time for Code Developer to know how rich Linux environment is?38. Command: gccgcc is the in-built compiler for ‘c‘ language in Linux Environment. A simple c program, save it on ur desktop as Hello.c (remember ‘.c‘ extension is must).
#include <stdio.h>int main(){ printf("Hello world\n"); return 0;}
?
Compile itroot@tecmint:~# gcc Hello.c
?
Run itroot@tecmint:~# ./a.out Hello world
?
Note: On compiling a c program the output is automatically generated to a new file “a.out” and everytime you compile a c program same file “a.out” gets modified. Hence it is a good advice to define a output file during compile and thus there is no risk of overwrite to output file.
?
Compile it this wayroot@tecmint:~# gcc -o Hello Hello.c
?
Here ‘-o‘ sends the output to ‘Hello‘ file and not ‘a.out‘. Run it again.
root@tecmint:~# ./Hello Hello world
?
39. Command: g++g++ is the in-built compiler for ‘C++‘ , the first object oriented programming language. A simple c++ program, save it on ur desktop as Add.cpp (remember ‘.cpp‘ extension is must).
#include <iostream>using namespace std;int main() { int a; int b; cout<<"Enter first number:\n"; cin >> a; cout <<"Enter the second number:\n"; cin>> b; cin.ignore(); int result = a + b; cout<<"Result is"<<" "<<result<<endl; cin.get(); return 0; }
?
Compile itroot@tecmint:~# g++ Add.cpp
?
Run itroot@tecmint:~# ./a.outEnter first number: ......
?
Note: On compiling a c++ program the output is automatically generated to a new file “a.out” and everytime you compile a c++ program same file “a.out” gets modified. Hence it is a good advice to define a output file during compile and thus there is no risk of overwrite to output file.
?
Compile it this wayroot@tecmint:~# g++ -o Add Add.cpp
?
Run itroot@tecmint:~# ./Add Enter first number: ......
?
40. Command: javaJava is one of the world’s highly used programming language and is considered fast, secure, and reliable. Most of the the web based service of today runs on java.
Create a simple java program by pasting the below test to a file, named tecmint.java (remember ‘.java‘ extension is must).
class tecmint { public static void main(String[] arguments) { System.out.println("Tecmint "); }}
?
compile it using javacroot@tecmint:~# javac tecmint.java
?
Run itroot@tecmint:~# java tecmint
?
Note: Almost every distribution comes packed with gcc compiler, major number of distros have inbuilt g++ and java compiler, while some may not have. You can apt or yum the required package.
Don’t forget to mention your valueable comment and the type of article you want to see here. I will soon be back with an interesting topic about the lesser known facts about Linux.
?
来源:http://www.tecmint.com/20-advanced-commands-for-middle-level-linux-users/
?