Showing posts with label rm Command. Show all posts
Showing posts with label rm Command. Show all posts

Tuesday, 2 June 2020

The Linux 'rm' command (remove files and directories)

RM Command, LPI Tutorial and Material, LPI Guides, LPI Certification, Linux Exam Prep

Linux FAQ: How do I delete files (remove files) on a Unix or Linux system?


The Linux rm command is used to remove files and directories. (As its name implies, this is a dangerous command, so be careful.)

Let's take a look at some rm command examples, starting from easy examples to more complicated examples.

How to delete files with rm


In its most basic use, the rm command can be used to remove one file, like this:

rm oldfile.txt

You can also use the rm command to delete multiple Linux files at one time, like this:

rm file1 file2 file3

If you prefer to be careful when deleting files, use the -i option with the rm command. The -i stands for "inquire", so when you use this option the rm command prompts you with a yes/no prompt before actually deleting your files:

rm -i files file2 file3


How to delete directories with rm


To delete Linux directories with the rm command, you have to specify the -r option, like this:

rm -r OldDirectory

The -r option means "recursive", and therefore this command recursively deletes all files and directories in the directory named OldDirectory.

As a warning, this command is obviously very dangerous, so be careful. Some people always add the inquire option when deleting directories, like this:

rm -ir OldDirectory

You can also delete multiple directories at one time, like this:

rm -r Directory1 Directory2 Directory3

How to use wildcards with rm


Unix and Linux systems have always supported wildcard characters, so in this case you can delete files and directories even faster. For instance, to delete all HTML files in the current directory, use this command:

rm *.html

Note that unlike DOS, you don't actually need the "." before the "html" in that command, so you can shorten the command like this:

rm *html

Unix and Linux wildcard characters and commands don't care at all about "." characters and filename extensions, so the "." is not needed.

You can also use wildcard characters in the middle of a filename or at the end of the filename. Here's an example where I'm deleting all files in the current directory that begin with the string "index":

rm index*

This command deletes files named index.html, index.php, and in general, any filename that begins with the character string "index".

You can also use wildcard characters like this to delete multiple files or directories:

rm Chapter[123].txt

That command deletes the files Chapter1.txt, Chapter2.txt, and Chapter3.txt, all in one command.

More Linux rm commands


There are probably many more Linux delete commands you can issue with the rm command. For instance, you can delete files and directories that aren't in the current directory. Here's an example where I delete a file named foo.txt that's in the /tmp directory:

rm /tmp/foo.txt

Wednesday, 12 December 2018

rm command in Linux with examples

rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic links and so on from the file system like UNIX. To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file with two different names). By default, it does not remove directories.

rm Command, Linux Certification, Linux Tutorial and Material

This command normally works silently and you should be very careful while running rm command because once you delete the files then you are not able to recover the contents of files and directories.

Syntax:


rm [OPTION]... FILE...

Let us consider 5 files having name a.txt, b.txt and so on till e.txt.

$ ls
a.txt  b.txt  c.txt  d.txt  e.txt

Removing one file at a time
$ rm a.txt

$ ls
b.txt  c.txt  d.txt  e.txt

Removing more than one file at a time
$ rm b.txt c.txt

$ ls
d.txt  e.txt

Note: No output is produced by rm, since it typically only generates messages in the case of an error.

Options:


1. -i (Interactive Deletion): Like in cp, the -i option makes the command ask the user for confirmation before removing each file, you have to press y for confirm deletion, any other key leaves the file un-deleted.

$ rm -i d.txt
rm: remove regular empty file 'd.txt'? y

$ ls
e.txt

2. -f (Force Deletion): rm prompts for confirmation removal if a file is write protected. The -f option overrides this minor protection and removes the file forcefully.

$ ls -l
total 0
-r--r--r--+ 1 User User 0 Jan  2 22:56 e.txt

$ rm e.txt
rm: remove write-protected regular empty file 'e.txt'? n

$ ls
e.txt

$ rm -f e.txt

$ ls

Note: -f option of rm command will not work for write-protect directories.

3. -r (Recursive Deletion): With -r(or -R) option rm command performs a tree-walk and will delete all the files and sub-directories recursively of the parent directory. At each stage it deletes everything it finds. Normally, rm wouldn’t delete the directories but when used with this option, it will delete.

Below is the tree of directories and files:

$ ls
A

$ cd A

$ ls
B  C

$ ls B
a.txt  b.txt

$ ls C
c.txt  d.txt

Now, deletion from A directory(as parent directory) will be done as:

$ rm *
rm: cannot remove 'B': Is a directory
rm: cannot remove 'C': Is a directory

$ rm -r *

$ ls


Every directory and file inside A directory is deleted.

4. –version: This option is used to display the version of rm which is currently running on your system.

$ rm --version
rm (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


Applications of wc Command


Delete file whose name starting with a hyphen symbol (-): To remove a file whose name begins with a dash (“-“), you can specify a double dash (“–“) separately before the file name. This extra dash is necessary so that rm does not misinterpret the file name as an option. Let say their is a file name -file.txt, to delete this file write command as:

$ ls
-file.txt

$ rm -file.txt
rm: unknown option -- l
Try 'rm ./-file.txt' to remove the file '-file.txt'.
Try 'rm --help' for more information.

$ rm -- -file.txt

$ ls

Monday, 1 October 2018

10 ‘rm’ command examples for beginners

rm stands for ‘remove‘ as the name suggests rm command is used to delete or remove files and directory in UNIX like operating system. If you are new to Linux then you should be very careful while running rm command because once you delete the files then you can not recover the contents of files and directory. Though there are some tools and commands through which deleted files can be recovered but for that you need expert skills.

In this post i will demonstrate 10 Linux rm command examples. Below is the basic syntax of rm command.

# rm <options> {files}

options used in rm command.

rm Command, LPI Tutorial and Material, LPI Guides, LPI Learning, LPI Certification

Example: 1 Remove or delete a file.


Let’s delete a file with name “linuxstufff.log”

[lpicentral@cloud ~]$ rm linuxstufff.log
[lpicentral@cloud ~]$

Delete multiple files at once.

Let’s assume that i want to delete four text files at once. Use the below syntax

# rm {file1} {file2}] {file3} {file4}

[lpicentral@cloud ~]$ rm file1.txt file2.txt file3.txt file4.txt
[lpicentral@cloud ~]$

Example: 2 Delete the files interactively.


‘-i‘ option in rm command will prompt before deleting a file, example is shown below.

[lpicentral@cloud ~]$ rm -i linuxstufff.log
rm: remove regular file ‘linuxstufff.log’? y
[lpicentral@cloud ~]$

Example: 3 Delete a empty directory


use ‘-d‘ option in rm command to delete a empty directory.

[lpicentral@cloud ~]$ ls -R appdata/
appdata/:
[lpicentral@cloud ~]$ rm -d appdata/
[lpicentral@cloud ~]$

we can also use ‘rmdir‘ command to delete empty folder or directory.

[lpicentral@cloud ~]$ ls -R appdata/
appdata/:
[lpicentral@cloud ~]$ rmdir appdata
[lpicentral@cloud ~]$

Example: 4 Deleting a directory recursively using ‘-r’ option


‘-r‘ option in rm command will delete all the files and sub-directories recursively of the parent directory.

[lpicentral@cloud ~]$ ls -lR dbstore/
dbstore/:
total 0
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 26 23:59 file1.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 26 23:59 file2.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 26 23:59 file3.log
drwxrwxr-x. 2 lpicentral lpicentral 6 Mar 26 23:59 service

dbstore/service:
total 0
[lpicentral@cloud ~]$ rm -r dbstore/
[lpicentral@cloud ~]$

Example: 5 Delete the files and sub-directories interactively.


Use ‘-ri‘ option in rm command to delete file and sub-directories interactively, Let’s assume we want to all files and directories of ‘dbstore’ directory interactively.

[lpicentral@cloud ~]$ ls -lR dbstore/
dbstore/:
total 0
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 00:02 file1.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 00:02 file2.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 00:02 file3.log
drwxrwxr-x. 2 lpicentral lpicentral 6 Mar 27 00:02 service

dbstore/service:
total 0
[lpicentral@cloud ~]$ rm -ri dbstore/
rm: descend into directory ‘dbstore/’? y
rm: remove regular empty file ‘dbstore/file1.log’? y
rm: remove regular empty file ‘dbstore/file2.log’? y
rm: remove regular empty file ‘dbstore/file3.log’? y
rm: remove directory ‘dbstore/service’? y
rm: remove directory ‘dbstore/’? y
[lpicentral@cloud ~]$

Example: 6 Deleting files forcefully using ‘-f’ option


‘-f‘ option in rm command will remove or delete the files forcefully regardless of its permissions and will also ignore non-existing files.

Let’s delete a write-protected file ‘tech.txt’

[lpicentral@cloud ~]$ ls -l tech.txt
-r--r--r--. 1 lpicentral lpicentral 0 Mar 27 00:23 tech.txt
[lpicentral@cloud ~]$
[lpicentral@cloud ~]$ rm tech.txt
rm: remove write-protected regular empty file ‘tech.txt’?

As we can see above that when we try to delete a write-protected file using rm command without ‘-f’ option , it gives us a prompt to delete write-protected file.

Now try to delete a file using ‘-f’ option.

[lpicentral@cloud ~]$ rm -f tech.txt
[lpicentral@cloud ~]$

Also try to delete a non-existing file.

[lpicentral@cloud ~]$ rm -f nonexist.txt
[lpicentral@cloud ~]$

Note : ‘-f’ option of rm command will not work for write-protect directories,

Let’s take an example , directory ‘/home/lpicentral/location/ ‘ is write protected and file (‘db_stuff‘) inside this directory is non-protected.

[lpicentral@cloud ~]$ ls -ld /home/lpicentral/location/
drwxrwxr-x. 2 root root 29 Mar 27 00:43 /home/lpicentral/location/

[lpicentral@cloud ~]$ ls -l /home/lpicentral/location/db_stuff
-rw-rw-r--. 1 lpicentral lpicentral 17 Mar 27 00:43 /home/lpicentral/location/db_stuff

[lpicentral@cloud ~]$ rm -f /home/lpicentral/location/db_stuff
rm: cannot remove ‘/home/lpicentral/location/db_stuff’: Permission denied
[lpicentral@cloud ~]$

Example 7: Prompt once before deleting more than three files or recursive delete.


‘-I‘ option in rm command will prompt once before deleting more than three files or recursive delete.

Suppose i want to delete all log files which starts with the name ‘app’ under the directory ‘linux_store’.

[lpicentral@cloud ~]$ ls -l linux_store/
total 0
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:07 app1.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:07 app2.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:07 app3.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:07 app4.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:07 app5.log
[lpicentral@cloud ~]$ rm -I linux_store/app*
rm: remove 5 arguments? y
[lpicentral@cloud ~]$

Example: 8 Regular expression in rm command


We can use regular expression in the rm command, some of the examples are shown below :

Let’s delete 5 log files starting from log1 to log5 under the directory ‘linux_store‘.

[lpicentral@cloud linux_store]$ pwd
/home/lpicentral/linux_store
[lpicentral@cloud linux_store]$ ll
total 0
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log1.txt
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log2.txt
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log3.txt
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log4.txt
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log5.txt
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log6.txt
........................................
[lpicentral@cloud linux_store]$
[lpicentral@cloud linux_store]$ rm -f log{1..5}.txt
[lpicentral@cloud linux_store]$

Delete all the files of the current directory that ends with ‘.txt’

[lpicentral@cloud linux_store]$ rm -f *.txt
[lpicentral@cloud linux_store]$

Delete all files of present working directory which has 3 characters in extension.

[lpicentral@cloud linux_store]$ rm -f *.???
[lpicentral@cloud linux_store]$

Example: 9 Delete large number files using rm command.


If your are trying to delete large number of files using rm command then you will get an error message ‘Argument list too long’

In the below example i am trying to delete all the files (around ‘300001’) of the directory ‘/home/lpicentral/linux_store‘ at once.

[lpicentral@cloud linux_store]$ ls -l | wc -l
300001
[lpicentral@cloud linux_store]$ rm *.log
-bash: /bin/rm: Argument list too long
[lpicentral@cloud linux_store]$

To resolve this issue , use the below find command.

[lpicentral@cloud ~]$ find ~/linux_store/ -type f -exec rm {} \;
[lpicentral@cloud ~]$

Example: 10 Delete a file which starts with hyphen symbol (-)


Let’s assume that we have a file with name ‘-store‘ in our current working directory and we want to delete this file.

[lpicentral@cloud linux_store]$ ll
total 0
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 02:05 -store
[lpicentral@cloud linux_store]$ rm -store
rm: invalid option -- 's'
Try 'rm --help' for more information.
[lpicentral@cloud linux_store]$

Use below either of the command to delete such files.

[lpicentral@cloud linux_store]$ rm -- \ -store
[lpicentral@cloud linux_store]$

OR

[lpicentral@cloud linux_store]$ rm ./\ -store
[lpicentral@cloud linux_store]$