In this guide, we want to teach you to Delete Files and Directories in the Linux Command Line Terminal with rm and rmdir commands. Also, you will learn to remove hidden files and all files and folders in the current directory. You can easily use the Linux commands to make your delete process. Follow this guide to the end to see how you can do this.
Easy Guide To Delete Files and Directories in Linux Command Line Terminal
Before you start, you must have access to your Linux server as a root or non-root user with sudo privileges. For this purpose, you can search for the Linux server initial guides on the Orcacore website.
Note: You just need to be sure which files or directories you want to remove because it is not easy to recover the files or directories.
Now follow the steps below to complete this guide.
Step 1 – Remove Files from the Command Line Terminal in Linux
At this point, we want to show you how to remove files in Linux. One of the best Linux commands that can be used for removing files is the rm command. Let’s see how you can use it.
Delete a Single File with the rm Command
To delete a single file, you can simply use the following syntax command:
If your file is protected, the prompt will ask you for the confirmation. If you are sure to delete the file press Y.
Delete Multiple Files with the rm Command
At this point, you can use the following syntax to delete multiple files:
rm filename1 filename2 ...
Delete Specific File Types with the rm Command
If you plan to remove files with a specific expansion, you can use the * option with your desired file type. For example, in the current directory, we want to remove all the jpg file types. To do this, we should run the command below:
This will remove all the jpg file types in your current directory.
Delete a File in a Specific Directory
If you want to delete a file in another directory, you can use the command below:
You should specify the directory to the command with your filename.
Delete All Files in a Directory
To delete all files in a directory, you can use the following syntax:
This will remove all files in a specific directory.
The rm command options to delete a file
Also, you can use more options for deleting a file in Linux with the rm command.
To confirm each file before deleting, you can use the -i option:
rm -i filename
# rm -i file2 rm: remove regular empty file 'file2'? y
To force delete a file without asking for confirmation, you can use the -f option:
rm -f filename
Also, you can combine the options to delete a file or files. For example, to force delete text files in verbose mode, you can use the -fv option:
rm -fv *.txt
Delete Hidden Files in Linux Terminal
Any filename that begins with a dot (.) is a hidden file. To display the hidden files, you can use the command below:
Exmaple Output . .bash_history .cache .local snap .viminfo .. .bashrc .lesshst .profile .ssh
Note: Remember that you can not delete the two individual directories:
- . – The current directory is indicated by a single dot.
- .. – The parent directory is indicated by two successive dots.
To delete a hidden file in Linux, you can use the following rm command:
Also, you can use the options we said above to the command.
Step 2 – Remove Directories from the Command Line Terminal in Linux
At this point, you can use the rm and rmdir commands to delete the directories in Linux.
You can use the rmdir command to delete empty directories. But with the rm command, you can remove directories and their contents recursively.
Remove an Empty Directory in the Linux Terminal
To remove an empty directory, you can simply use the rmdir command followed by the dir path:
If you want to use the rm command, you need to use the -d option:
rm -d dirname
Delete a Directory with its Files in Linux
You can use the rm command with the -r option to remove the directory with all the files:
rm -r dirname
Also, you can use the -f option to force delete it without asking for confirmation:
rm -rf dirname
Remove Multiple Directories in Linux Terminal
You can easily remove multiple directories with the rm -r command:
rm -r dirname1 dirname2 dirname3 ...
Remove Hidden Directories in Linux Terminal
Also, you can remove hidden directories same as the hidden files. The hidden directories start with a dot. To delete them, you can use:
rm -r .direname
That’s it, you are done.
To get more options, you can run the following commands:
# rm --help # man rm
At this point, you have learned to Delete Files and Directories in the Linux Command Line Terminal with rm and rmdir commands. The guide includes the following subjects:
- Delete a Single File, Multiple Files, Specific File Types, All files in a Directory, Delete a File in a Specific Directory, and Delete Hidden Files.
- Remove an Empty Directory, a Directory with its Files, Multiple Directories, and remove hidden directories.
Hope you enjoy it. You may be interested in these articles: