-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHow to Delete Files and Directories in Linux
107 lines (47 loc) · 3.32 KB
/
How to Delete Files and Directories in Linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
1. Remove file by using “unlink”:
Not so well-liked. We may use the unlink command to permanently delete a single file.
$ unlink {file-name}
2. Delete a single file:
The rm command, which facilitates deleting one or more files simultaneously, is a more widely used command for removing files.
$ rm {file-name}
If the file is write-protected, rm will ask you to validate its deletion; otherwise, it will delete it without prompting. Using the “-i” flag to force rm to prompt for confirmation before removing a file:
$ rm -i {file-name}
The rm command deletes files without showing any messages. Using the rm command with the -v flag to see what the rm command is currently doing.
$ rm -v {file-name}
Using the -f flag to remove write-protected files without asking for clarification.
$ rm -f {file-name}
3. Multiple files can be deleted:
Bypassing multiple filenames as arguments to rm, you can delete multiple files.
$ rm {file-name-1} {file-name-2} {file-name-3} ... {file-name-N}
Regular expressions are also supported by rm. If you want to delete all files with the name file-name-*, type:
$ rm file-name*.ext
Regular expressions may also be used to define different directories. We can use something like to delete three files that fit file-name-1, file-name-2, and file-name-3.
$ rm file-name-[123]
4. Delete the archive:
The rm command with the -d flag can be used to remove an empty directory.
$ rm -d {dir-name}
Supported options for file deletion can also be combined with deleting the directory with the -d flag.
$ rm -idv {dir-name}
Using the -r flag to delete a non-empty directory.
$ rm -r {dir-name}
If you do not want a prompt before deleting the directory and its contents, use the -rf flag. This will remove everything from inside the directory, including the directory itself, without any confirmation. Use caution especially when using as a root.
$ rm -rf {dir-name}
5. Locate and delete files:
We can use the locate command with various choices for more complicated specifications. To delete all files in a path specified by {dir-to-search} that follow a pattern {pattern}.
$ find {dir-to-search} -type f -name {pattern} -exec rm -f {} \;
Example:
$ find luv -type f -name "*.txt" -exec rm -f {} \;
We may slightly change the above command to delete everything that fits the sequence {pattern}, including directories within {dir-to-search}:
$ find {dir-to-search} -name {pattern} -exec rm -rf {} \;
Internally, modern implementations of the find command support the delete feature. The -delete flag is used to override the rm instruction, while the –depth flag tells find to process the contents of the directory before the directory itself:
$ find {dir-to-search} -type f -name {file-name-pattern} -depth -delete
6. Empty files should be found and deleted:
You may use the following command to remove all empty directories within a given path dir-to-search:
$ find {dir-to-search} -type d -empty -delete
Instead, use the following command to remove all empty files within a given path dir-to-search:
$ find {dir-to-search} -type f -empty -delete
7. Permissions are used to locate and delete files:
We can now remove files based on special permissions, such as:
$ find {dir-to-search} -name {pattern} -perm {NNN} -delete
Consider the following scenario:
$ find /var/tmp -name "temp*" -perm 755 -delete