linux--directory structure and file basic operation

Source: Internet
Author: User
Tags parent directory system log line editor



Second, the LINUX directory structure



Before you talk about the Linux directory structure, the first thing you need to know is that the difference between the Linux directory and the Windows directory may not be much different from the general operating experience, but it is completely different from the implementation mechanism.



A difference is reflected in the directory and storage media (disk, memory, DVD, etc.) of the relationship, the previous Windows has been mainly in storage media, mainly in the drive letter (C drive, D disk ...) ) and partition to achieve file management, and then the directory is not so important, the directory is not so significant, except for the system files in any place, any directory is not much of a relationship. So usually after Windows has been in use for a while, the file directories on the disk will appear cluttered (except for a handful of well-organized users). However, Unix/linux is the opposite, UNIX is a directory-based, Linux has inherited this excellent feature. Linux is a tree-shaped directory structure to build the entire system, can be understood as a tree directory is a user-operable system skeleton. Although essentially both the directory structure and the operating system kernel are stored on disk, the Linux disk is logically "hung" on the directory (mounted), each directory can not only use the local disk partition of the file system, but also can use the file system on the network. For example, you can use a network file system (SYSTEM,NFS) server to load a specific directory, and so on.






1. FHS Standard



The directory structure of Linux is complex, and simple and easy to say. The complication is that because the system's normal operation is based on the directory structure, for beginners, most of the directories do not know its role, important or not, especially for those who have been heavy Windows users, they will be tangled for a long time, about the software I installed where such problems. It's simple because most of the directory structures are well-defined (FHS standard) and are dead, and when you do, everything you do inside will become orderly.


FHS (English: Filesystem Hierarchy Standard Chinese: File system hierarchy Standards), most Linux versions use this form of file, FHS defines the purpose of each region in the system, The required minimum composition of the file and the directory also gives the exception handling and contradictory processing.


FHS defines the two-tier specification, and the first layer is/what file data should be placed in each of the following directories, such as the/etcsettings file that should be placed, the/bin/sbinexecutable file, and so on.



The second layer is defined for the subdirectories/usr/varof the two directories. For example/var/log, place system log files,/usr/shareplace shared data, and so on.












If you feel that you do not understand, then you can try the most realistic and intuitive way, execute the following command:


$ tree /


As for the FHS mentioned above, there is a very important thing you must understand, FHS is based on the experience of countless Linux users and developers, and will maintain the update, FHS based on the frequent use of the file system and whether or not to allow users to change freely (note, not not, the learning process, Do not be afraid of these), define the directory as a form of four interactions, as shown in the following table:





2. Directory path Path


Someone may not understand what this path means and what it does. As the name implies, the path is the route where you are going. If you want to enter a specific directory or want to get a directory of files (the directory itself is a file) then you have to use the path to find.



Use thecdcommand to switch directories, in Linux use.represents the current directory,..representing the previous level of the directory (note that we described in the previous section, to begin with the files are.hidden files, so these two directories must also be hidden, you can usels -acommand to view hidden files), indicating the last directory in which the-~current user's directory is usually representedhome. Use thepwdcommand to get the current path (absolute path).



Go to the top level directory:


$ cd ..


Go to yourhomedirectory:


$ cd ~ #or cd /home/<your username> 


Use topwdget the current path:


$ pwd
Absolute path


With respect to absolute paths, simply the complete path starting with the root "/" directory, with the directory you want to end up with, in the form of a directory that represents the directory in the directory/usr/local/binunder the root directoryusrlocalbin.


Relative path


Relative path, that is, relative to your current directory path, relative path is the current directory.as the starting point, the directory you want to the end, the form of expression such as:usr/local/bin(this assumes your current directory is the root directory). You may notice that we mean that the relative path actually does not add the one that represents the current directory,.but rather begins with the directory name, because this directory is a subdirectoryusr/of the directory, which can be omitted (in the case of.a similar cannot be omitted) If it is the top level directory of the current directory, you need to use it.., such as your current directory as ahomedirectory, the root directory should be represented as the top level directory (directory)../../homeof the previous directory (/directory).



Let's start with yourhomedirectory and enter the directory in absolute and relative paths, respectively/usr/local/bin:


/usr/local/bin# relative path $ cd : /.. /usr/local/bin


Enter a directory, you can use the absolute path can also use a relative path, then we should choose the right way to enter a directory. By intuition, you can use whichever you feel comfortable with, rather than using only one. For example, if I am currently in the/usr/local/bindirectory, I would like to go to the previous level of the local directory you say is easy to usecd ..orcd /usr/localconvenient? And if you want to get into ausrdirectory, thencd /usrit'scd ../..a little more convenient.



Tip: In the process of directory switching, please use theTabkey auto-completion, to avoid input errors, two consecutive timesTabcan display all candidate results.






Third, the basic operation of Linux files


Create a new blank file


Usingtouchcommands to create a blank file, thetouchmain function of the command is to change the time stamp of the existing file (for example, the last access time, the last modified time), but it does not add any parameters, only specify a file name, You can create a blank file with the specified file name (not overwriting a file with the same name), or you can specify the timestamp of the file at the same time, and more about the usage of thetouchcommand, which will be covered in the next file search.



Create a blank file named Test, because you do not have permissions in other directories, so you need tocd ~switch back to the user's/home/shiyanloudirectory first:


$ cd ~$ touch test
New Catalog


Use the Makemkdirdirectories command to create an empty directory, or you can specify a permission attribute to create a directory.



Create an empty directory named "Mydir":


$ mkdir mydir


Using-pparameters to create the parent directory at the same time (if the parent directory does not exist), we create a multilevel directory at the same time (this is useful when installing the software, configuring the installation path):


$ mkdir -p father/son/grandson





The following directory path, expressed as an absolute path, is also possible.



Copying files


Usecpthe (copy) command to copy a file to the specified directory.



Copy the previously created "test" file to the "/home/shiyanlou/father/son/grandson" directory:


$ cp test father/son/grandson


is not very convenient ah, if in the graphical interface you need to first copy the file in the source directory, and then into the destination directory to paste the file, and the command line operation step is one step.


Copy Directory


If you usecpthe command to copy a directory directly, the following error will occur:






To successfully copy a directory, you need to add-ror-Rparameter, meaning recursive replication, which means "personalities":


$ cp -r father family
deleting files


rmDelete a file using the (remove files or directories) command:


$ rm test


Sometimes you encounter a file that you want to delete as read-only permission, and the immediate use ofrmDelete displays a prompt, as follows:






If you want to ignore this hint and delete the file directly, you can use the-fparameter to force the deletion:


$ rm -f test
Delete Directory


As with the copy directory, to delete a directory, you also need to add-ror-Rparameter:


$ rm -r family
Moving files


Use themv(move or rename files) command to move the file (cut). To move the file "File1" to theDocumentsdirectory:



mv Source directory file destination directory:


$ mkdir Documents$ mv file1 Documents




Renaming files


Rename the file "File1" to "myfile":



mv Old filename new filename:


$ mv file1 myfile
Batch Rename


To achieve batch renaming, themvcommand is a bit out of order, and we can use a seemingly more professional commandrenameto implement it. But it has to use Perl regular expressions as parameters, about the regular expression we will be introduced in the following, only to do the demonstration, you just remember thisrenamecommand can be batch rename, and then re-study will not have any problems, after all, you have mastered a more commonmvcommand.



Batch create 5 files with wildcards :$touch file{1.. 5}. Txt# batch rename the text file with the suffix.txt to the file with the suffix.c :$rename 's/\. TXT /\

 


A simple explanation of the above commandrenameis to first use the wildcard character of the second argument to match all.txtthe suffixes of the file, and then replace the suffix of the matching files with the regular expression provided by the first parameter,.txt.cafter we have learned the commands behind ussed, I'm sure you'll understand better.





Usecat,tacAndnlcommand to view files


The first two commands are used to print the file contents to the standard output (terminal), which iscata positive sequence display, intacreverse order.


Standard input and output: when we execute a shell command line normally will automatically open three standard files, namely standard input file (stdin), default corresponding to the terminal's keyboard, standard output file (STDOUT) and standard error output file (stderr), The following two files correspond to the screen that is redirected to the terminal so that we can see the output directly. The process will get input data from the standard input file, output normal output data to the standard output file, and send the error message to the standard error file.


For example, we want to see/etcthe files that were copied from the directory beforepasswd:


$ cat passwd


You can add-nparameters to display line numbers:


$ cat -n passwd





nlcommand, add line numbers and print, which is acat -nmore professional line number than the Print command.



Here is a brief list of some of its commonly used parameters:

- b: add line number specified way, there are two main types: - a: b said whether null line, also list the line number (" cat - n "is this way) - b t: list only non-empty row number lists (the default is this way) - n: set the style of the line number, there are three main types: -n ln: line number field the far left shows - n rn: line number field, according to the right and do not add 0 - n rz: line number field, according to the right and add 0 - w: line number field is occupied by the digits (the default is 6)





You will find the use of these commands, the default terminal window size, a screen display of the content of the text, you have to use the mouse to drag the scroll bar or slide the wheel to continue to page down, if you can directly use the keyboard to turn the page, then you can use the following command to introduce.


UsemoreAndlesscommand paging to view files


If the abovecatis used to quickly view the contents of a file, then thismoreandlessis a natural to "read" The content of a file, such as the Man Manual is usedlessto display content. Themorecommands are relatively simple and can only be scrolled in one direction, whilelessfor the basis ofmoreandvi(a powerful editor we have separate courses to let you learn) development, more powerful.lessthe use of basic andmoreconsistent, specific use please check the Man manual, here only to describemorethe use of commands.



Tomoreopen a file with a commandpasswd:


$ more passwd





When turned on, only one screen is displayed by default, and the bottom of the terminal shows the current reading progress. You can use the key to scrollEnterdownSpacea line, scroll down one screen with the key, presshshow Help,qexit.


UseheadAndtailcommand to view files


These two commands, those who are impatient people should like, because they are only to view the first few lines of the file (the default is 10 lines, less than 10 lines show all) and the last few lines. Or take the passwd file example, such as when we want to see the recently added user, then we can view the/etc/passwdfile, but we also saw in front of the file, a lot of messy things, it seems really bothered. Because the system adds a new user, the user's information will be added to the end of the passwd file, then we can use thetailcommand:


$ tail /etc/passwd


Even more directly look at one line, plus the-nparameter, followed by the number of rows:


$ tail -n 1 /etc/passwd





Abouttailthe command, you have to mention it is a very good parameter-f, this parameter can be implemented to read the contents of a file and display. This allows us to dynamically view the logs to achieve real-time monitoring purposes. But I'm not going to go into this basic course approached more details about it, and interested users can learn it for themselves.




As I mentioned earlier, the types of files in Linux are not judged by file suffixes, we usually usefilecommands to view the file types:


$ file /bin/ls





Description This is an executable file that runs on a 64-bit platform and uses a dynamic link file (Shared library).




Editing files under Linux usually we will use a dedicated command line editor like (Emacs,vim,nano), because the content of the editor involved in Linux is more, and very important, so we have a separate course dedicated to this editor vim. Strongly hope that you are learning this Linux basic course you first pause here, to learn the use of the Vim editor (at least the basic operation), and then continue the course after the content, because the following will assume that you have learned the use of the Vim editor. If you want to get started faster, you can start by using the internal vim learning tutorial in Linux and enter the following command:


$ vimtutor
Relax, please.


Do you feel that there is no pressure to learn easily and happily in our environment, so it is no problem to sneak lazy occasionally? It is not very good to do so, to learn to give yourself a little pressure, strict demands on themselves. You might think that if someone can supervise it, you can learn it faster. Well, today teaches you how to summon a pair of eyes out to supervise you:


$ xeyes


You can use the following command to put it in the background to run:


$ nohup xeyes &

linux--directory structure and file basic operation


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.