Delete Files And Directories

The rm (remove) command is used to delete files and folders in Linux.

Create Files

touch sales.csv
touch config.json
touch README.md
touch documentation.html
touch sales.html

Create Subdirectory

mkdir sales_reports

Create File In Subdirectory

touch sales_reports/report.html

View Current Directory

ls -l
total 4
-rw-rw-r-- 1 chris chris    0 Jul 24 17:01 config.json
-rw-rw-r-- 1 chris chris    0 Jul 24 17:01 documentation.html
-rw-rw-r-- 1 chris chris    0 Jul 24 17:01 README.md
-rw-rw-r-- 1 chris chris    0 Jul 24 17:01 sales.csv
-rw-rw-r-- 1 chris chris    0 Jul 24 17:01 sales.html
drwxrwxr-x 2 chris chris 4096 Jul 24 17:02 sales_reports

Delete File

rm config.json

Delete All HTML Files

rm *.html

Delete Subdirectory (And Contained Files)

The -r option indicates that files/directories are deleted recursively, meaning not only is the directory deleted, but everything inside that directory.

rm sales_reports -r

View Current Directory

ls -l
total 0
-rw-rw-r-- 1 chris chris 0 Jul 24 17:01 README.md
-rw-rw-r-- 1 chris chris 0 Jul 24 17:01 sales.csv