- How do I delete a directory in Linux terminal?
- How to delete a directory in Linux terminal
- How do I delete a full directory in Linux?
- Conclusion
- Delete / Remove a Directory Linux Command
- Commands to remove a directory in Linux
- rmdir command syntax to delete directory in Linux
- Delete directory Linux Command
- How to see a diagnostic message for every directory processed
- Removing directories with rmdir and wildcards
- Linux remove entire directory including all files and sub-directories command
- Are you getting permission denied error message while removing directories?
- Use find command to delete unwanted directories
- How to find and remove all empty directories
- Conclusion
- How to remove non empty Directory in Linux
- Procedure to remove non empty directory in Linux
- Examples for removing non empty directory under Linux
- How to get visual confirmation about deleting directory
- How to get confirmation prompt before every removal of a dir
- Conclusion
- Ubuntu Linux: Delete Directory Command in Terminal
- Syntax
- Ubuntu delete directory called /tmp/foo
- Recursive directory removal on Ubuntu
- Deleting directories as a superuser on ubuntu
How do I delete a directory in Linux terminal?
Let us see how to delete directories in Linux using the command line.
How to delete a directory in Linux terminal
Say you want to delete a directory named /home/vivek/data/, run:
rmdir /home/vivek/data/
rmdir -v
/data/
Verify directory deleted from the system with help of ls command:
ls /home/vivek/data/
ls
/data/
Please note that when attempting to remove a directory using the rmdir command, the directory must be empty. Otherwise, you might see an error message that read as follows on screen when execute rmdir -v /home/vivek/projects/ :
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How do I delete a full directory in Linux?
As I said earlier rmdir command remove the DIRECTORY(ies) if they are empty. But, how do you delete a full directory that has many files and sub-directories? The solutions is to pass the -rf option to the rm command. The syntax is:
rm -r dir1
rm -rf dir2 dir3 /path/to/foo/
rm -rfv /path/to/bar/dir/
rm -rfv /home/vivek/projects/
Where,
- -r – Delete directories and their contents recursively
- -f – Forceful option i.e. ignore nonexistent files and arguments, never prompt for anything
- -v – Be verbose. Show what rmdir or rm command doing with given directory
- -i – Prompt before every removal of file/dir
- -I – Confirm (display prompt) once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes. Useful when working on a large number of files on Linux
Conclusion
This page showed how to delete both empty and non-empty directories along with all files/sub-directories using rm and rmdir command in Linux terminal application.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Delete / Remove a Directory Linux Command
Commands to remove a directory in Linux
There are two command to delete a folder in Linux:
- rmdir command – Deletes the specified empty directories and folders in Linux.
- rm command – Delete the file including sub-directories. You can delete non-empty directories with rm command in Linux.
Let us see some examples and usage in details delete the directories.
rmdir command syntax to delete directory in Linux
The rmdir command remove the DIRECTORY(ies), if they are empty. The syntax is:
rmdir directory-name
rmdir [option] directory-name
Open the terminal application and run command to delete given directory. For example, delete a folder named dir1:
rmdir dir1
Delete directory Linux Command
Open a command line terminal (select Applications > Accessories > Terminal), and then type the following command to remove a directory called /tmp/docs:
rmdir /tmp/docs
If a directory is not empty you will get an error message that read as follows:
rmdir letters
Sample outputs:
You can cd to the directory to find out and list all files:
$ cd letters
$ ls
Delete those files or directories. In this next example, remove data, foo and bar if bar were empty, foo only contained bar and data only contained foo directories:
cd /home/nixcraft
rmdir -p data/foo/bar
Where,
- -p : Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component.
How to see a diagnostic message for every directory processed
Pass the -v option to the rmdir command:
$ rmdir -v dir1
Sample outputs:
Removing directories with rmdir and wildcards
We can use wildcards such as ‘*’ and ‘?’ to match and delete multiple directories. For example:
$ ls -l dir*
We have three dirs named dir1, dir2, and dir3. To delete all directories starting with ‘dir’ in the current, you would use the following command:
rmdir -v dir*
Linux remove entire directory including all files and sub-directories command
To remove all directories and subdirectories use the rm command. For example, remove *.doc files and all sub-directories and files inside letters directory, type the following command:
Warning : All files including subdirectories will be deleted permanently when executed the following commands.
$ rm -rf letters/
Sample session:
Where,
- -r : Attempt to remove the file hierarchy rooted in each file argument i.e. recursively remove subdirectories and files from the specified directory.
- -f : Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions
Are you getting permission denied error message while removing directories?
Only owners can delete their directories. However, a sysadmin can delete any directories created by anyone on the system. The syntax is:
sudo rmdir /path/to/dir/
sudo rm -rf dir2
When prompted, you need to provide root user or sudo user password.
Use find command to delete unwanted directories
Say you want to find out all directories named ‘session’ and delete them in the current directory, run:
find . -type d -iname ‘session’ -delete
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How to find and remove all empty directories
Run:
find . -type d -iname ‘session’ -empty -delete
Where,
- -type d : Only search for directories and ignore all other files.
- -iname ‘session’ : Search directory named ‘session’. You can use wildcards here too. For example, -iname ‘dir*’ .
- -empty : Only match empty directories
- -delete : Deletes all found empty directories only
To delete all ‘.DS_store’ directories stored in /var/www/html, run:
sudo find /var/www/html/ -type d -name .DS_Store -exec rm <> \;
OR
sudo find /var/www/html/ -type d -name .DS_Store -exec rm <> +
The -exec option to the find command run an external command named rm to delete all files. The “ rm <> +/ ” is a better option as it uses one rm command to delete all .DS_Store directories.
Conclusion
This page showed how to delete a directory when it is empty. Further, it showed, how to remove folders using the rm and rmdir commands. See rm help page page for more info:
- For more information read man pages: rm(1)
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to remove non empty Directory in Linux
The following commands works with CentOS, RHEL, Fedora, Alpine, Arch, Debian, Ubuntu and all other Linux distros. Let us see some examples.
Procedure to remove non empty directory in Linux
We use the rm command to delete a directory that is not empty. The syntax is:
rm -rf dir-name
rm -rf /path/to/dir/name
Be careful when you use the rm command with -r and -f options. The -r option remove directories and their contents recursively including all files. The -f option to rm command ignore nonexistent files and arguments, never prompt for anything. There is no undo option. So you have to be very careful with rm -rf command. Let us see some examples.
Examples for removing non empty directory under Linux
Trying to remove trip-pictures directory with the rmdir command in Linx:
rmdir trip-pictures
Sample outputs:
To see files inside the directory use ls command ls -l trip-pictures
ls trip-pictures
To delete all files inside trip-pictures including folder itself run the following rm command:
rm -rf trip-pictures
How to get visual confirmation about deleting directory
Pass the -v to the rm command:
rm -vrf dir1
rm -vrf dir1 dir2
Sample outputs:
How to get confirmation prompt before every removal of a dir
You need to pass the -i option to the rm command:
rm -ir foo
Sample outputs:
To get prompt once before removing more than three files, or when removing recursively; less intrusive than -i , while still giving protection against most mistakes pass the -I option:
rm -Ir bar
Sample outputs:
Want to get info on all rm and rmdir switches? Try:
rm —help
rmdir —help
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Conclusion
You learned how to remove non empty directory under Linux or Unix-like operating systems using command line options. For more information see rm command and rmdir command command man pages by typing the following man command command:
man rm
man rmdir
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Ubuntu Linux: Delete Directory Command in Terminal
[donotprint]
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | 1m |
[/donotprint]Please note directory often referred to as a folder in the Apple Mac OS X and Microsoft Windows operating systems.
Syntax
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
rmdir [option] dirName
Ubuntu delete directory called /tmp/foo
Open the terminal. Type the following command:
$ rmdir /tmp/foo
To remove foo and bar empty directories, type:
$ rmdir foo bar
Recursive directory removal on Ubuntu
Remove all files and directories including all sub-directories i.e. recursive removal:
$ rm -rf /path/to/directory
$ rm -rf /tmp/foo
Please note that you can also pass -p option to the rmdir command. Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component:
$ rmdir -p /tmp/x/y/z
Deleting directories as a superuser on ubuntu
If directory is owned by root or any other user or if you are getting “access denied/permission denied” message, try:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Thanks for this. However, it doesn’t always work. I found that Thunderbird stores a hidden file in a /home directory and I kept getting a message saying that the folder was not empty. In the end, I used “gksudo nautilus”, renamed the hidden file and then deleted it.
It really works thanks
I really doesn’t work. I get the error message saying rmdir: invalid option — ‘r’ when i put in the following:
rmdir -r /sametime-connect-8.0.2/opt/ibm/lotus/sametime/eclipse/plugins/org.apache.lecene_1.4.103.v20060601/META-INF
@ LCC:
Go where you have your directory. But dont enter into Directory.Use
$> rm -rf Directory/*
This shall remove all the files contained in “Directory”.
Then do
$> rmdir Directory
This shall remove the Directory.
Hope it works
You are a god. Simple and clear instructions that worked when apparently I was doing something wrong with the instructions given above.
nikhil Jun 25, 2010 @ 4:59
thanx 4 dis help…
thanx it really helped me
I tried everything but still no luck. See what happened :
[rockie01@mxrplnux01 home]$ sudo rmdir -rf /home/jzhou01
rmdir: invalid option — r
Try `rmdir –help’ for more information.
[rockie01@mxrplnuxt01 home]$ sudo rmdir -rf /home/jzhou01/*
rmdir: invalid option — r
Try `rmdir –help’ for more information.
I also tried to delete the user with userdel -r but it says, the user does not exist then when I check the home directory, the user name and subdirectories are still there. Someone who deleted the user account before me did not do it correctly because the user name and it’s sub-directories are still in the home directory. How do I remove this user name directory?
rmdir -r is nothing.
rm -rf will remove the files in a certain driectory.
when the directory is empty, do rmdir .
like
$>rm -rf /home/jzhou01/*
then
$> rmdir /home/jzhou01/
Hope it works.
and by the way I dont get why u want it with sudo.
Actually I did those already. I tried :
$ sudo rm -rf /home/jzhou01/* and $sudo rmdir /home/jzhou01 but it did not work.
With the -rf the reply was :
rmdir: invalid option — r
Try `rmdir –help’ for more information
Without the -rf and just $sudo rmdir /home/jzhou01, the reply was :
Directory is not empty.
I found out later that the only files left in the directory were started with a . , sorta like .profile or some other invisible files that will not show with: ls /home/namedir or ls -la /home/namedir
When I tried this syntax :
$sudo rm /home/jzhou01 it worked. I checked the /home directory and the username of jzhou01 and it’s subdirectories and files were deleted or removed.
We use sudo because our management is very strict and only three senior systems admins in Unix are the ones with root access besides the manager of that dept. Otherwise all others in the Intermediate Systems Admin category will be in the sudoer’s file and will just have to use “sudo” for any administrative command. Very strict training so that when we reach the Senior level, we are able to fine tune and troubleshoot problems easily.
using “sudo” will confirm all files of specified directory will be removed but there’s no need unless the permissions are set against your needs. Try sudo chmod 770 “file name”(or 777, shouldn’t matter really. There is a small formula to determine the permissions with chmod, look it up) Make sure you’re in the directory with the file or type in the full path i.e. sudo chmod 770 BunkGames if you’re in the directory and sudo chmod 770 /home/user/Games/BunkGames if your anywhere else. Hope this helped. Also make sure you use the “man pages”. man=manual and if you type “man” before nearly any command you will get to see nearly all of that commands capabilities and how to use them.
The chmod formula is binary, the first number indicates the user, the 2nd group the “group” and the third one the world (=everyone).
The numbers are ordered Read, Write Execute. Where read is 1, write is 2 and execute is 4. So for a read + execute rank the number would be 1+4 = 5.
I know this is late, but I thought you might be interested.
Thanks for this! it really helped me!
thx for this handy help
I am having real trouble with Linux, i need some help.. I can’t figure out how to remove a directory i made, i was trying to install openoffice 3.3, then following the steps posted in some website, something like ( sudo -vxzf filename) done that and other stuff, the outcome was a file directory, that i couldn’t remove anymore “and no openoffice at all”. I have tried ( $rmdir dirpath), when i exe. that command all that says is ( dirpath is a directory).. that’s all i checked the dirpath, it’s still there. I am new to Linux..appreciate any help.
I am running Ubuntu 10.10.
Use sudo rmdir -rf /dir/dir/*file* matthew sellers Mar 6, 2011 @ 2:18
i know this doesnt necessarily go with the topic but i am really getting frusterated.. ok so i cant seem to get the terminal to let me type in my sudo password.. i put in the coding for the compiz fusion but when it brings up the statement to type my sudo password i try and nothing is coming up no letters what so ever… it works all the way up until that point.. so i hit enter to try it again and it keeps doin the same thing but only with my pass word can some one please help me figure this out cuz it is really hindering the work i am trying to do..
This is a secured login. Of course it will not show what you type in. It is invisible. When it prompts you for your password, you type your password in but you do not see ******* as you type in your password in other terminals. This is a security measure. Just type your password (yes, it is there even if you cannot see it. It’s invisible) then hit enter. This will log you into the system. Then you can just do your thing. Unless you are in the sudoers file. In order to be able to access a particular machine, first of all, you should have an account set up in the box in order for you to log in. Second, if you do not have straight root access (usually done for first level support as having root access might be too dangerous for inexperienced admins), you will be added to a sudoers file. If you are on the sudoers file then depending on what type of group access you have, your sudo config file only limits you to what you are allowed to do. Ask you Systems Admin (Unix / Limux) to see what the real issue is. Usually, they are just trying to give someone a hard time or you might need a manager’s approval before you are given access by the Unix Systems Admin.
Источник