Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

2.7 File directory management commands


May 23, 2021 That's what Linux should learn



So far, we've learned Linux commands like we're on the ground, and although we haven't seen results yet, we've actually done a pretty good job. In the daily operation and operation of Linux system, it is also necessary to master the creation, modification, copying, cutting, name change and deletion of files.

1. Touch command

The touch command is used to create a blank file or to set the time of the file in the format of "touch (options) .

This touch command is fairly simple in creating blank text files, so simple that it doesn't have to be rolled out. F or example, the touch linuxprobe command can create a blank text file called linuxprobe. F or touch commands, the difficult operation is mainly reflected in setting the file content modification time (mtime), file permissions or properties change time (ctime) and file read time (atime). The parameters of the touch command and their function are shown in Table 2-11.

The parameters of the Table 2-11 touch command and its role

Parameter Action -a Only modify "read time" (atime) -m Only modify "modify time" (mtime) -d modify both atime and mtime

Next, let's use the ls command to see when a file was modified, then modify the file, and finally, the touch command to set the modified file time to the time before the modification (as many hackers do):

[root@linuxprobe ~]# ls -l anaconda-ks.cfg -rw-------. 1 root root 1213 May 4 15:44 anaconda-ks.cfg [root@linuxprobe ~]# echo "Visit the LinuxProbe.com to learn linux skills" >> anaconda-ks.cfg [root@linuxprobe ~]# ls -l anaconda-ks.cfg -rw-------. 1 root root 1260 Aug 2 01:26 anaconda-ks.cfg [root@linuxprobe ~]# touch -d "2017-05-04 15:44" anaconda-ks.cfg [root@linuxprobe ~]# ls -l anaconda-ks.cfg -rw-------. 1 root root 1260 May 4 15:44 anaconda-ks.cfg

2. mkdir command

The mkdir command is used to create a blank directory in the format "mkdir (Options) Directory".

In Linux systems, folders are one of the most common file types. In addition to creating a single blank directory, the mkdir command combines -p parameters to recursively create a file directory with nested overlay relationships.

[root@linuxprobe ~]# mkdir linuxprobe [root@linuxprobe ~]# cd linuxprobe [root@linuxprobe linuxprobe]# mkdir -p a/b/c/d/e [root@linuxprobe linuxprobe]# cd a [root@linuxprobe a]# cd b [root@linuxprobe b] #

3. cp command

The cp command is used to copy a file or directory in the format "cp (options) source file target file." Everyone should be no stranger to file copy operations, in Linux systems, replication operations are divided into three specific cases:

If the target file is a directory, the source file is copied to that directory;

If the target file is also a normal file, it is asked if you want to override it;

If the target file does not exist, a normal copy operation is performed.

The parameters of the cp command and their function are shown in Table 2-12.

The parameters of the Table 2-12 cp command and its role

Parameter Role -p Preserves the properties of the original file -d If the object is a "linked file", the property of the "linked file" is preserved -r recursive continuous replication (for directory) - i If the target file exists, ask if the override -a is equivalent to -pdr (p, d, r for the above parameters)

Next, use touch to create a normal blank file called install .log, copy it as a backup file named x.log, and finally use the ls command to view the files in the directory:

[root@linuxprobe ~]# touch install.log [root@linuxprobe ~]# cp install.log x.log [root@linuxprobe ~]# ls install.log x.log

4. mv command

The mv command is used to cut or rename a file in the form of "mv (options) source file (target path| target file name).

The cut operation is different from the copy operation because it deletes the source file by default, keeping only the cut file. If you cut a file in the same directory, you actually rename it:

[root@linuxprobe ~]# mv x.log linux.log [root@linuxprobe ~]# ls install.log linux.log

5. rm command

The rm command is used to delete a file or directory in the format of "rm (Options) File".

When deleting files in a Linux system, you are asked by default if you want to do the deletion, and if you don't want to see this repeated confirmation all the time, you can follow the -f parameter with the rm command to force the deletion. I n addition, if you want to delete a directory, you need to add an -r parameter after the rm command before you can delete it. Let's try to delete the install .log and linux files .log created earlier:

[root@linuxprobe ~]# rm install.log rm: remove regular empty file ‘install.log’? y [root@linuxprobe ~]# rm -f linux.log [root@linuxprobe ~]# ls [root@linuxprobe ~] #

6. dd command

The dd command is used to copy or convert files in the specified size and number of blocks of data in the format "dd (parameters)."

The dd command is an important and characteristic command that allows users to copy the contents of a file by a specified size and number of blocks of data. O f course, you can also convert the data in it during the replication process if you want. T here is a device file called /dev/zero in the Linux system, which is full of philosophical theory every time it is explained in class. B ecause this file does not take up system storage space, but provides endless data, you can use it as an input file for the dd command to generate a file of a specified size. The parameters of the dd command and their function are shown in Table 2-13.

The parameters of the Table 2-13 dd command and its effect

Parameter The file name of the if input of the output of the file name bs sets the size of each "block" count to set the number of "blocks" to copy

For example, we can use the dd command to remove a block of data of 560MB from the /dev/zero device file and save it as a file 560_file file. Once you understand this command, you'll be free to create files of any size later:

[root@linuxprobe ~]# dd if=/dev/zero of=560_file count=1 bs=560M 1+0 records in 1+0 records out 587202560 bytes (587 MB) copied, 27.1755 s, 21.6 MB/s

The functionality of the dd command is by no means limited to copying files. If you want to make a disc from an optical drive device into an iso-formatted mirror file, you need third-party software in your Windows system, but you can use the dd command directly in a Linux system to suppress the disc image file and turn it into an immediately usable iso image:

[root@linuxprobe ~]# dd if=/dev/cdrom of=RHEL-server-7.0-x86_64-LinuxProbe.Com.iso 7311360+0 records in 7311360+0 records out 3743416320 bytes (3.7 GB) copied, 370.758 s, 10.1 MB/s

Considering that some readers will struggle with the relationship between bs block size and count block count, here's an example of eating. S uppose Xiaoming's meal quantity (i.e. demand) is a fixed value, the size of the spoon used to eat is the size of bs blocks, and the number of times the spoon is eaten is counting blocks. T o be full (to meet your needs), Xiaoming needs to balance the size of the spoon (bs block size) with the number of times you eat with a spoon (count blocks). T he larger the spoon, the fewer times you will eat with the spoon. As can be seen from above, bs and count are used to specify the size of capacity, as long as the needs can be met, can be combined and matched at will.

7. File command

The file command is used to view the type of file in the format "file file name."

In Linux systems, because text, directories, devices, and so on are all referred to as files, and we can't know the specific file type by suffix alone, we need to use the file command to look at the file type.

[root@linuxprobe ~]# file anaconda-ks.cfg anaconda-ks.cfg: ASCII text [root@linuxprobe ~]# file /dev/sda /dev/sda: block special