Brother Bird's Linux private cuisine Basic study reading notes (7): Linux File and directory management

Source: Internet
Author: User
Tags touch command

This chapter mainly describes how to operate and manage the Linux system files and directories, mainly including the switch between directories, the creation and deletion of directories, the creation and deletion of files , file lookup, file content viewing, and so on.

relative paths and absolute paths have been mentioned in the previous chapter, and absolute paths refer to the path names that begin with the root directory ("/"), and the relative paths are never started by the root directory . instead, the relative path is the path name relative to the current working directory. There are several special directories in the Linux operating system:

. Represents this layer directory;

          .. Represents a previous level of directory;

-Represents a previous working directory;

~ Represents the home folder where the current user's identity resides;

~account represents the home folder of the account user;

Linux under a few common operations directory with the file command:

(1) CD (change directory) is a command to switch directories, using a CD [relative path name or absolute pathname], a small detail to note, when the CD is not followed by any path, represents the meaning of returning to his home folder (for root, that is/root/, for the general user account, which is/home/account).

(2) PWD Displays the directory where you are currently located.

(3) mkdir new directory, using the mkdir [-MP] directory name. By default, themkdir command creates a directory that needs to be created one layer at a level, but can also help you create a recursive creation of the desired directory (including the upper directory) directly by using the-p parameter.

(4) rmdir Delete empty directory, the use of rmdir [-p] Directory name, directory Deletion also requires a layer to delete, and the deleted directory must not exist in other directoriesor file, that is, the deleted directory must be an empty directory, or you can try to use the-p parameter to delete the upper directory as well.

(5) in Linux, the LS command is probably the most frequently used command, because the LS command lets us know about the information about a file or directory, including the properties, permissions, and so on.

(6) If you want to complete the copy of the file, you can use the CP command, the use of the CP source file destination file, or the CP source file 1 source file 2 source file 3 ... directory. under the default conditions, the source file of CP and the permissions of the destination file are different, the owner of the destination file is usually the command operator itself. So sometimes when we use the CP command to back up some files, we need to be aware that all the properties of the file and the permissions are fully copied, you need to use the-a or-p parameter. The CP command can also replicate the directory, and you need to use the - r parameter.

(7) RM Remove the file or directory, previously said rmdir delete directory can only delete empty directory, and "Rm-r directory name" can be deleted unless empty directory, because RM is relatively a comparison Dangerous command, so in order to fear that the file was mistakenly deleted, many distribution default to the-I parameter added to the RM command, that is, before the deletion will ask, if you want to delete directly ignore the query, you need to use \RM, by adding a backslash before the command, you can ignore the alias of the specified parameters.

(8) MV moves files and directories, or renames. Use the method for MV [-FIU] source destination or MV Source1 source2 source3 ... directory. MV In addition to moving files or directories, another use is to change the file name. For example, the MV Mvtest Mvtest2, you can rename mvtest to Mvtest2.

(9) for a given full file name, we can use a slash to separate which part is the file name, which part is the path name. Of course, you can also use the basename command to get the file name in the path, and the directory name in the path by the dirname command.


For an ordinary file, we can view the contents of the file by following several commands:

Cat: Start all of the file contents from the first line

TAC: Displays the contents of all files from the last line

nl: Displays the line number by the way

MORE: Page-by-page display of file contents

Less: page-by-page display of file content, but allows page forward

head: Show only the first few lines

Tail: Show only a few lines at the end

od: Reads the contents of the file in binary mode.

simply comparing these several commands that display the contents of a file, the Cat command is a concatenate (continuous) shorthand that displays the contents of the entire file on the screen, so the cat command has aThe problem is that when the entire file content is more than the screen size, we can only see the last display on the screen content, the results before the show is too late to see. TACThe command and cat commands are the opposite (as can be seen from the name), and the TAC command displays the contents of the file continuously on the screen from the last line of the file. NL command Display fileThe line number is automatically added to the content. Displays the contents of the file on the screen in a breath compared to the CAT,TAC and NL commands, and the more command allows paging to display the contents of the file. But more commands to viewThe contents of the file cannot be page forward, only page backward, if you want to page forward, you have to use the less command, so the lower command is more resilient than the more command . and man, the command is to call the less command to display the contents of the description file. We can also use the head or tail command to make a simple selection of the output data, head displaythe first few lines of the file, and the tail command is used to select the last few lines of the file. The OD command allows us to read non-plain text files, such as binary files, and the OD command allows us to use different types to output, such as octal, decimal, ASCII characters, and other types to output.

the next step is to introduce the touch command, which has two main features: Modify the file time and create a new file. In the Linux operating system, each file has three time parameters, namely: The file content access time Atime (access times), the content modification time mtime (modification times), the State modification time CTime ( Status time). the ls-l command displays the file's Mtime by default, which is the time the file content was last modified.

the Touch command is used by the touch [-ACDMT] file name, by default Touch is followed by a file, the three time of the file is updated to the current time, and a new empty file is automatically created if the file does not exist.

When we add a file with touch or create a new directory with the mkdir command, what is the default permission for that file or directory? This is related to the umask. You can use the Umask command to view the umask values for the current system. For example, enter the Umask command, you can get the number shape of the permission to set the score, you can also use umask-s as a symbol to display the permission settings. For example, the following conditions:

# umask

0022

# Umask-s

u=rwx, G=rx, O=rx

The umask's fractional value refers to the value that the default permission should subtract. It is important to note that the default permissions for new files and new directories are not the same . File defaults do not require executable permissions, so the default permission value of the new file is rw-rw-rw-, but for the directory, the executable permission means that the directory can be entered, so the default permissions for the directory are open, that is, the default permissions for the directory is rwxrwxrwx. But why does the output of the umask have four numbers, the first number represents the special permission, the next three numbers correspond to the owner, the user group, the other person should subtract the permission.

For example, because Umask's fractional value represents the value that the default permission should subtract , when you create a new file, the default permission for the file is rw-rw-rw-, but because the Umask 's second three number is 022, the file owner's permissions do not change. However, the user group and other people's write permissions should be subtracted, so the last permission for the new file is Rw-r--r--。 However , when you create a new directory, the permissions for the last new directory are rwxr-xr-x because the default permissions for the directory are rwxrwxrwx and the corresponding values for the umask are subtracted.

The chattr command is used to set the hidden properties of a file, and the hidden properties of these files are very helpful for system security. The most important two properties of the Chattr command are +a and +i, after setting the +a property on a file, the file can only add data and cannot modify the data, but after setting the +i property on a file, the file cannot be deleted, renamed, set connection , etc., +a and + Both of these properties can be set only by root. The lsattr command is used to view the hidden properties of a file.

a key element of this chapter is the file's three special permissions: Suid,sgid,sbit. When we use the Ls-l command to view the properties of/usr/bin/passwd this file, its first field is this:-rwsr-xr-x. It is strange that the permissions of the file are not only r,w,x, why is there another s? This involves the concept of suid.

when s appears on the file owner's X-permission position, it is called the Set UID, or suid special permission. What does SUID special permissions mean to a file? First , the suid is only valid for the binary, and then the performer needs X permissions on the file, and the SUID permission is only valid during the execution of the program, which will enable the program's performer to obtain the permissions of the program's file owner .

Sgid refers to the set GID, referred to as Sgid, when s appears in the location of the X permission of the user group to which the file belongs. Unlike suid can only be set for files, Sgid can be set for files or directories. When Sgid is set for a file, it is only useful for binary files, and the program's performer has x permission on the program, and the performer will get the permissions of the user group to which the file belongs during execution. When Sgid is set on a directory, it has the following functions: When the user enters the directory (provided that the directory has r and X permissions), the user's valid user group in this directory becomes the user group to which the directory belongs. In other words, when a user creates a new file under the directory (provided that the user has W permissions on the directory), the user group to which the new file is created is the same as the owning user group for that directory.

sbit refers to the sticky bit, translated into Chinese should be referred to as "stick bit", this flag is only valid for the directory, the file has no effect. Sbit the function of a directory is that when a user creates a file or directory under that directory (provided that the user has write access to the directory), only himself and Root have the right to delete the file.

we already know that when you change file permissions with a number pattern, you only need to use a combination of three numbers. Now if you want to set suid/sgid/sbit permissions, just precede the previous three digits with a number, 4 for suid,2 on behalf of sgid,1 for Sbit. So you want to change the permissions of a file to "-rwsr-xr-x", just use the command chmod 4775 filename.

Maybe the reader will still feel that these three special permissions are abstract, and I'll explain them in a special instance later. Recently looking at the famous <<APUE>> (Advanced Programming for UNIX environments), where One chapter is about the properties of the file, and the file attributes are explained by each member of the structure of stat, which is " Set user ID Bit "," set user group ID bit " in fact, "stick bit" was originally set for the binary file, makes it possible for those programs that run frequently to reside in memory, so there is a" sticky "saying, but later Unix extends the" stick bit "to the directory, which is also related to the function. Well, I'll summarize it later when I write my reading notes about <<APUE>>.

for a file, we can use the file this command to get the basic data type of this document, through File command, you can determine whether a file is an ASCII text file, or is a data file, or a binary file (you can also see if you have used the dynamic function library and so on)

Sometimes we need to know the full path to a file, or to query where a file is placed. For a script file, we can query the full path of a command through which, using the which command, but which is looking for the full file name of the "executable" according to the path that the PATH environment variable regulates. Find the first one that stops looking . If you want to find all executed files with the same name, you need to add the-a parameter.

Both the Whereis and locate commands can be used to find arbitrary files. the Whereis and locate commands use the database to find files, so it's fairly fast . The Linux operating system records all the file information in a database file, and the Whereis and locate commands are used to locate the file using this database file. Although this approach is fairly fast, the database file update is not real-time (different distribution, CentOS updates the data file once a day), so when you use the Whereis or locate command to find a file You may have found a file that has been deleted, or the newly created file is not queried. Of course, you can also manually update this database file through UpdateDB, so that the search is relatively accurate after that.

The Find command can also be used to find a file, but the Find command finds the hard disk directly, so the Find command finds the exact result, but it takes a lot of time. and the Find command can find files that meet certain requirements.


This chapter is a summary of Linux commands:

Cd,pwd,mkdir,rmdir,rm,mv,basename,dirname,cat,tac,nl,more,less,head,tail,od,touch, Umask,chattr, Lsattr,file,which,whereis,locate,find.




Brother Bird's Linux private cuisine Basic study reading notes (7): Linux File 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: 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.