Linux File and directory management

Source: Internet
Author: User

A directory related to the path 1 directory:

Exchange Directory Command CD
. Represents this Layer directory
.. Represents a previous level directory

    • Represents a previous working directory
      ~ Represents the home directory where [current user identity] resides
      Two directories are present in all directories [.] current directory [...] Top level directory of current directory

[Email protected] ~]# Ls-al.
Total Dosage 76
Dr-xr-x---. Root root 4096 January 28 16:02.
Drwxr-xr-x. Root root 4096 January 28 15:56..
Drwxr-xr-x. 3 root root 67 January 10:58 AAA
---x--x--x. 1 root root 0 January 13:49 aaa.py
-RW-------. 1 root root 13194 January 16:02. bash_history
-rw-r--r--. 1 root root 18 December bash_logout
-rw-r--r--. 1 root root 176 December Bash_profile
-rw-r--r--. 1 root root 176 December BASHRC
DRWX------. Root root 4096 January 10:36. Cache
Drwxr-xr-x. Root root 4096 January 11:43. config
-rw-r--r--. 1 root root 100 December CSHRC
DRWX------. 3 root root 24 December 11:52. Dbus
-RW-------. 1 root root 16 December 11:55. Esd_auth
DRWX------. 3 root root 19 January 10:36. gnome2
DRWX------. 2 root root 6 January 10:36. gnome2_private
-RW-------. 1 root root 3100 January 28 15:57. Iceauthority
Drwxr-xr-x. 4 root root 35 January 19:24. java
DRWX------. 3 root root 4096 January 11:43. KDE
DRWX------. 3 root root 18 December 11:55. Local
Drwxr-xr-x. 4 root root 37 January 10:36. Mozilla
-RW-------. 1 root root 0 January 20:01. mysql_history
Drwxr-xr-x. 4 root root 32 January 22 19:23. PyCharmCE2017.1
-rw-r--r--. 1 root root 129 December TCSHRC
-RW-------. 1 root root 6573 January 11:08. Viminfo
Drwxr-x--x. 2 root root 29 January 14:47. Virt-manager
-RW-------. 1 root root 134 January 28 16:02. Xauthority
Drwxr-xr-x. 2 root root 6 December 27 11:54 Public
Drwxr-xr-x. 2 root root 6 December 27 11:54 templates
Drwxr-xr-x. 2 root root 6 December 27 11:54 Video
Drwxr-xr-x. 2 root root 6 December 27 11:54 's Photos
Drwxr-xr-x. 2 root root 6 December 27 11:54 Document
Drwxr-xr-x. 2 root root 6 January 28 10:38 Download
Drwxr-xr-x. 2 root root 6 December 27 11:54 Music
Drwxr-xr-x. 2 root root 6 January 28 15:48 Desktop
[Email protected] ~]# Ls-al. /
Total Dosage 40
Drwxr-xr-x. Root root 4096 January 28 15:56.
Drwxr-xr-x. Root root 4096 January 28 15:56..
lrwxrwxrwx. 1 root root 7 December 19:18 Bin--Usr/bin
Dr-xr-xr-x. 3 root root 4096 December 11:52 boot
Drwxr-xr-x. Root root 3300 January 15:57 Dev
Drwxr-xr-x. 154 root root 8192 January 15:56 etc
Drwxr-xr-x. 3 root root 16 January 11:54 FTP
Drwxr-xr-x. 3 root root 18 December 19:40 Home
lrwxrwxrwx. 1 root root 7 December 19:18 Lib--usr/lib
lrwxrwxrwx. 1 root root 9 December 19:18 lib64-usr/lib64
Drwxr-xr-x. 2 root root 6 March media
Drwxrwxrwx. 2 root root 6 mnt March
Drwxr-xr-x. 4 root root 30 January opt
Dr-xr-xr-x. 495 root root 0 January 15:55 proc
Dr-xr-x---. Root root 4096 January 16:02 root
Drwxr-xr-x. Root root 1360 January 20:18 run
lrwxrwxrwx. 1 root root 8 December 19:18 sbin-Usr/sbin
Drwxr-xr-x. 3 root root 19 January 16:27 SRV
Dr-xr-xr-x. Root root 0 January 15:56 sys
DRWXRWXRWT. Root root 4096 January 20:55 tmp
Drwxr-xr-x. Root root 4096 December 19:18 usr
Drwxr-xr-x. Root root 4096 January 15:56 var

1 CD (Change directory, Exchange catalog)
2 pwd Displays the directory where you are currently
3 mkdir [-MP] Directory name
-M: Profile-by-file permissions
-P: Creating a recursive directory
4 rmdir (delete [empty] directory)
rmdir [-p] Directory name
-P: Along with the upper empty directory to delete, the deleted directory must not exist in other directories or files.

2 about path variables for executing files: $PATH

Why can I execute the LS command at any time because/bin/ls is in the path of the environment variable
To view the value of an environment variable:
[Email protected] ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

PATH (must be uppercase) This variable is a bunch of directories, each directory is separated by a colon (:), each directory is a sequence of points,

[Email protected] ~]# mv/bin/ls/root/
[[email protected] ~]# ls
-bash:/usr/bin/ls: No file or directory
[Email protected] ~]#/root/ls
AAA aaa.py ls Public template video picture document download music Desktop
[Email protected] ~]#./ls
AAA aaa.py ls Public template video picture document download music Desktop

Add ROOT to Path:
[[email protected] ~]# path= "{path}:/root"
[[email protected] ~]# ls
AAA aaa.py ls Public template video picture document download music Desktop
Move LS to/bin, and test
[Email protected] ~]# mv/root/ls/bin/
[[email protected] ~]# ls
AAA aaa.py Public template video picture document download music Desktop
Path NOTE:
1 different identity users preset path, the default can be arbitrarily executed instructions are also different
2 path can be modified
3 A file that specifies an instruction directly using an absolute path or relative path is executed more correctly than the search path.
4 instructions should be placed in the correct directory, the implementation will be convenient
5 this catalog [.] is best not to be placed in path.

Two files and directory management 1 files and directories view: LS

ls [-aadffhilnrrst] file or directory name:
Options and Parameters:
-A: All files, together with hidden files (beginning with. 's files)
-A: All files, together with hidden documents, but not included. With the. These two directories
-D: Lists only the directory itself, not the file data in the directory
-F: List results directly instead of sorting, LS presets are sorted by one file name
-F: According to the file, directory and other information, given additional data structure, (: On behalf of theexecutable file,/: representative directory, = The socket file, I for the FIFO file)
-H: Display the file capacity in a more readable way
-I: List inode numbers
-l; Long data serial out, including data such as attributes and permissions of the file
-N: Lists UID and GID instead of user and group names
-r: Reverse output of the sort structure,
-R: Listed along with subdirectory content, equals all files in that directory are displayed
-S: Sort files by size, not file name
-T: Sort by time,
--color=never: Do not give color display based on file characteristics
--color=always: Display Color
--color=auto: Let the system decide whether to give the color according to the setting
--full-time: Output in full time mode
--time={atime,ctime}: Output access time or Change permission attribute time (CTime) instead of content change time (modification times)

2 Copy, delete and move: CP RM mv1 CP Copy

CP [-ADFILPRSU] source file (source) destination file (destination)
Options and Parameters:
-A: equivalent to the meaning of-dr--preserver=all
-D: If the source file is a property of a linked file, the link file property is copied instead of the file itself.
-F: Force means that if the target file already exists and cannot be opened, then it is removed and then tried.
-I: If the target file (destination) already exists, the action will be asked before overwriting.
-L: Link file for hard link creation instead of copying the file itself
-P: Copy along with file attributes (includes permissions, user, time)
-R: Recursive continuous replication for the assignment behavior of the directory
-S: Copy as a symbolic link file (symbolic link), also a shortcut file
-u:destination is copied only if the destination is newer than the source, or if the destination file does not exist
--preserver=all: In addition to the-p permission-related parameters, the SELinux attribute is added, links, xattr, etc. are also assigned.
If the source file has more than two, the final target file must be a directory.

2 rm (remove files or directories)

RM [-fir] File or directory
-F: Ignore files that do not exist
-I: Interactive mode. Ask the user if the action is being deleted
-R: Recursive Delete,

3 MV (move files and directories, or rename)

MV [-fin] source folder destination Folder
Options and Parameters:
-f:force mandatory meaning, if present, is also directly covered
-I: If the target file already exists, you will be asked to overwrite
-U: If the target file already exists and source is newer, update (update)

3 getting the file name and directory name

[[email protected]/]# basename/etc/sysconfig/network get file name
Network
[[email protected]/]# dirname/etc/sysconfig/network Get directory Name
/etc/sysconfig

Three-file Content Lookup 1 Direct view file Contents 1 cat [-abbntv]

Options and Parameters
-A: Equivalent to-vet's consolidation option to list some special characters instead of blanks
-B: List line numbers, line numbers only for non-blank lines, blank lines without line numbers
-E: Displays the end of the break character $
-N: Print travel numbers, along with blank lines, with line numbers, unlike the-B option
-T: Display the [tab] key in ^i
-V: Lists some special characters that can't be seen

2 TAC (Reverse list) 3 NL (Add line number to print)

NL [-BNW] File
Options and Parameters:
-B: Specifies the way the line number is specified, there are two main types
-B A: line numbers are also listed, whether or not they are blank lines
-B T: If there is a blank line, do not list the line number (default) for the empty row
-N: List the method of line number representation, there are three main kinds:
-N LN: line number is displayed on the left of the screen
-N RN: The line number is displayed to the right of the field and does not add 0
[Email protected]/]# nl-n rn/etc/passwd
1 Root:x:0:0:root:/root:/bin/bash
2 Bin:x:1:1:bin:/bin:/sbin/nologin
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 Adm:x:3:4:adm:/var/adm:/sbin/nologin
5 Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 Sync:x:5:0:sync:/sbin:/bin/sync
-N RZ: line number in its own field of the most room display, and add 0
[Email protected]/]# nl-n rz/etc/passwd
000001 Root:x:0:0:root:/root:/bin/bash
000002 Bin:x:1:1:bin:/bin:/sbin/nologin
000003 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
000004 Adm:x:3:4:adm:/var/adm:/sbin/nologin
000005 Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
000006 Sync:x:5:0:sync:/sbin:/bin/sync
-W: The number of characters that the line Number field occupies.

2 Flip View 1 more (one page flip)

Parameters:
SPACEBAR: Represents a PAGE Down
Enter: Represents a downward line
/string: Represents the keyword in this display, down search [string]
: F: Displays the file name immediately and the number of rows currently displayed
[Email protected]/]# MORE/ETC/PASSWD
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Operator:x:11:0:operator:/root:/sbin/nologin
Games:x:12:100:games:/usr/games:/sbin/nologin
Ftp:x:14:50:ftp User:/var/ftp:/sbin/nologin
Nobody:x:99:99:nobody:/:/sbin/nologin
Dbus:x:81:81:system message Bus:/:/sbin/nologin
Polkitd:x:999:998:user for Polkitd:/:/sbin/nologin
Apache:x:48:48:apache:/usr/share/httpd:/sbin/nologin
Unbound:x:998:996:unbound DNS Resolver:/etc/unbound:/sbin/nologin
Colord:x:997:995:user for Colord:/var/lib/colord:/sbin/nologin
Usbmuxd:x:113:113:usbmuxd User:/:/sbin/nologin
Ntp:x:38:38::/etc/ntp:/sbin/nologin
Libstoragemgmt:x:996:994:daemon account for Libstoragemgmt:/var/run/lsm:/sbin/nologin
Avahi:x:70:70:avahi MDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
Avahi-autoipd:x:170:170:avahi Ipv4ll Stack:/var/lib/avahi-autoipd:/sbin/nologin
saslauth:x:995:76: "SASLAUTHD user":/run/saslauthd:/sbin/nologin
Qemu:x:107:107:qemu User:/:/sbin/nologin
"/ETC/PASSWD" line 25th
Q: The delegate immediately leaves more and no longer displays the contents of the file
b or [Ctrl]-b: Represents a page back,

2 Less (one page, one flip)

Parameters:
SPACEBAR: Flip one PAGE Down
[PageDown]: Flip one PAGE Down
[PageUp]: Flip one page Up
/string: Search down [string] function
String: Next search for [string] function
N: Ser Previous Search
N: Reverse repeats the previous search
G: The first line of this information in the forward way
G: The last line of the data in the forward path
Q: Exit less

3 Data interception 1 head (take out the first few lines)

Head-n Number File
Number is row count

2 tail (take back a few lines)

Tail-n Number File
-N: followed by a number that represents the meaning of a few lines
-F: Indicates continuous detection of the following file name until the ctrl-c is pressed to end tail detection

The default is to remove the following 10 lines of the file.

4 Non-plain text documents: OD

od [-t TYPE] File
Parameters or options:
-T: The output of various types (type) can be followed, for example:
A: Use default characters to output
C: Use ASCII characters to output
D[size]: Use decimal to output data, with each integer occupying size bytes
F[size]: Use floating-point numbers (floating) to output data, each of which occupies size bytes
O[size]: Use octal to output data
x [size]: Use hexadecimal to output data
[Email protected] ~]# od-t d/etc/passwd
0000000 1953460082 809138234) 1916416058 980709231
0000020 1869574703 1647262324) 1647275625 174617441
0000040 980314466 976304760) 1768045105 1647262318

5 Modify file time or build new document touch

Modification Time (Mtime):
When the file's [content data] changes, the time is updated, and the content data is directly the contents of the file, not the attributes or permissions of the file
Status Time (CTime):
When the [status] of the file changes, the time is updated, and the permissions or properties are changed
Access Time (Atime):
When the contents of the file are fetched, the read time (access) is updated, and the atime changes when the cat reads
By default, LS displays the file's Mtime, which is the last time the content of the file was changed
Touch [-ACDMT] File
Options and Parameters:
-A: Revise access time only
-C: Only the time to modify the file, if the file does not exist do not create a new file
-D: You can follow the date with the revision instead of the current date, or you can use the--date= "date or time"
-M: Modify Mtime only
-T: can be followed with revision time instead of the current time, in the format [YYYYMMDDHHMM]

Four default permissions and hidden permissions for files and directories

Linux In addition to the basic r,w, permissions, in the traditional EXT2/EXT3/EXT4 file system, you can also set other system hidden properties, which can be set using Chattr, and lsattr to view.

1 file preset permissions: Umask1 Umaks: The default value of the user's permissions at the time of setting up a file or directory.

[Email protected] ~]# umask
0022
[Email protected] ~]# umask-s
U=rwx,g=rx,o=rx
The first group of Umaks is used for special permissions.
Default file and directory Umaks
The umask of the file does not have the executable permission, that is, the maximum is 666
User is established as [directory], because X is related to whether it can enter this directory, so the default is all the group Yin is open, also 777
Umaks's score refers to the privilege that the default value needs to be lost.
Because Umask is 022, user has not been given any permissions, but group and other W permissions have been removed.

2 use and importance of Umaks: thematic production

The change of Umaks
Umaks 002
Can change
[Email protected] ~]# umask
0002
[email protected] ~]# touch ABCD
[[email protected] ~]# ls
AAA aaa.py ABCD Public template video picture document download music Desktop
[[email protected] ~]# ll ABCD
-rw-rw-r--. 1 root root 0 January 18:57 ABCD
[[email protected] ~]# umask root Umaks is 022
0022
[[email protected] ~]# su-admin general user Umaks is 002
[Email protected] ~]$ umask
0002

2 File Hidden Properties

The CHATTR directive can only be fully effective on EXT2/EXT3/EXT4 Linux legacy file Systems, and others may not be completely supported, such as XFS only supports partial parameters
To view the file system type:
[Email protected] ~]# df-t
File system type 1k-block used available% mount point
/dev/mapper/rhel-root xfs 18348032 16734720 1613312 92%/
Devtmpfs Devtmpfs 1008528 0 1008528 0%/dev
Tmpfs tmpfs 1017812 1017732 1%/DEV/SHM
Tmpfs tmpfs 1017812 9084 1008728 1%/run
Tmpfs tmpfs 1017812 0 1017812 0%/sys/fs/cgroup
/DEV/SDA1 xfs 508588 122644 385944 25%/boot

1 chattr [+-=] [asacdistu] file or directory name

Options and Parameters:

    • : Add a special parameter, other parameters that are not understood
      -: Removal of a particular parameter, other existing parameters will not move
      =: Set a certain, and only the following parameters
      A: When you set a This property, if you have access to this file, his access time atime will not be modified, can avoid the I/O slower and excessive access to the disk
      S: General file is written asynchronously to disk, if you add s This property, when you make any file modification, the change [synchronous] write to disk
      A: After setting a, this file will only add data, not delete or modify the data, only the root can set this property
      C: After this property is set, it will automatically compress the file, when it is read, it will be automatically decompressed, but when it is stored, it will be compressed and stored
      D: When the dump program is executed, setting the D property will allow the file (directory) to be not backed up by dump
      I: can make a file [can not be deleted, renamed, set links can not write or add data], for system security is very helpful, only the root can set this property
      [email protected] ~]# touch ABCD
      [Email protected] ~]# chattr +i ABCD
      [Email protected] ~]# RM ABCD
      RM: Do you want to delete the normal empty file "ABCD"? Y
      RM: Unable to delete "ABCD": Operation not allowed
      [Email protected] ~]# chattr-i ABCD
      [Email protected] ~]# RM ABCD
      RM: Do you want to delete the normal empty file "ABCD"? Y

S: When the file is set to the S property, if the file is deleted, he will completely remove the hard disk space, if deleted by mistake, can not be rescued at all.
U: In contrast to S, when using U to configure a file, if the file is deleted, the data content actually exists on the disk and can be used to rescue the file.

2 Show hidden properties of a file (lsattr)

lsattr [-adr] File or directory
-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 name within the directory
-r: Data along with subdirectories are also listed
[email protected] ~]# touch ABCD
[Email protected] ~]# chattr +ais ABCD
[Email protected] ~]# lsattr ABCD
--s-ia----------ABCD

3 Special permissions for files: Suid,sgid,sbit1 set UID

When s appears on the permissions of the file owner X, this is called the set UID, referred to as SUID,
Limitations and features of SUID:
SUID permissions are valid only for binary programs
Performer has x executable permission for the program
This permission is valid only in the course of executing the program
The performer will have the permissions of the program owner (owner).

[Email protected] ~]# LL/USR/BIN/PASSWD
-rwsr-xr-x. 1 root root 27832 January 2014/usr/bin/passwd
[Email protected] ~]# Ll/etc/shadow
----------. 1 root root 1322 January 11:32/etc/shadow

2 Set GID

When s appears in Group X, it is called set GID,
[Email protected] ~]# ll/usr/bin/locate
-rwx--s--x. 1 root slocate 40504 January 2014/usr/bin/locate
Sgid Effect:
Sgid Useful for binary programs
The program performer has x permissions for the program.
The performer will receive support for the program group during execution

[Email protected] ~]# ll/usr/bin/locate/var/lib/mlocate/mlocate.db
-rwx--s--x. 1 root slocate 40504 January 2014/usr/bin/locate
-rw-r-----. 1 root slocate 4540251 January 14:16/VAR/LIB/MLOCATE/MLOCATE.D

When a directory has Sgid permissions set, he will have the following features:
Users can enter this directory when they have r and X permissions for this directory
A valid group of users in this directory will become a group of directories
User: If the user has permission to W in this directory, the new file created by the consumer will be the same as the group for this directory.

3 Sticky bit

Sbit is currently only valid for the directory and has no effect on the file.
Sbit the role of the directory is:
When the user has w,x permissions for this directory, that is, the permission to write
When a user creates a file or directory under that directory, only himself and Root have the right to delete the file

4 Suid/sgid/sbit permission setting

4 for Suid
2 for Sgid
1 for Sbit
[email protected] ~]# touch ABCD
[Email protected] ~]# chmod 4755 ABCD
[[email protected] ~]# ll ABCD
-rwsr-xr-x. 1 root root 0 January 19:57 ABCD

4 Observing file Types files

[Email protected] ~]# file/var/lib/mlocate/mlocate.db
/var/lib/mlocate/mlocate.db:data
[Email protected] ~]# FILE/USR/BIN/PASSWD
/usr/bin/passwd:setuid ELF 64-bit LSB Shared Object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for Gnu/linux 2.6.32, buildid[sha1]=0x91a7160a019b7f5f754264d920e257522c5bce67, stripped

5 command and File search 1 script file name search

which
which [-A] command
-A: Lists all instructions that can be found in the path directory, and more than one of the found command names
[email protected] ~]# which ifconfig
/sbin/ifconfig
[email protected] ~]# which which
Alias Which= ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde '
/bin/alias
/usr/bin/which

2 Search for file names

Whereis (Find file filenames in some specific directories)
Whereis [-BMSU] file or directory name
Options and Parameters:
-L: Can list several main directories of Whereis back query
-B: Only binary format files are found
-M: Just find the file under the description file manual path
-S: Only source files are found
-U: Search for other special files not included in the above three items

[Email protected] ~]# Whereis ifconfig
Ifconfig:/usr/sbin/ifconfig/usr/share/man/man8/ifconfig.8.gz
[Email protected] ~]# Whereis passwd
passwd:/usr/bin/passwd/etc/passwd/usr/share/man/man1/passwd.1.gz/usr/share/man/man5/passwd.5.gz
[Email protected] ~]# whereis-m passwd
passwd:/usr/share/man/man1/passwd.1.gz/usr/share/man/man5/passwd.5.gz

Locate/updatedb
Locate [-ir] keyword
Options and Parameters:
-I: Ignore case differences
-C: Do not output file name, only calculate the number of files found
-L: Only a few lines in the meaning-L 5 means output 5 rows
-S: Outputs information about the database file used by the locate, including the number of files/directories recorded in the database, etc.
-R: Can be followed by the normal representation of the display mode
[Email protected] ~]# locate-l 5 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/etc/vsftpd/vuser_passwd.db

[Email protected] ~]# locate-s
Database/var/lib/mlocate/mlocate.db:
16,122 folders
202,444 files
10,405,544 number of bytes in the file name
4,540,251 bytes for storing the database

Find
Find [PATH] [option] [action]
Options and Parameters
Time-related options: Total-atime,-ctime,-mtime,
-mtime N: N is a number, meaning a file that has been changed over the nth days
-mtime +n: Lists file names that have been changed before n days (excluding n days themselves)
-mtime-n: Lists the file names of the content that have been changed within n days (including the N-day itself)
-newer File:file for an existing file, list file names that are newer than

Linux File and directory management

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.