Analysis of common Linux File Management commands

Source: Internet
Author: User
Tags touch command

Analysis of common Linux File Management commands

1. What are the file management commands on Linux? Common usage and related examples.

(1) Directory management commands
-- Ls: list content in a specified directory
Format: ls [OPTION]... [FILE]...
-A: displays all files, including hidden files.
-A: displays all files except .. and ..
-L, -- long: displays the detailed attributes of a file.
-H: converts the file size in units, which may affect the accuracy.
-D: view the directory itself rather than its internal files
-R: displays objects in reverse order.
-R: recursive display of Files
Example: ls-lah/-- show all files in the/directory in detail (including hidden files)
Ls-ldh/etc -- Detailed display of the/etc directory itself
Ls-lhv/-- display all files in/directory in reverse order (including hidden files)
Ls-R/etc -- recursively display all files in/etc
-- Mkdir: create a directory
Format: mkdir [OPTION]... DIRECTORY...
-P: automatically create the parent directory as needed
-M: Permission given when creating a directory
Example: mkdir-p/data/test/A/B -- recursively create three directories:/test/A/B Under the/data Directory
Mkdir-m 711-p/data/MODE/A -- recursively create the MODE/A directory in the/data directory and specify the permission of directory A as 711.
-- Rmdir: delete a directory
Format: rmdir [OPTION]... DIRECTORY...
-P: Delete the directory if its parent directory is empty.
Example: rmdir-p/data/test/A -- after directory A is deleted, the directory test is empty and deleted together.
-- Cd: Switch Directories
Example: cd...: switch to the parent directory
Cd ~ : Switch back to your home directory
Cd-: switch back and forth between the last directory and the current directory
-- Pwd: displays the current directory
(2) file management commands
-- Cp: Copy
Format: single-SOURCE replication: cp [OPTION]... [-T] source dest (if DEST does not exist, it is created and overwritten)
Multi-SOURCE replication: cp [OPTION]... SOURCE... DIRECTORY (the DEST must be directory)
-I: Interactive replication, that is, reminding the user to confirm before Overwriting
-F: forcibly overwrite the target file
-R,-R: recursive copy directory
Example: cp-if/data/example 1-32.16.txt/data/test -- test must be a directory to copy the three files to test.
Cp-r/data/practice -- copy the content in the data directory and directory together to practice
-- Mv: Cut
Format: single-SOURCE replication: mv [OPTION]... [-T] source dest (if DEST does not exist, it is created and overwritten)
Multi-SOURCE replication: mv [OPTION]... SOURCE... DIRECTORY (the DEST must be directory)
-I: Interactive replication, that is, reminding the user to confirm before Overwriting
-F: forcibly overwrite the target file
Example: mv-I/data/upload 1-32.16.txt/practice -- cut the three txt files in the/data directory to the/practice directory
-- Rm: Delete
Format: rm [OPTION]... FILE...
-I: Interactive replication, that is, reminding the user to confirm before Overwriting
-F: forcibly overwrite the target file
-R,-R: recursive processing. Delete all files under the specified directory, including directories.
Example: rm-rf/practice -- recursively delete/practice directory
(3) text content management commands
-- Cat: forward view of Text Content
Format: cat [OPTION]... [FILE]...
-N: Number of the displayed text line
-E: displays the end symbol of a row $
Example: cat-n/etc/fstab -- View/etc/fatab content and display the row number
-- Tac: view text content in flashback
Format: tac [OPTION]... [FILE]...
Example: tac/etc/passwd -- view text content in flashback
-- Head: displays text. The first 10 lines are displayed by default.
Format: head [OPTION]... [FILE]...
-N #: show text header # Line Content
Example: head-5/etc/passwd -- display the content of the first five lines of the/etc/passwd file
-- Tail: displays text. The last 10 lines are displayed by default.
Format: tail [OPTION]... [FILE]...
-N #: show text # Line Content
-F: view the content at the end of the file and do not exit. The new row is displayed.
Example: tail-8/etc/passwd -- display the last eight lines of content in the/etc/passwd file
-- More: the text content is displayed on the split screen, and the display stops after each display.
Format: more [options] file [...]
Space key: displays the content of the next screen of Text
Enter: only the next line of text is displayed.
Key B: displays the content of the previous text screen.
Q: Exit
-- Less: text content is displayed on the split screen and does not exit.
Format: less [options] file [...]
Space key: displays the content of the next screen of Text
Enter: only the next line of text is displayed.
Key B: displays the content of the previous text screen.
Q: Exit

2. bash's working features-Command Execution status return values and command line expansion involved content and its example demonstration.

(1) After the bash command is executed, the execution result is determined by the Status return value:

Success: 0 is returned.

Failed: 1-255 is returned.

Note: After the command is executed, its status return value is saved in bash's special variable "$ ?" .

Example:

(2) bash command line expansion:

~ : Automatically expand to the Home Directory of the current user

~ USERNAME: automatically expands to the Home Directory of the specified user

The content of {}:{} can be a list of separated paths, which can be expanded into multiple paths.

Example:

3. Use the command line expansion function to complete the following exercises:

(1) Create a_c, a_d, B _c, B _d IN THE/tmp directory.

(2) create/tmp/mylinux directory:
Mylinux/
── Bin
── Boot
│ ── Grub
── Dev
── Etc
│ ── Rc. d
│ ── Init. d
│ ── Sysconfig
│ ── Network-scripts
── Lib
│ ── Modules
── Lib64
── Proc
── Sbin
── Sys
── Tmp
── Usr
│ ── Local
│ ── Bin
│ ── Sbin
── Var
── Lock
── Log
── Run

4. What are the metadata information of a file, and what are their meanings? How can I view them? How to modify the file timestamp information.

(1) data in Linux files can be divided into two types: data, that is, the actual content of the file (data itself) and metadata, used to describe the features of a file (the attribute of the data ).

(2) run the stat command to view the metadata of the file,

The metadata of the File includes the File name, Size, data Block, I/O Block, Device, and Inode), Links, Access, Uid, Gid, and Access Time) the latest file modification Time and the latest file attribute Change Time ).

(3) Use the touch command to modify the timestamp information of the file, where:

-C: the specified file is not created if it does not exist (all time changes );

-A: only modify access time (the actual change time will change );

-M: only modify the modify time (the actual change time will change );

-T: Use the specified date and time, in the format of [[CC] YY] MMDDhhmm [. ss] (access time and modify time are the specified time, and change time is the current system time)

Example:

5. How to define the alias of a command and reference the execution result of another command in the command?

You can use the alias COMMAND to view the alias of all commands or define the alias. the alias format is alias NAME = COMMAND (the alias defined is only valid for the current shell terminal ).

To cancel an alias, run the unalias command.

Example:

6. Show All files or directories in the/var directory that start with l and end with a lowercase letter and contain at least one digit (which can contain other characters) in the middle.

Ls-d/var/l * [[: digit:] * [[: lower:]

7. Display files or directories starting with any number and ending with a non-number in the/etc directory.

Ls-d/etc/[0-9] * [^ 0-9] Or ls-d/etc/[[: digit:] * [^ [: digit:]

8. The/etc directory starts with a non-letter and is followed by a file or directory with a letter and any other characters of any length.

Ls-d/etc/[^ [: alpha:] [[: alpha:] *

9. Create a file starting with tfile in the/tmp directory, followed by the current date and time, the file name is like: tfile-2016-05-27-09-32-22.

Touch/tmp/tfile-$ (date '+ % Y-% m-% d-% H-% M-% s ')

10. Copy all files or directories starting with p and ending with non-numbers in the/etc directory to the/tmp/mytest1 directory.

Cp-r/etc/p * [^ [: digit:]/tmp/mytest1/

11. Copy all files or directories ending with. d In the/etc directory to the/tmp/mytest2 directory.

Cp-r/etc/*. d/tmp/mytest2/

12. Copy all files starting with l, m, or n and ending with. conf to the/tmp/mytest3 directory.

Cp-r/etc/[l, m, n] *. conf/tmp/mytest3/

This article permanently updates link: https://www.bkjia.com/Linux/2018-02/151109.htm

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.