Python learning Day09-Linux acquaintance

Everything is a file

*********************************************** LInux directory structure : ***************************************** 

/ (Root): all directories in
/ boot: boot configuration files, kernels, and other files needed to boot (operating system)
/ etc: system configuration stored documents relating to
/ home: storage of common user directory
/ mnt: on the hard disk manually mounted file system (U disk is inserted, the hard disk connected to the system)
/ media: automatically mounted (loaded) in the CD disk partition, and the like, a digital camera, a removable medium.
/ cdrom: mount the CD? 
/ opt: store a number of optional programs, such as a test version of the program, all the data, library files to the installation directory of the program there under the same directory
/ root: (root) directory system administrator for system say, the system administrator is like God, he can do anything to the system, such as deleting your files, do not use the root user under normal circumstances.
/ bin: to store frequently used files (command file).
/ sbin: system management commands, is stored here hypervisor system administrators 
/ tmp: temporary directory, temporary files, the system will clean up the files in the directory on a regular basis.
/ usr: Under this directory, you can find additional tools in / bin or / etc directory that do not fit. Such as games, printing tools. / usr directory contains many subdirectories: / usr / bin directory for program storage; / usr / share for shared data, such as music files or icons; / usr / lib directory holds those not directly running, but some library files necessary to run many programs. / usr / local: This directory is generally used to store user directory where the software is compiled and installed; source package software is generally installed, if not specified, then the installation directory, usually installed in this directory.
    / usr / bin / non-essential executable files (not required in a single user mode); for all users.
    / usr / include / standard include files.
    / usr / lib / / usr / bin / and / usr / sbin / binary file library.
    / usr / sbin / Non-essential system binaries, such as: daemon large number of network services.
    / usr / share / architecture independent (shared) data.
    / usr / src / source code, for example: the kernel source code and header files.
    / usr / X11R6 / X Window system version. 11, Release 6. The
    / usr / local / local third hierarchy data, specific to this station host. Generally speaking there are further subdirectories, for example: bin /, lib /, share /.

/ var: This directory contains files that are often modified, including various logs, data files;
/ var / Cache / application cache data. These data are generated locally in a time consuming I / O or calculation results. The application must be able to regenerate or recover data. Cached files can be deleted without loss of data.
/ var / lib / status information. By the program maintenance of persistent data at runtime. For example: a database like packaging system metadata.
/ var / lock / lock file, a trace file class resources currently in use.
/ var / log / log file that contains a large number of log files.
/ var / mail / e-mail user.
/ var / run / start-up information since the last run of the system, such as: the current daemon and the user logged in running. Now it has been / run instead of [13].
/ var / spool / offline files pending tasks, such as: print queues and unread mail.
/ var / spool / mail / user's mailbox (not encourage the storage location)
/ var / tmp / temporary files during the restart of the system can be retained.
/ lib: directory is you need to share libraries on the root file system, the root file system to store the shared files required to run the program. These files contain many programs can be shared code to avoid each program contains a copy of the same subroutine, it is possible to make the executable file smaller, saving space.
/ lib32: ditto
/ lib64: ditto
/ lost + found: the directory in most cases are empty. However, when a sudden power failure or abnormal shutdown, the file is temporarily stored in some;
/ dev: storage devices (U disk, CD-ROM, a hard disk) file
/ run: instead of / var / run directory,
/ proc: virtual file system, you can get in the directory system information, which is in memory generated by the system itself, the contents of the directory is not on the hard disk and in memory;
/ SYS: and proc as a virtual file system can obtain system information in the directory, the information in memory is generated by the system itself, and not the contents of the directory on the hard disk and in memory;

 

 

 

Locales

Check whether the installation of Chinese support

locale -a 

If there zh_CN.utf8 indicates that the system has been installed Chinese locale, if not you need to install the appropriate software package. Installation is as follows:

sudo apt-get install language-pack-zh-hans language-pack-zh-hans-base

..............................................

 

Software Management apt (Advanced Packaging Tool), he can automatically download, configure, install the package; simplifying the Linux system. Debian and derivative version are included apt, RedHat linux series of use yum to manage, which in Fedora22 in Centos7 start using dnf instead yum.

* apt-cache search package search Package 
Gets the package apt-cache show package-related information, such as description, size, version, etc. 
* sudo apt-get install package installation package 
sudo apt-get install package -reinstall reinstall package 
sudo apt-get -f install mandatory installation 
* sudo apt-get remove package delete package 
* sudo apt-get remove package -purge remove the package, including deleting profiles, etc. 
* sudo apt-get autoremove automatically delete unwanted package 
* sudo apt-get update update source 
* sudo apt-get upgrade installed packages updated 
sudo apt-get dist-upgrade to upgrade the system 
sudo apt-get dselect-upgrade using dselect upgrade 
apt-cache depends package is understood to use dependent 
apt-cache rdepends package to understand a particular depends 
sudo apt-get build-dep package related to the installation environment compiled 
source code apt-get source package download the package  
archive autoclean clean up download files sudo apt-get clean && sudo apt -get in
sudo apt-get check dependency check for damage

apt configuration file

Set packages get /etc/apt/sources.list source 
/etc/apt/apt.conf apt configuration file 
/etc/apt/apt.conf.d apt fragmented configuration file 
/ etc / apt / preferences version parameter 
/ var / cache / apt / archives / partial deposit being downloaded packages 
/ var / cache / apt / archives to store the downloaded packages 
/ var / lib / apt / lists to store the downloaded package Details 
/ var / lib / apt / lists / partial deposit being downloaded package details

apt configuration file

Copy the code
Set packages get /etc/apt/sources.list source 
/etc/apt/apt.conf apt configuration file 
/etc/apt/apt.conf.d apt fragmented configuration file 
/ etc / apt / preferences version parameter 
/ var / cache / apt / archives / partial deposit being downloaded packages 
/ var / cache / apt / archives to store the downloaded packages 
/ var / lib / apt / lists to store the downloaded package Details 
/ var / lib / apt / lists / partial deposit being downloaded package details
Copy the code

Software source configuration file format: 

Copy the code
deb http://security.ubuntu.com/ubuntu great-security main restricted 
# deb-src http://security.ubuntu.com/ubuntu great-security main restricted 
deb great http://security.ubuntu.com/ubuntu -security universe 
# deb-src http://security.ubuntu.com/ubuntu great-security universe 
deb http://security.ubuntu.com/ubuntu great-security multiverse 
# deb-src http: //security.ubuntu. com / ubuntu-security multiverse great
Copy the code

date: set the system to display the date or time, and

Copy the code
Displays the current date date // 
# Date Format 
#% the Y year 
#% m month The (01..12) 
# Day of month The% D (EG, 01) 
#% H hour (00..23) 
#% the I hour ( 01..12) 
#% m minute (00..59) 
#% S SECOND (00..60) 
DATE + "the Y% m% D% H% m% S%" 
    20,160,824 223 856 
DATE + "% Y-% % d% H M-:% M:% S " 
    2016-08-24 22:39:07 

DATE -s // set the current time, only root privileges to set up, the other can only view. 
date -s 20061010 // set to 20,061,010, which will set the specific time to empty 00:00:00 
DATE -s 12:23:23 // set a specific time and date will not make changes to the 
date -s "12:12 : 232006-10-10 "// this will set the all-time 

# Note: You need time to poke not the hardware clock after reset the time. As follows: 
hwclock -w unified operating system and hardware time
Copy the code

 

cal: display a calendar

cal # realities of the current month's calendar 
cal -y # display the calendar year 
cal 2016 # # display the specified calendar year

 

Set the time zone 

tzselect

# 或者

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 

 

 Similar note pad ++ software under gedit #Linux

tab key to auto-complete, double-click displays the current command all files

Ctrl K delete the contents of the current behind the cursor Ctrl U to delete the previous

hot key

ctrl-a: Move the cursor to the beginning where the command line. 
ctrl-e: Move the cursor to the end of the command line. 
ctrl-u: erase all characters preceding the cursor position in the command line. 
ctrl-k: prompt clearance from the location to the end of the row of characters between the
ctrl-w: Clear left of the field 
portion is removed ctrl-u or ctrl-k will paste or ctrl-w: ctrl-y. 
ctrl-r: will automatically incremental search command history buffer back into character. 
tab: command line completion - Auto-completion of the current command line. If you enable auto-completion script command parameters and options can be automatically filled.

ctrl-l: clear screen

Ctrl-c: abort command

Ctrl-d: Exit the current terminal

 

Logout / restart / shutdown

logout logout #

reboot # reboot the system: the need administrator rights

shutdown # shutdown: Administrator rights are required

Copy the code
shutdown -r now # now immediately restart 
shutdown -r +5 # three minutes after the restart 
shutdown -r 12:12 # at the 12:12 will reboot the computer 

shutdown -h now # now immediately shutdown 
shutdown -h +5 "The System will shutdown after 3 minutes "# prompt the user to shut down in three minutes after 
shutdown -h +5 # 5 minutes after the shutdown 
shutdown -h 12:00 # 12 o'clock shutdown 
shutdown -c # cancel the shutdown operation
Copy the code

 

shift-prtsc: region screenshot

cd: Change directory change dir

Copy the code
CD # back to the current user's home directory 
# ~ can be used to represent the user's home directory 
CD / etc # switch to the / etc directory 

CD -
# switch to a directory

of the current directory.
.. the parent directory
Copy the code

 pwd: view the current working directory

passwd: Modify the current user's password

Getting Help 

 -h  --help  info  man 

Copy the code
man man # View man command man   
man cd 
man pwd 
man 5 passwd 
man -k # passwd fuzzy search 
man -f passwd # Exact Match
Copy the code

 

ls: lists all the subdirectories and files in the destination directory

Format: ls [options] [directory name] 

-a displays all files and subdirectories (Paul point file).

-l In addition to the file names, permissions, owner, file size and other information will be detailed in the file.

-r will list contents of the directory are displayed in reverse alphabetical order.

-t sort by file modification time, rather than to sort by file name.

With -A -a, but not listed "." (Current directory), and ".." (indicating a parent directory of the current directory).

-F after the file and directory names listed in the add mark. For example, after the executable file to add "*", the directory name to add "/" to distinguish between different types.

-R If there are files in the destination directory and its subdirectories, to list all files.

. with.. 

It indicates the current directory

.. represents the parent directory

Copy the code
ls # list files and directories in the current directory 
ls. # list files and directories in the current directory 
ls .. # list files and directories under the parent directory of the current directory 
ls / etc # listed in the / etc directory files and directories 

ls -l # display file format information in a long 
total amount of 76 
-rwxrwxrwx (read, write, execute) 1 will will 78. 5 18:11 ss_start.sh dated 13 is 
User rwx
Group rwx
OTHER rwx
Copy the code

file permission

rwxrwxr--: three sets of rwx indicate the owner of all groups, other people's rights.

r: represents the read, it can be represented by numeral 4
w: identification may write, numeral 2 can be represented by
x: represents the executable, may be represented by numeral 1
-: indicates no corresponding rights can be represented by numbers 0

Modify the permissions of methods: 

Copy the code
O + w file1 chmod 
chmod gw file1 
chmod Go-w file1 
chmod U = rwx file1 

chmod 755 file1 #-the X--rwxr-XR (755) Only the owner has read, write, and execute permissions, group and others only read and execute permissions 
chmod 644 # -rw-r - r-- (644) only the owner has read and write permissions, group and others have only read permissions 


# where: 
# U behalf of the owner (user ) 
# group (group) g on behalf of the owner's 
# o behalf of someone else, but not u and G (oTHER) 
# a representative of all the people, that is, including the u, g and o
Copy the code

Permissions on the directory: 

r: Indicates whether you can read the file name in the directory

w: Indicates whether you can modify the file created in the directory

x: Indicates whether the directory can be searched

After x have permission, you can execute the file using ./a.py way.

 

chown: change the file owner and group of all

chown root:root  file
chown root   file  
chown :root   file

 

 shebang: #! XXXXXXXXXX specified indicator

Create a directory: 

# Mkdir directory name 
mkdir my_dir 

# - the p-parameter: recursively create directories for the same time to create multi-level directory 
mkdir a / b / c / d   

Create a file

touch: change the time the file or directory, create an empty file when the file does not exist.

Copy the code
was created when the touch file1 # file1 does not exist 
touch -c file1 # does not create a file 
touch -r ref_file file1 file1.txt update timestamp and ref + file the same 
-t 201,210,120,505.25 file1 Touch 

# -t time Use the specified time as a time value the new value specified file corresponding time stamp. : # # Time defined here as the form of a decimal number       
# [[CC] YY] MMDDhhmm      [.SS] 
after two YY is the number of years; # here, CC is the number of years in the first two, namely "several centuries" bits, the number of years a century. If not given value CC, the number of touch will CCYY defined within the 1969--2068. MM is the number of the month, DD for the day will be limited in the number of years CCYY of 1969--2068. MM is the number of the month, DD is the number of days, hh is the number of (several) hours, mm is the number of minutes, SS is the number of seconds. Second setting range is 0--61 here, so leap seconds can be processed. Time for these numbers is a time TZ environment variable specified time zone. Due to system constraints, time is earlier than January 1, 1970 is wrong.
Copy the code

 

Delete: 
RM: Delete command 

rm -f file1 # delete a file mandatory 
rm -ra / b / file1 # delete the specified directory and all files and directories under 
rm -rf a / b / file1 # forcibly remove the specified directory and all files and directories under 

# rm command too dangerous, not recommended 

rmdir # delete empty directories

 

mv: Move or Rename command file or directory

the SOURCE DEST # Music Videos 

Music Videos the test.log # test.txt file rename 
mv test1.txt dir1 / # move files 
mv test1.txt test2.tx test3.tx dir1 / # moving multiple files

 

cp: Copy

Copy the code
cp SOURCE DEST # copy files 

cp -i SOURCE DEST # If you encounter a situation to be covered, you are prompted 
source file cp -r dir1 dir2 # If the given file is a directory, then cp will copy recursively all directories under the subdirectories and files. At this time, the target file must be a directory name 
cp -p file1 file2 # cp except this time copy the contents of the source file, it will also modify time and access rights are also copied to the new file.
-rp dir1 dir2 cp 
cp command to copy direct-A #
Copy the code

 

stat: view the file information to believe

Copy the code
stat filename 
#  Access time(atime):是指取用文件的时间,所谓取用,常见的操作有:使用编辑器查看文件内容,使用cat命令显示文件内容,使用cp命令把该文件(即来源文件)复制成其他文件,或者在这个文件上运用grep sed more less tail head 等命令,凡是读取而不修改文件的操作,均衡改变文件的Access time.  
#  Modify time(mtime):是指修改文件内容的时间,只要文件内容有改动(如使用转向输出或转向附加的方式)或存盘的操作,就会改变文件的Modify time,平常我们使用ls –l查看文件时,显示的时间就是Modify time  
#  Change time(ctime):是指文件属性或文件位置改动的时间,如使用chmod,chown,mv指令集使用ln做文件的硬是连接,就会改变文件的Change time.
Copy the code

 

cat : 链接文件后输出文件内容到屏幕上,其实就是查看文件内容

crontab 执行定时任务 

Copy the code
* * * * * command to be executed
- - - - - -
| | | | | |
| | | | | --- 预执行的命令
| | | | ----- 表示星期0~7(其中星期天可以用0或7表示)
| | | ------- 表示月份1~12
| | --------- 表示日期1~31
| ----------- 表示小时1~23(0表示0点)
------------- 表示分钟1~59 每分钟用*或者 */1表示


-u user:用来设定某个用户的crontab服务;
-e:编辑某个用户的crontab文件内容。如果不指定用户,则表示编辑当前用户的crontab文件。
-l:显示某个用户的crontab文件内容,如果不指定用户,则表示显示当前用户的crontab文件内容。
-r:从/var/spool/cron目录中删除某个用户的crontab文件,如果不指定用户,则默认删除当前用户的crontab文件。
-i:在删除用户的crontab文件时给确认提示
Copy the code

 

 tar

Copy the code
-c :建立一个压缩文件的参数指令(create 的意思);
-x :解开一个压缩文件的参数指令!
-t :查看 tarfile 里面的文件!
特别注意  c/x/t 同时仅能存在一个,因为不可能同时压缩与解压缩。
-z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
-j :是否同时具有 bzip2 的属性?亦即是否需要用 bzip2 压缩? 更新压缩率更高
-v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
-f :使用档名,请留意,在 f 之后必须要立即接文件名,放在所有其他参数的后面
-p :使用原文件的原来属性(属性不会依据使用者而变)
-P :可以使用绝对路径来压缩!
-N :比后面接的日期(yyyy/mm/dd)还要新的才会被打包进新建的文件中!

tar jcf dst source1 source2 #将source1和source2压缩到dst压缩包中
# 将当前目录下所有.txt文件打包并压缩归档到文件this.tar.gz tar czvf this.tar.gz ./*.txt # 将当前目录下的this.tar.gz中的文件解压到当前目录 tar xzvf this.tar.gz ./ # 将整个 /etc 目录下的文件全部打包成为 /tmp/etc.tar tar -cvf /tmp/etc.tar /etc # 仅打包,不压缩! tar -zcvf /tmp/etc.tar.gz /etc # 打包后,以 gzip 压缩 tar -jcvf /tmp/etc.tar.bz2 /etc # 打包后,以 bzip2 压缩 # 解压文件 tar -xf a.tar.gz # tar -xf a.tar.gz -C /tmp # 指定解包路径
Copy the code

 

grep

Copy the code
格式:
grep better a.text grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] [-e PATTERN] [FILE...] 参数: -c --count #计算符合样式的列数 -l --file-with-matches #列出文件内容符合指定的样式的文件名称。 -v --revert-match #显示不包含匹配文本的所有行。 -i --ignore-case #忽略字符大小写的差别。 -o # 只显示匹配到的关键字 -n # 显示行号 -E 使用正则表达式() grep -E = egrep
初识正则表达式
^ : 匹配一行的开头 grep “^S” z.txt
$ : 匹配一行的结尾
[] : 范围匹配 [^0-9]非零到九
[a-z] : 匹配所有小写字母
[A-Z] : 匹配所有大写字母
[0-9] : 匹配所有数字;[^0-9]非零到九,^表示取反
[0-9a-zA-Z]

. : 匹配单个字符,任意一个字符
* : 表示*前面的内容出现0次或多次;[0-9][a-z]*,*只与[a-z]有关
.*:任意一个字符出现多次
+ : 表示+前面的内容出现1次或多次 [a-z] 至少出现一次小写字母
? : 表示?前面的内容出现0次或1次
cat a.txt |grep hat$ # 匹配以hat结尾的行
cat a.txt |grep ^hat # 匹配以hat开头的行
cat a.txt | grep -E "[0-9]*"  # 匹配有0到多个数字的行
cat a.txt | grep -E "[0-9]+"  # 匹配有至少有1个数字的行
cat a.txt | grep -E "[0-9]?"  # 匹配有0到1个数字的行
Copy the code

 

sed : 流编辑器,一次处理一行内容

Copy the code
sed [-nefr] [动作] [文件]
选项与参数:
-n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来
-e :直接在命令列模式上进行 sed 的动作编辑
-f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作
-r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
-i :直接修改读取的文件内容,而不是输出到终端。

动作说明: [n1[,n2]] 动作:
n1, n2 :不一定存在,一般代表选择进行动作的行数,比如,如果我的动作是需要在 10 到 20 行之间进行的,则10,20[动作行为]

动作:
#a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)
#c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
#d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
    sed  "3d"  file  #  删除第三行
    sed  "1,3d"  # 删除前三行
    sed  "1d;3d;5d"  # 删除1、3、5行
    sed  "/^$/d" #删除空行   
    sed  "/abc/d" #删除所有含有abc的行
    sed  "/abc/,/def/d" #删除abc 和 def 之间的行,包括其自身
    sed  "1,/def/d" #删除第一行到 def 之间的行,包括其自身
    sed  "/abc/,+3d " # 删除含有abc的行之后,在删除3行
    sed  "/abc/,~3d" #从含有abc的行开始,共删除3行
    sed  "1~2d"  # 从第1行开始,每2行删除一行, 删除奇数行
    sed  "2~2d"  # 从第2行开始,每2行删除一行, 删除奇数行
    sed  "$d"  # 删除最后一行
    sed  "/dd\|cc/d"  删除有dd或者cc的行
    
#i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
#p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行
    sed -n  "3p"  file  #  显示第三行
    sed -n  "1,3p"  # 显示前三行
    sed -n  "2,+3p"  # 显示第二行,及后面的三行
    sed -n  "$p"  # 显示最后一行
    sed -n "1p;3p;5p"  # 只显示文件1、3、5行
    sed -n  "$="  # 显示文件行数
#s: Alternatively, work can be directly substituted. Usually this action can be used with s regular expression, e.g. 1,20s / Old / new new / G 
    's / Old / new new / G'   
    
    Sed "s / \ (All \) / BB /" 
    Sed -R & lt "s / ( all) / bb / "
Copy the code

 

Guess you like

Origin www.cnblogs.com/yangyuanbo/p/12330120.html