Python Learning record--ubuntu (iii) file operations

Source: Internet
Author: User

A. mkdir is used to create a directory:

mkdir Directory name #创建目录

Mkdir-p Directory name 1/directory Name 2/directory name 3 #创建多层目录

Two. RM for deleting files ( Use caution, easy to trigger program crashes ):

1. Parameters

(1) RM file name #只能删除文件, but cannot delete directory

(2) rm-i file name #会出现提示

(3) rm-f file name #强制删除

(4) RM-RF directory name #强制删除目录

Three. MV is used to move or rename files/directories:

1. Example

(1) MV File/directory name new File/directory name #重命名文件/directory

(2) MV FileName Directory name #将文件移至目录下

(3) MV File/directory name. #移动至当前目录

Four. CP for copying Files:

1. Example

(1) CP filename New file name #在当前目录复制文件并重新命名

(2) CP directory name/new directory name # Note Directory replication to add/

(3) CP -a filename new file name #复制文件, and retain the original permission and owner

five .> for redirection (write file):

(1) > redirect, if the file exists, then overwrite the file content, file does not exist when creating the file

(2) >> Redirect, if the file exists, append the content to the file, and the file does not exist when the file is created

(3) 1> standard correct output, same as cover

(4) 1>> standard correct output, same as Append

(5) 2> standard error output, same as overlay

(6) 2>> standard error output, same as Append

(7) &> standard correct output and standard error output, same as overlay

Eg:cat 123.txt 1>a 2>b #如果正确输出则a有内容b没有

Vi. File Attributes modification:

1.chmod for modifying file permissions

(1) chmod u (on behalf of user users)/g (on behalf of group user groups)/O (on behalf of others others)/A (for all) =rwx (permissions for Read and write execution, no permissions used-instead)./File name #修改文件权限

(2) chmod u (on behalf of user users)/g (on behalf of group user groups)/O (on behalf of others others)/A (on behalf of all) +/-rwx (permissions for Read and write execution, no permissions are not written)./file name #可用加/minus way to modify permissions

(3) chmod corresponds to three digits./File name #修改文件权限

(4) can also use the number to represent the authority: R:4,w:2,x:1 Rwx:7,rw:6,rx:5

2.chown for modifying file owners and all groups

(1) Chown name file name #更改所有者

(2) Chown: name filename #更改所有组

(3) Chown name: Name File name #更改所有者和所有组

Seven. LS is used to view file information:

1. Parameters

(1) LS #查看当前目录下可见的文件

(2) Ls-a #查看当前目录下所有文件, if the document is in front of the hidden file; Represents the current directory and parent directory, respectively

(3) Ls-l #显示所有文件及详细信息

(4) ls-l./File name #显示该文件详细信息

The details are displayed in the following format:

File types (-for normal files, d for catalog files, b for block device files, c for character device files, L for link files (e.g. shortcuts), p for pipe files, s for socket files)

For example:-rwxrwxrwx,-is the file type, R is readable (whether the directory can read the file name in the folder), W is writable (whether the directory can create and modify files in the directory), X is executable (whether the directory can be searched/able to enter the directory)

3 groups, which represent the user, user group, other people's file operation permissions

    in ./When executing a file, the user must have X permission on the file

Digital

Owner

User groups belonging to

File size (default is bytes, LS-LH can be changed to KB, directory default is 4096 bytes)

Date Modified

Filename

(4) Ls-lt #附加以时间排降序 (if R is added after T, can be changed to ascending)

(5) Ls-r #遍历所有文件目录

Eight. Common file Operation command:

1. Change the current directory

(1) cd+ address #更改目录至制定地址, such as Cd/home; CD ~/Desktop

(2)CD. #返回当前目录的上一级目录

(3) cd-#返回到当前目录之前打开的目录

(4) CD #回用户家目录

2.touch file name #将文件时间修改成当前时间, such as the time to consolidate multiple files

3.cat file name #查看文件内容

Cat-s the file name #将多个空行合并成一个空行, but the row is not merged if there are spaces within it

4.TAC file name #从下往上输出文件内容

5.sort #排序

(1) Sort-r #反向排序

(2) Sort-n #使用纯数字排序

(3) Sort-f #忽略大小写

(4) Sort-t separator #指定分隔符

6.uniq #忽略或报告重复行

Uniq-c #进行计数

Uniq-u #不显示重复行

Uniq-i #不区分大小写

7.cut #提取文本列

Cut-d delimiter-F column number

Cut-c #以字符为单位提取 such as: cut-c 1,5 extract first and fifth words, Cut-c 1-5 extract first to fifth words

8.tee #读取标准输入的数据 and output its contents as a file

9.history #列出历史命令

History number #显示最近多少条命令

History!number #执行第几条命令

10.more/less file name #分页显示, space to display the next page, enter to display the next line, B fallback, q exit

11head/tail file name #显示前/After 10 lines of text

  Tail-f file name #实时监控文件变化, can be used on log file monitoring

12.which Command #查命令在什么位置

13.stat file name #用于查看文件信息:

14.pwd #查看当前目录

    1. Alias #用于给命令起别名

Alias alias = ' formerly ' #如: Alias la= ' Ls-a '

If an alias is required to be permanent, it needs to be saved to the. bashrc file

16.source file name #在当前bash环境下读取并执行FileName中的命令, the filename file can have no "execute permission"

17.echo #可用于在屏幕上打印文件内容, echo-n non-newline output

18.df-th #以能显示的最大单位显示文件系统类型

18.du-s (if the directory is followed, only one layer is displayed)-H (displayed in the largest units that can be displayed) directory name #显示目录的大小

Nine. File Search:

1.locate:

(1) LOCATE/ETC/SH # Search path contains/etc/sh files.

(2) Locate ~/a # Search the user home directory, all files beginning with a.

(3) Locate-i #忽略大小写.

  Locate you find a file, you cannot find the file if the database is not updated in a timely manner, you can update the database manually by using the sudo updatedb command

2.find:

(1) How to use: Find Path-option [-print] [-exec-ok command] {} \;

(2) Search by file name:

Find directory-name file name #在目录里面搜索某文件名的文件

Find/home-name "*.txt" #在家目录里搜索txt后缀的文件

Find/home-iname "*.txt" #忽略大小写

(3) Search by file type:

Find directory-type type parameter f normal file L symbol connection D directory C character device B block device s socket p FIFO

(4) Search by directory depth:

Find. -maxdepth 3-type F # Find from current directory, max depth is 3

Find. -mindepth 2-type F # Minimum depth of 2

(5) Search according to the file's permission or size name type:

Find directory-type f-size (+|-) file size # + = Greater than-Indicates less than

b--block (512 bytes) c--byte w--Word (2 bytes) k--Kbytes m--megabytes g--gigabytes

(6) Search by Time:

-atime (+|-) n # This option represents finding files that were read n days ago.

-mtime (+|-) n # This option represents a file that finds the content of a file that was changed n days ago.

-ctime (+|-) n # This option represents a file that looks for changes to the properties of files that were older than n days.

-newer File # This option represents finding out all the new files that are newer than document.

-newer file1! –newer File2 # This option represents a new file that looks for newer files than the File1 file time but does not have file2 time.

Attention:

N is a number, and if there is no + or-number, it represents a file that has been changed in the range of a day, but only for a period of the past n days.

If n is preceded by A + sign, it represents a file that has changed before n days. If it is a minus sign, it means finding all files that have changed within the N-day range.

-newer file1! –newer File2! is a logical non-operator

(7) Search by user/authority:

-user user name: Finds files based on the file's owner name.

-group group name: Finds files based on the file's group name.

-uid N: Finds files based on the UID of the file owner.

-gid N: Finds files according to the GID of the file group.

-nouser: The query file belongs to a file that does not exist in the/etc/passwd file.

-nogroup: Querying files belonging to a file that does not exist in the/etc/group file

-perm 777: Query permissions of 777 files

(8) Specify multiple criteria when searching:

-O: Logical OR, two conditions as long as one is satisfied.

-A: Logic with, two conditions must be met at the same time.

Find/etc-size +2m-a-size-10m

(9) Processing the results of the search:

-exec shell command {} \;

-ok shell command {} \; Recommended

Where the-exec is to execute the shell command, followed by the shell directive, and then the "{}" is to query the results of the previous query, the last "\;" indicates the end of the command. It is important to note that the " {} "and" \ "There is a space between." The only difference between the-OK option and-exec is that it asks for it in advance of executing the shell command.

The-print option is to display the results on standard input

10. Program Writing terminal gedit:

Write #! at the beginning of the file /usr/bin/env Python3 #用于指定解释器为python3, which specifies that files can be executed in./File name mode later

Python Learning record--ubuntu (iii) file operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.