Common Linux Commands: ln, cp, rm, find

1. ln ln is short for link.Links can be understood as “shortcuts” or “aliases” for files/directories. There are soft links and hard links, with soft links being more commonly used. ln [options] <source file> <target link> Soft links, also known as symbolic links, are more like “shortcuts” in Windows systems.

-s –symbolic Create a soft link. If this option is not added, a hard link is created by default.

-f –force Force creation. If the target link file already exists, it will be overwritten.

Common usage:

ln -sf <source file> <target link>

You can soft link files or directories.

Deleting a soft link does not delete the source file/directory.

When creating links, it is best to use the full path for the source file/directory, for example, use /home/zhiyuan/test/test.txt instead of just test.txt

zhiyuan@mypi:~/test $ pwd/home/zhiyuan/testzhiyuan@mypi:~/test $ ls -altotal 16drwxr-xr-x  3 zhiyuan zhiyuan 4096 Oct 12 17:39 .drwx------ 15 zhiyuan zhiyuan 4096 Oct 12 17:51 ..drwxr-xr-x  2 zhiyuan zhiyuan 4096 Oct 12 17:38 mynewdirlrwxrwxrwx  1 zhiyuan zhiyuan   10 Oct 12 17:38 mynewdir_aa -> ./mynewdir-rw-r--r--  1 zhiyuan zhiyuan    0 Oct  8 19:32 mynewfile-rw-r--r--  1 zhiyuan zhiyuan   82 Oct 11 21:41 test.txtzhiyuan@mypi:~/test $ ln -sf /home/zhiyuan/test/test.txt /home/zhiyuan/test_aa.txtzhiyuan@mypi:~/test $ ls -al /home/zhiyuan/test_aa.txt lrwxrwxrwx 1 zhiyuan zhiyuan 27 Oct 12 17:52 /home/zhiyuan/test_aa.txt -> /home/zhiyuan/test/test.txtzhiyuan@mypi:~/test $ zhiyuan@mypi:~/test $ ln -sf /home/zhiyuan/test/mynewdir /home/zhiyuan/test/mynewdir_aa

2. cp

cp is short for copy, used to copy files or directories.

cp [options] <source file or directory> <target file or directory>

-a –archive Archive mode, equivalent to a combination of -dpR, preserving all attributes and copying recursively.

-r, -R –recursive Recursively copy directories and their contents.

Common usage:

cp -a -r <source file or directory> <target file or directory>

zhiyuan@mypi:~/test $ cp -a -r test.txt test2.txtzhiyuan@mypi:~/test $ cp -a -r mynewdir mynewdir2zhiyuan@mypi:~/test $ ls -altotal 24drwxr-xr-x  4 zhiyuan zhiyuan 4096 Oct 12 18:05 .drwx------ 15 zhiyuan zhiyuan 4096 Oct 12 17:52 ..drwxr-xr-x  2 zhiyuan zhiyuan 4096 Oct 12 17:52 mynewdirdrwxr-xr-x  2 zhiyuan zhiyuan 4096 Oct 12 17:52 mynewdir2lrwxrwxrwx  1 zhiyuan zhiyuan   10 Oct 12 17:38 mynewdir_aa -> ./mynewdir-rw-r--r--  1 zhiyuan zhiyuan    0 Oct  8 19:32 mynewfile-rw-r--r--  1 zhiyuan zhiyuan   82 Oct 11 21:41 test2.txt-rw-r--r--  1 zhiyuan zhiyuan   82 Oct 11 21:41 test.txt

3. rm

rm is short for remove, used to delete files or directories, and can also be used to delete links.

rm [options] <file or directory>

-r, -R –recursive Recursively delete, used to delete directories and all their contents

-f –force Force deletion, ignore nonexistent files, do not prompt

Common usage:

rm -fr <file or directory>

zhiyuan@mypi:~/test $ ls -altotal 24drwxr-xr-x  4 zhiyuan zhiyuan 4096 Oct 12 18:05 .drwx------ 15 zhiyuan zhiyuan 4096 Oct 12 17:52 ..drwxr-xr-x  2 zhiyuan zhiyuan 4096 Oct 12 17:52 mynewdirdrwxr-xr-x  2 zhiyuan zhiyuan 4096 Oct 12 17:52 mynewdir2lrwxrwxrwx  1 zhiyuan zhiyuan   10 Oct 12 17:38 mynewdir_aa -> ./mynewdir-rw-r--r--  1 zhiyuan zhiyuan    0 Oct  8 19:32 mynewfile-rw-r--r--  1 zhiyuan zhiyuan   82 Oct 11 21:41 test2.txt-rw-r--r--  1 zhiyuan zhiyuan   82 Oct 11 21:41 test.txtzhiyuan@mypi:~/test $ rm -fr mynewdir2zhiyuan@mypi:~/test $ rm -fr mynewdir_aazhiyuan@mypi:~/test $ rm -fr test2.txtzhiyuan@mypi:~/test $ ls -altotal 16drwxr-xr-x  3 zhiyuan zhiyuan 4096 Oct 12 18:13 .drwx------ 15 zhiyuan zhiyuan 4096 Oct 12 17:52 ..drwxr-xr-x  2 zhiyuan zhiyuan 4096 Oct 12 17:52 mynewdir-rw-r--r--  1 zhiyuan zhiyuan    0 Oct  8 19:32 mynewfile-rw-r--r--  1 zhiyuan zhiyuan   82 Oct 11 21:41 test.txt

Never execute the following commands:

rm -rf / # Deletes the entire system

rm -rf /* # Deletes everything in the root directory

rm -rf . # Deletes everything in the current directory

rm -rf ~ # Deletes everything in the home directory

rm -rf /home/username/ # Deletes all files for a specific user

4. find

The find command is used to search for files or directories.

find [path…] [expression]

-name pattern Match by file name

-iname pattern Case-insensitive file name matching

Common usage:

find . -iname “*abc*”

find /home/zhiyuan -iname “*abc*”

. indicates searching in the current directory.

* is a wildcard

“*abc*” indicates any file name or directory containing the characters abc.

“*abc” indicates ending with abc.

“abc*” indicates starting with abc.

zhiyuan@mypi:~/test $ pwd/home/zhiyuan/testzhiyuan@mypi:~/test $ tree.├── mynewdir│   └── test2.txt├── mynewfile├── my.txt├── Test2.txt└── test.txt2 directories, 5 fileszhiyuan@mypi:~/test $ find . -name "test*"./test.txt./mynewdir/test2.txtzhiyuan@mypi:~/test $ find . -iname "test*"./Test2.txt./test.txt./mynewdir/test2.txtzhiyuan@mypi:~/test $ find . -iname "my*"./mynewfile./my.txt./mynewdir

Leave a Comment