Linux learning record IV - operation files and directories

preface

Why learn to use these command-line programs? It is much easier to use the graphics file manager to perform some tasks performed by these commands. However, although using the graphics file manager can easily realize simple file operations, it is easier to use the command-line program for complex tasks, because the command-line program has powerful functions and flexible operations.

1, Some preliminary knowledge

1. Wildcard

Wildcard is a shell feature that makes the command line so powerful. Because the shell often needs to use file names, it provides some special characters to help you quickly point to a set of file names. These special characters are called wildcards. Wildcards (also known as file name substitution) allow users to select file names based on character patterns.

Wildcard:

wildcardMatches
*Match any number of characters (including 0 and 1)
?Match any single character (excluding 0)
[characters]Match any character belonging to the character set
[!characters]Matches any character that does not belong to the character set
[[:class:]]Matches any character belonging to the specified character class

Common character classes:

Character classMatches
[:alnum;]Match any letter or number
[: alpha:] matches any letter
[:digit:]Match any number
[:lower:]Match any lowercase letter
[:upper:]Match any capital letter

The use of wildcards makes it possible to build complex filtering criteria for file names.
for instance:

formMatches
g*Any file starting with g
b*.txtStart with b, with any number of characters in the middle, and start with txt
date???Any file that starts with date and is followed by three characters
[abc]*Any file starting with any one of abc
abc.[0-9][0-9][0-9]Any number that begins with abc followed by 3
[[:upper:]]*Any document that begins with a capital letter
[![:digit:]]*Any file that does not begin with a number
*[[:lower:]123]Any document that ends with a lowercase letter or any one of the numbers 1, 2, or 3

Wildcards can be used with any command that uses a file named parameter, which makes the operation very flexible.

2. Link

The link gives me the feeling that it is very similar to the pointer in C language.

Hard link

Hard link was originally used by UNIX to create links. By default, each file has a hard link, which will give the file a name. When a hard link is created, an additional directory entry is also created for the file.
Hard links have two important limitations:

  1. Hard links cannot reference files outside their own file system. In other words, a hard link cannot refer to a file that is not on the same disk partition as the hard link
  2. Hard links cannot reference directories
    Hard links are no different from the file itself. When the hard link is deleted, the link is only deleted, and the file itself still exists (in other words, the space is not released. Like a pointer?), Unless all links to the file are deleted.

Soft link (symbolic link)

Symbolic links are created to overcome the limitations of hard links. Symbolic links work by creating a special type of file that contains text pointers to referenced files or directories.
The file pointed to by the symbolic link is almost the same as the symbolic link itself. When a symbolic link is deleted, the file itself still exists (associative pointer). If the file is deleted before the symbolic link, the symbolic link still exists, but does not point to any file, which is called a bad link (associative null pointer).

2, Introduction to several commands

Note:... Indicates that the parameters are repeated

1. mkdir -- create directory

I think this command is the abbreviation of make directory. mkdir command is used to create a directory. The format is as follows:;
mkdir directory...
You can create one or more directories at a time,
mkdir dir1 create a single dir1 directory
MKDIR dir1, dir2 and dir3 create directories named dire1, dire2 and dire3 respectively

2. cp -- copy files and directories

It can be used in two different ways;

  1. Copy a single file or directory item1 to file or directory item2:
    cp item1 item2
  2. Copy multiple items (files or directories) into one directory;
    cp item... directory
    cp command options (short option and equivalent long option):
Short optionsLong optionmeaning
-a– ArchiveCopy files and directories and their attributes, including ownership and permissions. The copied file has the default properties of the file operated by the user.
-i– interactivePrompt the user for confirmation before overwriting an existing file. If this option is not specified, cp overwrites the file by default
-r– recursiveCopy directories and their contents recursively. This option (or - a option) is required when copying directories
-u– updateWhen copying files from one directory to another, only those files that do not exist in the target directory or the updated files of the corresponding files in the target directory will be copied
-v– verboseWhen copying a file, an informational message is displayed

3. mv -- remove and rename files

The use of mv is basically similar to cp. mv can perform file movement and file renaming, depending on how it is used.
mv item1 item2
Move (or rename) the file (or directory) item1 to item2;
mv item... directory
Move one or more entries from one directory to another
mv options:

Short optionsLong optionmeaning
-i– interactivePrompt the user for confirmation before overwriting an existing file. If this option is not specified, mv overwrites the file by default
-u– updateWhen moving files from one directory to another, only those files that do not exist in the target directory or the update files of the corresponding files in the target directory will be moved
-v– verboseAn informational message is displayed when a file is moved

4. rm -- delete files and directories

rm is used to delete files and directories. The operations are as follows
rm item...
item is the name of one or more files (or directories)
Be careful with rm, because UNIX like operating systems (such as Linux) do not contain commands for restore and delete operations!!! The default user of Linux system is wise and knows what he is doing (maybe he didn't expect a user like me).
Tip: when the rm command is used with wildcards, in addition to examining the input carefully, you can use the ls command to test the wildcards in advance. This will show the pre deleted files. Then you can use the arrow keys to return the commands before and replace the ls with rm.
rm options:

Short optionsLong optionmeaning
-i– interactivePrompt the user for confirmation before deleting an existing file. If this option is not specified, the rm command deletes the file by default
-r– recursiveDelete the directory recursively. In other words, if the deleted directory has subdirectories, it should also be deleted. To delete a directory, you must specify this option
-f–forceIgnore nonexistent files without prompting for confirmation. This option overrides the -- i option
-v– verose (verbose)Show informational messages when deleting files

5. ln -- create link

I think this is the abbreviation of link, abbreviated as ln.
ln command can be used in two ways;
ln file link -- used to create hard links
ln -s item link - used to create symbolic links, where the item can be either a file or a directory

3, Actual combat drill

After learning so many commands, I'm very itchy. Let's practice it together.

1. Create directory

First use mkdir to create a directory to play. Let's call it playground. We'll drill in this file.

yrf-tan@yrftan-Lenovo-G50-70m:~$ pwd
/home/yrf-tan
yrf-tan@yrftan-Lenovo-G50-70m:~$ mkdir playground
yrf-tan@yrftan-Lenovo-G50-70m:~$ cd playground
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ 

Then create two files in playground: dir1 and dir2;

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ mkdir dir1 dir2
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ cd dir1
yrf-tan@yrftan-Lenovo-G50-70m:~/playground/dir1$ cd ..
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ 

2. Copy file

Place some data in the directory created by us through the cp command, and put the passwd file in the / etc directory (user account list). Pay attention to the use of shortcuts to the current working directory, that is, add a single dot at the end of the command.

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ cp /etc/passwd .
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls
dir1  dir2  passwd


Use the - v option again

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ cp -v /etc/passwd .
'/etc/passwd' -> './passwd'

This time, a message is displayed indicating what it did. Because the cp command assumes that the user knows his current operation, there is no warning. Add the - i option to do it again;

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ cp -i /etc/passwd .
cp: overwrite './passwd'? 
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls
dir1  dir2  passwd

By typing y at the prompt, the file will be rewritten, and any other characters will cause the cp command to retain the file. I typed enter, so the file is retained.

3. Move and rename files

Let's change the name of the passwd file,

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ mv passwd happy
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls
dir1  dir2  happy

Now move the happy file to each file and then move it back;

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls
dir1  dir2  happy
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ mv happy dir1
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls dir1
happy
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ mv dir1/happy dir2
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls dir2
happy
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ mv dir2/happy .
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls
dir1  dir2  happy

4. Create link

It feels like creating links and using pointers. It's also very sour to use.

Create hard link

Now, let's try to create some links to data files.

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ln happy happy-gaoxin
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls
dir1  dir2  happy  happy-gaoxin
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ln happy dir1/happy-gaooxin
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls dir1
happy-gaooxin
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ln happy dir2/happy-gaoxin
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls dir2
happy-gaoxin
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls
dir1  dir2  happy  happy-gaoxin
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls -l
total 16
drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:02 dir1
drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:03 dir2
-rw-r--r-- 4 yrf-tan yrf-tan 2410 5 April 23-14:33 happy
-rw-r--r-- 4 yrf-tan yrf-tan 2410 5 April 23-14:33 happy-gaoxin
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls -li
total 16
926829 drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:02 dir1
926830 drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:03 dir2
926831 -rw-r--r-- 4 yrf-tan yrf-tan 2410 5 April 23-14:33 happy
926831 -rw-r--r-- 4 yrf-tan yrf-tan 2410 5 April 23-14:33 happy-gaoxin

The second field of happy and happy Gaoxin is 4, which is the number of hard links of the file happy (Note: because the file name is created by the link, a file usually has at least one link). Through ls -l, we can find that the file sizes of happy and happy Gaoxin are the same, and then through ls -li, we can find that their index node numbers are the same, So they are the same files.

Create symbolic links

A symbolic link is a special file that contains a text pointer to the target file or directory.
The creation method is similar to creating a hard link:

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ln -s happy happy-sb
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls
dir1  dir2  happy  happy-gaoxin  happy-sb

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls -l
total 16
drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:02 dir1
drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:03 dir2
-rw-r--r-- 4 yrf-tan yrf-tan 2410 5 April 23-14:33 happy
-rw-r--r-- 4 yrf-tan yrf-tan 2410 5 April 23-14:33 happy-gaoxin
lrwxrwxrwx 1 yrf-tan yrf-tan    5 5 May 23-15:13 happy-sb -> happy

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ln -s /playground/happy dir1/happy-sb
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls -l dir1
total 4
-rw-r--r-- 4 yrf-tan yrf-tan 2410 5 April 23-14:33 happy-gaooxin
lrwxrwxrwx 1 yrf-tan yrf-tan   17 5 May 23-15:17 happy-sb -> /playground/happy

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ln -s /playground/happy dir2/happy-sb
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls -l dir2
total 4
-rw-r--r-- 4 yrf-tan yrf-tan 2410 5 April 23-14:33 happy-gaoxin
lrwxrwxrwx 1 yrf-tan yrf-tan   17 5 May 23-15:18 happy-sb -> /playground/happy

Because when you create a symbolic link, you also create a text describing where the target file is associated with the symbolic link, so you should indicate the path.
The detailed list of dir1 and dir2 shows that happy sb is a symbolic link, which is confirmed by the first character "l" in the first field, and the length of the symbolic link file is 17, which is the number of characters in the / playground/happy string, not the length of the file it points to. When creating symbolic links, you can use absolute pathnames and relative pathnames, which are preferable because relative pathnames allow directories containing symbolic links to be renamed and / or moved without breaking links. In addition to ordinary files, symbolic links can also reference directories.

5. Delete files and directories

We use the rm command to clear the playground directory,
First, delete the hard link;

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls
dir1  dir2  happy  happy-gaoxin  happy-sb
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ rm happy-gaoxin
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls -l
total 12
drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:17 dir1
drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:18 dir2
-rw-r--r-- 3 yrf-tan yrf-tan 2410 5 April 23-14:33 happy
lrwxrwxrwx 1 yrf-tan yrf-tan    5 5 May 23-15:13 happy-sb -> happy

Happy Gaoxin has been deleted, and the number of links in the file happy has changed from 4 to 3
Try deleting happy again:

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ rm -i happy
rm: remove regular file 'happy'? y
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls
dir1  dir2  happy-sb
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls -l
total 8
drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:17 dir1
drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:18 dir2
lrwxrwxrwx 1 yrf-tan yrf-tan    5 5 23rd, 15th:13 happy-sb -> happy

Happy has been deleted, and happy sb has become a bad link. In my terminal, this link is marked in red (not shown in csdn articles).

Try deleting symbolic links:

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ rm happy-sb
yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ ls -l
total 8
drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:17 dir1
drwxrwxr-x 2 yrf-tan yrf-tan 4096 5 May 23-15:18 dir2

It is worth noting that most operations are targeted at the link target, not the link itself. rm takes the link itself as the object. When deleting a link, the link itself is deleted, but the target file still exists.

Finally, let's delete playground. To do this, we need to return to the home directory and use rm's recursive option to delete the playground directory and all its subdirectories.

yrf-tan@yrftan-Lenovo-G50-70m:~/playground$ cd
yrf-tan@yrftan-Lenovo-G50-70m:~$ rm -r playground
yrf-tan@yrftan-Lenovo-G50-70m:~$ ls
deepin-wine-for-ubuntu  examples.desktop                        Public     music
Desktop                 google-chrome-stable_current_amd64.deb  Templates
Documents               Music                                   Videos
Downloads               Pictures                                download

be accomplished!!!

summary

I learned a lot today, learned some basic related knowledge such as wildcards and links, and mastered several simple commands for operating files and directories, mkdir, cp, mv, rm and ln. More importantly, I actually started to operate again, from creating files, copying files, removing files, renaming files, creating hard links, symbolic links and deleting links, The whole process of deleting files and directories. This process can be practiced several times, and other operations can be "played" more. Different operations can be added to improve the proficiency of operating files and directories.

Keywords: Linux

Added by legohead6 on Wed, 09 Feb 2022 02:34:50 +0200