linux--Archives and Directory Management

Source: Internet
Author: User
Tags touch command

Here is a summary of some of the recent Learning files and directory management knowledge points
Blog Park-Bang Bang sauce Good ********

******************************related operations for directories**********************************
1. Special Catalogue
(1).: Represents this Layer directory
(2).. : Represents a previous level directory
(3)-: Represents the previous working directory
(4) ~: The home directory on behalf of "Current user identity"
(5) ~account: The home directory of the user who represents account

2. Transforming CatalogsCD (change directory)

3. Display the directory where you are currently locatedPWD (Print working Directory)
PWD [-P]
-P: Shows the exact path instead of using the link path
Because/var/mail is a link file, linked to/var/spool/mail

4. Create a new directoryMkdir
mkdir [-MP] Directory name
-M: Permissions for direct configuration files
-P: Help to directly set up the desired directory (including the upper directory) recursively
Such as:
Mkdir-m 711 Test1 #设定目录权限为rwx--x--x
Mkdir-p Test1/test2/test3 #自动建立多层目录

5. Delete the [empty] directoryRmDir
rmdir [-p] Directory name
-P: Also deleted along with the upper empty directory
Such as:
Mkdir-p Test1/test2/test3
Rmdir-p Test1/test2/test3 #删除test3文件夹, its upper directory test2, and the upper directory test1 are deleted
Note: There are no other directories or files in Test1,test2,test3 in the previous example, otherwise the deletion will fail.
If you want to delete everything in the directory, use Rm-r test so that the test folder and everything underneath it will be deleted.

6. Variables for executing file paths: $PATH
When we execute an instruction, for example [LS], the system will follow the path to the directory defined by each path to search for an executable file named LS, if in the directory defined by path has more than one executable file named LS, then the first search of the same name will be executed first.
Both Root and general users have/bin this directory is within the path variable, but the generic user's path does not contain any sbin directories.
If the command is moved, the system cannot go through the path path to find it. We can only specify the file name of the execution command directly by using an absolute path or a relative path.
This directory (.) is best not to be placed in path.

*****************************Archives and Directory Management******************************
1. File and catalog view LS
ls [-ADHILRT] directory name
-A: All files, together with hidden files, are listed
-D: Lists only the directory itself, not the profile data in the directory
-H: List the file capacity in a human-readable way (GB,KB, etc.)
-I: List inode numbers
-L: Long data string output, including file properties and permissions, and so on data
-r: Reverse output of the sort results
-T: Sort by content change time instead of file name
--color=never: Do not give color display according to file characteristics
--color=always: Display Color
--color=auto: Let the system decide whether to give the color according to the setting
--full-time: The modified time of the output file in full time mode (year, month, day, hour, minute)
--time= (atime,ctime): Output access time or Change permission attribute time (CTime) instead of content change time
Note: When you do not add parameters, the file names of non-hidden files are displayed by default, sorted by file name, and the color represented by the file name is displayed

2. Copy CP
CP [-ADFIPR] source destination
-A: equivalent to-PDR
-D: If source is the property of a soft-linked file (link file), then the file property of the link is copied instead of the file itself, meaning that the copied files are also link files.
-F: If the target file already exists and cannot be opened, move it out and try again
-I: If destination already exists, ask for coverage
-P: Copy the past with the attributes of the archive, instead of using the default properties (Backup common)
-R: Recursive continuous Replication for directory replication behavior
-L: Link file for hard link creation, not copy file itself
-S: Copy into symbolic link file (symbolic Link), i.e. shortcut file
-U: Update destination If destination is older than source (backup is often used).

1) If the source has more than 2, then the last destination must be a directory.
2) The copied file must have Read permission, or the copy fails.
3) When making a backup, some special privilege files that require special attention, such as password file/etc/passwd and some configuration files, cannot be copied directly with CP, and the option to fully copy the file permissions must be added with-A or-P (in the default case, (owner and group are instruction operators themselves, but if the operator itself cannot modify the owner and group of the file at will, the-p parameter cannot change the files owner and group)
4) After copying the files to other users, pay attention to the file permissions (read and write execution and owner, etc.), otherwise, other people will not be able to modify the files you give.


Additional knowledge:
Hard Links: is a pointer to the file index point, and the system does not reassign the inode for it.
Create command: LN existingfile newfilehard
1) After creation, use Ls-il to view the inode number of the NewFile, Existingfile and Newfilehard inode numbers should be the same, indicating that they are the same file, so the property size is the same as the original file.
-->13059-rwx------2 Root root 57 November 5 15:30 Existingfile
-->13059-rwx------2 Root root 57 November 5 15:30 Newfilehard
2) Create a hard link for each file, and their number of links will be +1.
1) do not create links between files in different file systems;
2) Only Superuser can create hard links for directories;

Soft Links(Symbolic Link): is a shortcut to a soft-connected file that will be assigned a new inode number, and he overcomes the lack of hard links
Build command: Ln-s existingfile newfilesoft
Note: The soft connect original and linked files will have different inode numbers, indicating that they are two different files.
-->13059-rwx------2 Root root 57 November 5 15:30 Existingfile
-->13059-rwx------2 Root root 57 November 5 15:30 Newfilehard
-->13060 lrwxrwxrwx 1 root root 5 November 5 15:40 newfilesoft->existingfile
Disadvantage: When the original file is moved from one directory to another directory, access to the symbolic link file will fail.

3. Remove the file or directory RM
RM [-fir] File or directory
-f:force, ignore non-existent files, do not display warning message
-I: interactive mode, ask before deleting
-R: Recursive delete
1) The preset has been added to the option of-I, so you have to press Y to delete, if you want to terminate the delete operation, you can enter n or press CTRL + C.
2) If you want to ignore the preset options, you can add a backslash before the command, ignoring the specified option for alias. such as: \rm-r/tmp/test
3) to see if the command has an alias, enter alias to display all of the system's aliases, or the type command name will show whether the command is alias,keyword,function,builtin,file or not.
4) To delete a-start file (such as-aaa-), delete it as follows: RM./-aaa-(./is the meaning of this directory)

4. Move the file or directory MV
MV [-FIU] source destination
-f:force, if the target file already exists, do not ask, directly overwrite
-I: Ask if the target file already exists
-U: Overwrite if target file already exists and source is newer
1) You can rename a file: MV file_name file_new_name (equivalent to rename command)
2) Move n files to a directory: MV File1 file2 Destination_dir

5. Get the filename and directory name of the path
(1) Get the file name:
(2) Get the path

*******************************file Content Query********************************
1. Start displaying the contents of the archive from the first line cat
Cat [-ABENTV] filename
-A: equivalent to-vet
-B: List line numbers, only row numbers are displayed for non-blank lines, blank lines are not marked with line numbers
-E: Displays the end of the break symbol $
-N: List line numbers, blank lines also mark line numbers
-T: The TAB key is displayed in ^i
-V: Lists some special characters that can't be seen

2. Start the reverse display of the TAC from the last line
TAC filename

3. When displayed, output line number NL
NL [-BNW] filename
-B: Specify the way the line number is
-B A: List line numbers (similar to cat-n), whether or not there is a blank line
-B T: If there is a blank line, the line number of the empty row is not listed (similar to Cat-b, which is the default value)
-N: How line numbers are listed
-N LN: line number is displayed on the left of the screen
-N RN: The line number is displayed on the very right of its own field and does not add 0;
-N RZ: The line number is displayed on the very right of the field, plus 0;
-W: Number of digits occupied by the line number field
Such as:
Nl-b a-n rz-w 3/etc/issue
001 CentOS Release 5.3 (Final)
002 Kernel \ r on an \m

4. Page-by-page display of archive content more
More filename
The last line indicates the percentage that is currently displayed, and you can enter useful instructions in the last row
Available keys:
1) SPACEBAR: Turn down one page
2) Enter: Turn down one line
3)/String: In the currently displayed content, look down the string
4): F: Displays the file name and the number of rows currently displayed
5) Q: Leave more and no longer show
6) B or ctrl+b: page back, only valid for the file, invalid for the pipeline

5. One page and one page display and can page less
Less filename
Available keys:
1) SPACEBAR: Turn down one page
2) Pagedown: Turn down one line
3) Pageup: Turn up one line
4)/String: In the currently displayed content, look down the string
5) String: In the currently displayed content, look up the string
6) N: Repeat the previous search
7) N: Reverse repeats the previous search
8) Q: Leave more and no longer show

6. Only a few lines of head
Head-n number filename
-N Number: Displays the number line
Such as:
Head-n -100/etc/man.config #number可以是负数
---> Display all previous rows, but not the last 100 rows

7. Just look at the tail a few lines tail
Tail-n number filename
-N Number: Displays the last number line with no options, the last 10 rows (including blank lines) are displayed by default
Such as:
Tail-n +100/etc/man.config
---> If the document has 140 rows, start at line 100th and display all data after 100-140 rows
Displays the 11th to 20th lines of the document: Head-n 20/etc/man/config|tail-n 10

8. Read the content in binary mode od
od [-t TYPE] filename
-T: followed by output of various types of type, such as
A: Use the default characters to output
C: Use ASCII characters to output
Such as:
Od-t occ/etc/issue
---> in eight-in-a-table to store values and ASCII

9. Modify the file time or create a new file Touch
1) 3 time parameters under LS instruction:
*modification Time (mtime): Update this when the contents of the file change
*status Time (CTime): Update this when file status (such as permissions and attributes) changes
*access Time (Atime): When the file content is read (such as cat), update this
The LS command displays mtime by default, to show other times, so you can:
--->ls-l--time=atime/ctime filename

2) Touch command
touch [-ACDMT] filename
-A: Revision only atime
-C: Only when the file is modified (mtime,atime), if the file does not exist, do not create a new file
-D: Date or time after which you want to revise or use--date=
-M: Revision mtime only
-T: The time after which you want to revise YYMMDDHHMM
* Three times to view documents:
ll bashrc;ll--time=atime bashrc;ll--time=ctime BASHRC #ll是一个别名, equivalent to Ls-l
---> List of BASHRC files latest revision time, read time and status modification time
* Time to repair documents
touch-d "2 days Ago" BASHRC
---> Only mtime and atime have changed, CTime cannot be modified

*******************************default permissions and hidden permissions for files and directories********************************
1. File Preset permissions Umask
Umask [-S]
Umask: Displays the default value of the current user's permissions when creating the file or directory, shown in digital form, 7-fractional = actual permissions
Umask-s: Show permission defaults as a symbol type

On the properties of the default permissions, the directory is not the same as the archive:
1) file 666=-rw-rw-rw-(general file does not give X permission)
2) directory 777=drwxrwxrwx (the x permission of the directory determines whether it can use the CD command to enter the directory)

If the current user is root, the umask command is displayed back to 022 (that is,----w--w--), so
1) When creating the file: (-rw-rw-rw-)-(----w--w--) = (-rw-r--r--)
2) When the directory is established: (DRWXRWXRWX)-(----w--w--) = (drwxr-xr-x)

If you want to change the default value of Umask to 003, you can enter: Umask 003, then perform a new file or document operation
(in general, Root umask is 022, the average user's umask is 002)

2. Configure the Archive hidden properties chattr
chattr [+-=][asacdistu] Filename/dirname
+: Add a special parameter, other existing parameters do not move
-: Remove a special parameter, other existing parameters do not move
=: Set a certain, and only the following parameters

A: Access time Atime will not be modified when access to this file
S:sync, any changes to the file will be written to the disk synchronously
A: Only the root can set a property, after setting the file can only add data, can not delete or modify data (common)
C: Automatic compression when saving, automatic decompression when reading
When the D:dump program is executed, the file will not be backed up by dump
I: Make file cannot be deleted, renamed or set link, unable to write or add data (common)
S: If the file is deleted, it will be completely removed from the hard disk space and cannot be saved
U: In contrast to S, using U to configure the file, if the file is deleted, the data is still in the hard disk, can save

3. Show archive hidden Properties lsattr
lsattr [-adr] Filename/dirname
-A: The properties of hidden files are also displayed
-D: If a directory is followed, only the properties of the directory itself, not the file names within the directory, are listed
-R: Data is also listed along with subdirectories

4. File Special Permissions
1) Set UID-->u=user,owner
View/usr/bin/passwd permissions, get:-rwsr-xr-x, it contains S, that is, suid special permissions.
Illustrates the following things:
1. First the S permission is only valid for the binary program, so passwd is a binary file (not the same as the shell script file OH)
2. Who (root or general user) wants to execute the passwd program can be executed. So all performers have X execution permissions on it.
3. When the performer executes the passed program, it only temporarily gets root permission to execute it.
4. The performer is the owner of the program
When a file has special permissions for suid, the user temporarily has permission to the program owner during execution when the user executes the binary program.

2) Set GID
When the logo is in the x item of the owner of the file for S (SUID), that s is set GID at the X of the group!
When a directory has special permissions for Sgid, the file group created on behalf of the user under this directory will be the same as the group name for that directory.

How to create a binary file
DD If=/dev/zero of=hello.txt bs=1m count=1
/dev/zero---The device provides an endless 0
/dev/null---Nickname is bottomless pit, you can output any data to it

3) sbit permissions
Premise: User A is a group or other person's identity to directory A, and has a W permission to the directory. This means that user a can "delete/rename/move" a directory or file created by anyone in the directory.
Sbit Permissions: If you add sbit permissions to directory A, this is user a can only "delete/rename/move" The files or directories that they have created.

4) Setting of Suid/sgid/sbit permissions
(1) Value: Suid:4 (u+s) sgid:2 (g+s) sbit:1 (o+t)
(2) Setting: Add a value before the original RWX permission value
such as: 4755=-rwsr-xr-x,6755=-rwsr-sr-x,1755=rwxr-xr-t
(3) Uppercase S,T:7666=-RWSRWSRWT (666 is no x permission, so the uppercase St is the null permission)

5. Observing file Types
File filename---What is the document type: Ascii,data,binary

*******************************Search for files and directories********************************
Search for script file names
1) which (search execution script)
which [-A] command
-A: The instructions found by the path directory CAs are listed, not just the first found command name
(note: Which is based on the user's path path to look for, different users of the path is not the same, so the results of the search will not be the same)
Such as:
which CD--->no CD in ...
#which的预设是找PATH内规范的目录, but the CD is a bash built-in instruction, so it can't be found.

#find命令不常用 because it is slow and consumes hard drives. The Linux system will record all the files in the system in a database file, Whereis and locate use the database to search for data, so the speed is fast, and there is no actual search hard disk, compared to save time.
2) Whereis (search for specific files)
Whereis [-BMSU] file or directory name
-B: Only binary format files are found
-M: Find only files under the manual path of the description file
-S: Find source files only
-U: Search for other special files that are not in the above three items

3) Locate
Locate [-ir] keyword
-I: Ignore case differences
-R: Can be followed by the display of regular expressions

Limitations: Locate is looking for data that is found by the data in the established database/var/lib/mlocate/, and does not go directly to the hard drive to access the data.
But the database is built by default only once a day (each distribution is different), so when you create a new file, but before the database update search, then locate will tell you can not find!
FIX: Manually update the database! Simply type "UpdateDB" on it. UpdateDB instruction will read the settings of/etc/updatedb/conf this configuration file, then go to the hard disk to search the file name action, and finally update the entire database file.

4) Find
Find [PATH] [option] [action]
Option time-related options:
-mtime N-N is a number that means a file that has been altered in [one day] before the
-mtime +n: Lists files that have been altered before n days (excluding n days themselves)
-mtime-n: Lists files that have been changed within n days (including the N-day itself)
-newer File:file is an existing file that lists new file names than file

7 6 5 4 3 2 1 now

Other related to the user or group name, related to the file permissions and names, and some additional action options, it is not said ~

**********************************the relationship between authority and instruction***********************************
1. Allow users to access the basic permissions of a directory:
1) Available directives: such as CD
2) permissions required for the directory: User has at least X permissions to the directory
3) Additional requirements: If the user wants LS, also need r permission

2. The user reads the basic permission of a file in a directory:
1) Available directives: such as cat,more,less, etc.
2) permissions required for the directory: User has at least X permissions to the directory
3) Permissions required for the file: User has at least r access to the file

3. Let the user modify the basic permissions of a file:
1) Available directives: e.g. VI
2) permissions required for the directory: the user has at least X permission in the directory where the file resides
3) Permissions required for the file: User has at least r access to the file

4. Have a user create a profile with basic permissions:
Permissions required for the directory: the user has w,x permissions in the directory, with a focus on W

5. Allow the user to enter a directory and execute the basic permissions of an instruction in that directory:
1) Permissions required for the directory: the user has at least X permission in the directory where the file resides
2) Permissions required for the file: User has at least x access to the file

Example: There are 2 accounts in the system, Alex,arod, which, in addition to their own groups, also support a group called Project. Assume that these 2 users jointly own the/srv/ahome directory development rights, and that directory is not allowed to enter the query. How do I set permissions for this directory?
#进入root账户, create 2 new accounts, Alex and Arod, and group project
[[email protected] ~]$ su root
[[Email protected]]# groupadd Project
[Email protected]]# useradd-g Project Alex
[[Email protected]]# useradd-g Project Arod
[[Email protected]]# ID Alex
uid=501 (Alex) gid=502 (Alex) groups=502 (Alex), 501 (project)
[[Email protected]]# ID Arod
uid=502 (Arod) gid=503 (Arod) groups=503 (Arod), 501 (project)
[[Email protected]]# mkdir/srv/ahome
[[Email protected]]# ll/srv/ahome/
Total 0
#由上下可知, for the catalog LS to add the parameter-D
[Email protected]]# ll-d/srv/ahome/
Drwxr-xr-x. 2 root root 4096 Nov 11:04/srv/ahome/
#改变 the group to which the/srv/ahome directory belongs, this directory can be accessed by accounts within the project group
[Email protected]]# chgrp project/srv/ahome/
[Email protected]]# ll-d/srv/ahome/
Drwxr-xr-x. 2 root project 4096 Nov 11:04/srv/ahome/
#改变群组后, you also need to give the Group W permissions (create a new delete file in the renamed directory).
#由root的预设权限umask可知root的是022, that is, the new directory does not have W permissions under the group.
[Email protected] ahome]# chmod 770/srv/ahome/
[Email protected] ahome]# ll-d/srv/ahome/
DRWXRWX---. 2 root project 4096 Nov 11:04/srv/ahome/
[email protected] ahome]# su Alex
[email protected] ahome]$ touch ABCD
[[email protected] ahome]$ su root
[email protected] ahome]# su Arod
#下面可见alex新建的文档群组是alex, not a common group Project,arod cannot edit it.
[[email protected] ahome]$ ll ABCD
-rw-rw-r--. 1 Alex Alex 0 Nov 11:12 ABCD
[[Email protected] ahome]$ exit
#目录具有SGID的特殊权限时, the file group created on behalf of the user under this directory will be the same as the group name for that directory.
[Email protected] ahome]# chmod 2770/srv/ahome/
[Email protected] ahome]# ll-d/srv/ahome/
Drwxrws---. 2 root project 4096 Nov 11:12/srv/ahome/
[email protected] ahome]# su Alex
[[email protected] ahome]$ Touch 1234
#修改目录权限后可见: Alex the new group for document 1234 is Project ~
[email protected] ahome]$ LL
Total 0
-rw-rw-r--. 1 Alex Project 0 Nov 11 11:14 1234
-rw-rw-r--. 1 Alex Alex 0 Nov 11:12 ABCD
[Email protected] ahome]$

linux--Archives and Directory Management

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: 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.