basic command

send email

Mail can be sent and received using the mail command, the syntax is as follows:

$mail [-s subject] [-c cc-addr] [-b bcc-addr] to-addr

The meaning of each option is as follows:

Description of options
-s mail title.
-c Users to send, multiple users separated by commas (,).
-b Users who need to BCC (Bcc), multiple users are separated by commas (,).


For example, send an email to [email protected]:

$mail -s "Test Message" [email protected]
Hello everyone,
this is Linux tutorial and url is http://see.xidian.edu.cn/cpp/linux/.
Cc:

The first line is the entered command, -s indicates the subject of the email, and the following [email protected] is the recipient of the email. After entering this line of commands, press Enter to enter the writing of the email body. You can enter any text. , such as the two lines above. After entering the email body, you need to press CTRL+D to end the input. At this time, you will be prompted to enter the Cc address, that is, the email copy address, and the email will be sent without pressing Enter.

It is also possible to send files via the redirection operator <:

$mail -s "Report 05/06/07" [email protected] < demo.txt

Through the above command, the content of the demo.txt file can be sent to [email protected] as the content of the email.

 

 

check the file

To view files and directories in the current directory, use the ls command

You can get more file information with the -l option of the ls command

You can get more file information with the -l option of the ls command

Create a file

In Linux, you can use the vi editor to create a text file, for example:

$ vi filename

The above command will create and open the file filename, press the i key to enter edit mode, and you can write to the file. E.g:

This is Linux file....I created it for the first time.....
I'm going to save this content in this file.

When you are done editing, you can press esc to exit edit mode

:wq save and quit

:q quit

View file contents

You can use the cat command to view the contents of the file, here is a simple example:

$ cat filename
This is Linux file....I created it for the first time.....
I'm going to save this content in this file.
$

Line numbers can be displayed with the -b option of the cat command, for example:

$ cat -b filename
1   This is Linux file....I created it for the first time.....
2   I'm going to save this content in this file.
$

count words

You can use the wc command to count the number of lines, words and characters in the current file. The following is a simple example:

$ wc filename
2  19 103 filename
$

The meaning of each column is as follows:

  • Column 1: The total number of lines in the file
  • Second column: number of words
  • The third column: the number of bytes of the file, which is the size of the file
  • Fourth column: filename


It is also possible to view the contents of multiple files at once, for example:

$ wc filename1 filename2 filename3

copy file

You can use the cp command to copy files. The basic syntax of the cp command is as follows:

$ cp source_file destination_file

The following example will copy the file filename:

$ cp filename copyfile
$

Now there will be an extra copyfile file with the same filename in the current directory.

rename file

Renaming files can be done using the mv command, the syntax is:

$ mv old_file new_file

The following example will rename the file filename to newfile:

$ mv filename newfile
$

现在在当前目录下,只有一个 newfile 文件。

mv 命令其实是一个移动文件的命令,不但可以更改文件的路径,也可以更改文件名。

删除文件

rm命令可以删除文件,语法为:

$ rm filename

注意:删除文件是一种危险的行为,因为文件内可能包含有用信息,建议结合 -i 选项来使用 rm 命令。

下面的例子会彻底删除一个文件:

$ rm filename
$

你也可以一次删除多个文件:$ rm filename1 filename2 filename3

主目录

登录后,你所在的位置就是你的主目录(或登录目录),接下来你主要是在这个目录下进行操作,如创建文件、删除文件等。

使用下面的命令可以随时进入主目录:
$cd ~
$
这里 ~ 就表示主目录。如果你希望进入其他用户的主目录,可以使用下面的命令:
$cd ~username
$
返回进入当前目录前所在的目录可以使用下面的命令:
$cd -
$

绝对路径和相对路径

Linux 的目录有清晰的层次结构,/ 代表根目录,所有的目录都位于 / 下面;文件在层次结构中的位置可以用路径来表示。

如果一个路径以 / 开头,就称为绝对路径;它表示当前文件与根目录的关系。举例如下:
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
不以 / 开头的路径称为相对路径,它表示文件与当前目录的关系。例如:
chem/notes
personal/res
获取当前所在的目录可以使用 pwd 命令:
$pwd
/user0/home/amrood

$
查看目录中的文件可以使用 ls 命令:
$ls dirname
下面的例子将遍历 /usr/local 目录下的文件:
$ls /usr/local

X11       bin          gimp       jikes       sbin
ace       doc          include    lib         share
atalk     etc          info       man         ami

创建目录

可以使用 mkdir 命令来创建目录,语法为:
$mkdir dirname
dirname 可以为绝对路径,也可以为相对路径。例如
$mkdir mydir
$
会在当前目录下创建 mydir 目录。又如
$mkdir /tmp/test-dir
$
会在 /tmp 目录下创建 test-dir 目录。mkdir 成功创建目录后不会输出任何信息。

也可以使用 mkdir 命令同时创建多个目录,例如
$mkdir docs pub
$
会在当前目录下创建 docs 和 pub 两个目录。

创建父目录

使用 mkdir 命令创建目录时,如果上级目录不存在,就会报错。下面的例子中,mkdir 会输出错误信息:
$mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
No such file or directory
$
为 mkdir 命令增加 -p 选项,可以一级一级创建所需要的目录,即使上级目录不存在也不会报错。例如
$mkdir -p /tmp/amrood/test
$
会创建所有不存在的上级目录。

删除目录

可以使用 rmdir 命令来删除目录,例如:
$rmdir dirname
$
注意:删除目录时请确保目录为空,不会包含其他文件或目录。

也可以使用 rmdir 命令同时删除多个目录:
$rmdir dirname1 dirname2 dirname3
$
如果 dirname1、dirname2、dirname3 为空,就会被删除。rmdir 成功删除目录后不会输出任何信息。

改变所在目录

可以使用 cd 命令来改变当前所在目录,进入任何有权限的目录,语法为:
$cd dirname
dirname 为路径,可以为相对路径,也可以为绝对路径。例如
$cd /usr/local/bin
$
可以进入 /usr/local/bin 目录。可以使用相对路径从这个目录进入 /usr/home/amrood 目录:
$cd ../../home/amrood
$

重命名目录

mv (move) 命令也可以用来重命名目录,语法为:
$mv olddir newdir
下面的例子将会把 mydir 目录重命名为 yourdir 目录:
$mv mydir yourdir
$

点号(.)

一个点号(.)表示当前目录,两个点号(..)表示上级目录(父目录)。

ls 命令的 -a 选项可以查看所有文件,包括隐藏文件;-l 选项可以查看文件的所有信息,共有7列。例如:
$ls -la
drwxrwxr-x    4    teacher   class   2048  Jul 16 17.56 .
drwxr-xr-x    60   root              1536  Jul 13 14:18 ..
----------    1    teacher   class   4210  May 1 08:27 .profile
-rwxr-xr-x    1    teacher   class   1948  May 12 13:42 memo
$

查看文件权限

使用 ls -l 命令可以查看与文件权限相关的信息:
$ls -l /home/amrood
-rwxr-xr--  1 amrood   users 1024  Nov 2 00:10  myfile
drwxr-xr--- 1 amrood   users 1024  Nov 2 00:10  mydir
第一列就包含了文件或目录的权限。

第一列的字符可以分为三组,每一组有三个,每个字符都代表不同的权限,分别为读取(r)、写入(w)和执行(x):
  • 第一组字符(2-4)表示文件所有者的权限,-rwxr-xr-- 表示所有者拥有读取(r)、写入(w)和执行(x)的权限。
  • The second set of characters (5-7) indicates the permissions of the user group to which the file belongs, -rwxr-xr-- indicates that the group has read (r) and execute (x) permissions, but no write permissions.
  • The third set of characters (8-10) indicates permissions for all other users, and rwxr-xr-- indicates that other users can only read (r) the file.

file access mode

File permissions are the first line of defense in Linux systems. The basic permissions are read (r), write (w), and execute (x):
  • Read: The user can read the file information and view the file content.
  • Write: The user can edit the file, write content to the file, and delete the file content.
  • Execute: The user can run the file as a program.

Directory Access Mode

Access patterns for directories are similar to files, but with a few differences:
  • Read: Users can view files in a directory
  • Write: User can delete files or create files in the current directory
  • Execute: The execute permission gives the user the right to traverse the directory, such as executing the cd and ls commands.

change permissions

You can use the chmod (change mode) command to change the access permissions of a file or directory. Permissions can be represented by symbols or numbers.

 

Use numbers to indicate permissions

In addition to symbols, you can also use octal numbers to specify specific permissions, as shown in the following table:

Digital Description Authority
0 don't have any permissions ---
1 execute permission --x
2 write permission -w-
3 Execute and write permissions: 1 (execute) + 2 (write) = 3 -wx
4 read permission r--
5 Read and execute permissions: 4 (read) + 1 (execute) = 5 r-x
6 Read and write permissions: 4 (read) + 2 (write) = 6 rw-
7 All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 rwx


The following example first uses the ls -1 command to view the permissions of the testfile file, and then uses the chmod command to change the permissions:

$ls -l testfile
-rwxrwxr--  1 amrood   users 1024  Nov 2 00:10  testfile
$ chmod 755 testfile
$ls -l testfile
-rwxr-xr-x  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod 743 testfile
$ls -l testfile
-rwxr---wx  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod 043 testfile
$ls -l testfile
----r---wx  1 amrood   users 1024  Nov 2 00:10  testfile

更改所有者和用户组

在Linux中,每添加一个新用户,就会为它分配一个用户ID和群组ID,上面提到的文件权限也是基于用户和群组来分配的。

有两个命令可以改变文件的所有者或群组:

  • chown:chown 命令是"change owner"的缩写,用来改变文件的所有者。
  • chgrp:chgrp 命令是"change group"的缩写,用来改变文件所在的群组。


chown 命令用来更改文件所有者,其语法如下:

$ chown user filelist

user 可以是用户名或用户ID,例如

$ chown amrood testfile
$

将 testfile 文件的所有者改为 amrood。

注意:超级用户 root 可以不受限制的更改文件的所有者和用户组,但是普通用户只能更改所有者是自己的文件或目录。

chgrp 命令用来改变文件所属群组,其语法为:

$ chgrp group filelist

group可以是群组名或群组ID,例如

$ chgrp special testfile
$

将文件 testfile 的群组改为 special。

$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin
管道使用竖线(|)将两个命令隔开,竖线左边命令的输出就会作为竖线右边命令的输入。连续使用竖线表示第一个命令的输出会作为第二个命令的输入,第二个命令的输出又会作为第三个命令的输入,依此类推。

A tool that accepts data, filters (processes or filters) and outputs it, is called a filter.

grep command

grep is a powerful text search tool that can use regular expressions and return matching lines, the syntax is:
$grep pattern file(s)
"grep" comes from the g/re/p command of ed (a line text editor for Linux), g/re/p is an abbreviation for "globally search for a regular expression and print all lines containing it", which means using regular The expression is searched globally and the matching lines are printed.

grep can be thought of as a filter, and if grep doesn't specify a file to retrieve, it reads from the standard input device (usually the keyboard); the same goes for other filters.

The simplest use of the grep command is to retrieve text that contains fixed characters.

For example, use the grep command in a pipe to allow only lines containing specified characters to be output to the display:
$ls -l | grep "Aug"
-rw-rw-rw-   1 john  doc     11008 Aug  6 14:10 ch02
-rw-rw-rw-   1 john  doc      8515 Aug  6 15:30 ch07
-rw-rw-r--   1 john  doc      2488 Aug 15 10:51 intro
-rw-rw-r--   1 carol doc      1605 Aug 23 07:35 macros
$
The grep command has many options:
Option Description
-v Reverse the query, outputting rows that don't match. For example, grep -v "test" demo.txt will output lines that do not contain "test".
-n Output matching lines and line numbers.
-l Output the filename where the matched line is located.
-c Output the total number of rows matched.
-i Matches are case-insensitive.
Below we use a regular expression to match lines that contain the characters "carol", then any number (including zero) of other characters, and finally "Aug".

Use the -i option for case-insensitive matching:
$ls -l | grep -i "carol.*aug"
-rw-rw-r--   1 carol doc      1605 Aug 23 07:35 macros
$

sort command

The sort command is very useful in Linux, it sorts the lines in a file alphabetically or numerically. The sort command can take input both from a specific file and from stdin

The collation can be controlled by the following options:

Description of options
-n Sort by numerical order, for example, 10 will be sorted after 2; the -n option will ignore spaces or tab indentation.
-r Sort descending. sort defaults to ascending order.
-f not case sensitive.
+x Sort the xth column (starting at 0).


The following example pipes the ls, grep, and sort commands together to filter lines containing "Aug" and sort by file size:

$ls -l | grep "Aug" | sort +4n
-rw-rw-r--  1 carol doc      1605 Aug 23 07:35 macros
-rw-rw-r--  1 john  doc      2488 Aug 15 10:51 intro
-rw-rw-rw-  1 john  doc      8515 Aug  6 15:30 ch07
-rw-rw-rw-  1 john  doc     11008 Aug  6 14:10 ch02
$

The above command sorts the files modified in August in the current directory according to their size; +4n means to sort the 5th column according to the numerical size.

pg and more commands

If the content of the file is too much, the whole display will be very messy. You can use the pg and more commands to paginate and display, and only display one screen at a time.

For example, through a pipe, use the more command to display files in a directory:

$ls -l | grep "Aug" | sort +4n | more
-rw-rw-r--  1 carol doc      1605 Aug 23 07:35 macros
-rw-rw-r--  1 john  doc      2488 Aug 15 10:51 intro
-rw-rw-rw-  1 john  doc      8515 Aug  6 15:30 ch07
-rw-rw-r--  1 john  doc     14827 Aug  9 12:40 ch03
.
.
.
-rw-rw-rw-  1 john  doc     16867 Aug  6 15:56 ch05
--More--(74%)

如上,一次只显示一屏文本,显示满后,停下来,并提示已显示全部内容的百分比,按空格键(space)可以查看下一屏,按 b 键可以查看上一屏。

 

 

http://c.biancheng.net/cpp/html/2735.html

 

 

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326217840&siteId=291194637