Rm,mv,cp,touch of Linux basic file management commands

Source: Internet
Author: User
Tags touch command

In the Linux system, all files, even the CD-ROM also as a file, to use the CD first to establish a directory, and then by mounting to operate the disc, even the mouse, the keyboard is considered a file. Therefore, it is important to learn the relevant operation commands of the file.

RM command

RM is a commonly used command that removes one or more files or directories from a directory, and it can delete all files and subdirectories under a directory. For linked files, only the link is deleted and the original file remains unchanged.

Note: RM is a dangerous command, use with special care, especially for beginners, or the entire system will be destroyed in this command, such as root in the root directory to execute RM * RF is fatal to the Linux system. So, before we execute RM, it's a good idea to check in which directory, exactly what to delete, and keep a high level of sanity in the operation.

1) command format

RM [Options] FILENAME

2) Command function

Delete one or more files or directories in a directory, and RM does not delete the directory if the-r option is not used.

3) Command parameters

-F: Force Delete, ignore nonexistent file, do not give hint.

-I: Do an interactive delete, which will prompt you to delete

The default is no prompt, the reason is prompted because the system uses the default command alias is generally only the root user does not have this function, is also the system security measures, specific can be viewed through the alias command (if the command used a command alias can be "\" to suppress the command alias, without quotation marks)

-R: Instructs RM to delete all directories and subdirectories listed in the parameters recursively

-V: Shows the process of command execution

4) Common examples

Example one: Delete the directory of test under the/tmp directory

[[email protected] tmp]# mkdir test[[email protected] tmp]# rm testrm:cannot Remove directory ' Test ': is a directory #默认不可以删除目录 [[email protected] tmp]# lsgconfd-root mapping-root test[[email protected] tmp]# rm-r testrm:remove Direc Tory ' test '? Y[[email protected] tmp]# lsgconfd-root mapping-root

Example two: Delete the MNT subdirectory and its subdirectories under the/tmp directory and do not require one by one acknowledgment

[Email protected] tmp]# tree mnt/mnt/|--m| '--X '--y3 directories, 0 files[[email protected] tmp]# rm-rf mnt[[email protected] tmp]# lsgconfd-root mapping-root

MV Command

The MV command is the abbreviation for move, which can be used for moving files or renaming files

1) command format:

MV [option] source file or directory destination file or directory

2) Command function:

Depending on the second parameter type in the MV command (target file or target directory), when the second parameter type is a file, the MV command finishes renaming the file, at which point the source file can only have one (or the source directory name), which renames the given source file or directory to the specified destination file name. When the second parameter is a directory name that already exists, the source file or directory parameter can have more than one, and the MV command moves the source files specified by each parameter to the destination directory. When moving files across file systems, the MV is copied before the original files are deleted.

3) Command parameters:

-B: Overwrite the previous backup if you need to overwrite the file

-F: Force replication, if the destination file already exists, will not be queried and directly overwritten

-I: If the file already exists, you will be asked whether to overwrite

-U: Update if the target file already exists and source is newer

-T: Move the source file or directory specified by all parameters to the specified directory

4) Common examples:

Example one: renaming Test.txt to Test.log

[[email protected] tmp]# lsgconfd-root mapping-root test.txt[[email protected] tmp]# mv Test.txt log.txt[[email protecte D] tmp]# Lsgconfd-root log.txt mapping-root

Example two: Move the A.txt,b.txt,c.txt file to the/tmp/txt directory

[Email protected]/]# mv-t/tmp/txt/a.txt b.txt c.txt [[email protected]/]# ls/tmp/txt/a.txt b.txt c.txt

Example three: file is covered with money do a simple backup

[email protected] txt]# cat a.txt 1234567890[[email protected] txt]# cat b.txt abcdefghi[[email protected] txt]# mv-b A. TXT b.txtmv:overwrite ' b.txt '? Y[[email protected] txt]# ls-ltotal 20-rw-r--r--1 root root 4 14:57 b.txt-rw-r--r--1 root root 4 14:57  b.txt~-rw-r--r--1 root root 0 4 14:45 c.txt[[email protected] txt]# cat B.txt1234567890[[email protected] txt]# cat B.txt~abcdefghi

5) Extension:

-B does not accept parameters, and MV will read the environment variable Version_control as a backup strategy.

--backup This option specifies a total of four backup strategies if the target file is present:

1.control=none or off: not backed up.

2.control=numbered or T: Digitally numbered backup

3.control=existing or nil: if there is a digitally numbered backup, continue numbering the backup M+1...N:

There is a digitally numbered file log2.txt.~1~ before performing the MV operation, then execution will produce log2.txt~2~, and so on. If you don't have a digitally numbered file before, use the simple backup described below.

4.control=simple or never: Use simple backup: A simple backup is made before being overwritten, a simple backup can only have one copy, and a simple backup will be overwritten when it is overwritten again.

CP command

The CP command is used to copy files or directories and is one of the most commonly used commands in a Linux system.

1) command format

CP [option] source file or directory destination file or directory

2) Command function

Copy the source file to the destination file, or copy multiple source files to the destination directory.

3) Command parameters

-I: Ask before overwriting

-r: Copy all items in directory and directory

-F: Overwrite files that already exist without prompting

-A: Archive replication, often used for backup

-P: The properties of the source directory or file are all reserved

-V: Shows the process of command execution

4) Common examples

Example one: copying multiple files to the same directory

[[email protected] txt]# Cp/etc/{passwd,inittab,rc.d/rc.sysinit}/tmp/-V '/etc/passwd '/tmp/passwd '/etc/ Inittab '/tmp/inittab '/etc/rc.d/rc.sysinit '/tmp/rc.sysinit

Example Two: copying test.rb to the test directory and preserving the properties of the original file

-rw-r--r--1 root root 0 4 15:33 test.rbdrwxr-xr-x 2 root root 4096 4 14:59 txt[[email protected] tmp]# CP -P test.rb test[[email protected] tmp]# ls-l testtotal 4-rw-r--r--1 root root 0 4 15:33 test.rb

Touch command

Touch commands are not commonly used, and may be used when make is used, to modify the timestamp of a file, or to create a new file that does not exist.

1) command format

touch [Options] FILENAME

2) Command function

Change the access time and modification time of the file to the current time.

Files that do not exist will be created as empty files unless you use the-C

3) Command parameters

-A: Change access time only

-C: Do not create any files

-M: Change only the modified time

-r: Specifies the time attribute of a file instead of the current time

-T: Use time in [[CC]YY]MMDDHHMM[.SS] format instead of current time

4) Common examples

Example one: Change the time of A.txt to the current time and the file does not exist.

[[email protected] txt]# Touch A.txt[[email protected] txt]# lltotal 24-rw-r--r--1 root root 0 4 15:50 a.txt-rw-r-- r--1 root root 4 14:57 b.txt-rw-r--r--1 root root 4 14:57 b.txt~-rw-r--r--1 root root 0 4 14:45 C . txt

Example two: Change the time of C.txt to August 1 1 o'clock 1 minutes

-rw-r--r--1 root root 0 4 14:45 c.txt[[email protected] txt]# touch-ct 08010101 c.txt[[email protected] txt]# Llto Tal 24-rw-r--r--1 root root 0 4 15:50 a.txt-rw-r--r--1 root root one 4 14:57 b.txt-rw-r--r--1 root root Au G 4 14:57 b.txt~-rw-r--r--1 root root 0 1 01:01 c.txt

Example three: Change the c.txt time to be the same as the B.txt

-rw-r--r--1 root root 4 14:57 b.txt-rw-r--r--1 root root 4 14:57 b.txt~-rw-r--r--1 root root 0 1  01:01 C.txt[[email protected] txt]# touch-r b.txt c.txt[[email protected] txt]# lltotal 24-rw-r--r--1 root root 0 4 15:50 a.txt-rw-r--r--1 root root 4 14:57 b.txt-rw-r--r--1 root root 4 14:57 b.txt~-rw-r--r--1 root ro OT 0 4 14:57 c.txt



This article is from the "Small Monk" blog, be sure to keep this source http://xseng.blog.51cto.com/2513398/1535569

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.