Remove all files and directory linux

Linux Delete All Files In Directory Using Command Line

Linux Delete All Files In Directory

The procedure to remove all files from a directory:

  1. Open the terminal application
  2. To delete everything in a directory run: rm /path/to/dir/*
  3. To remove all sub-directories and files: rm -r /path/to/dir/*

Let us see some examples of rm command to delete all files in a directory when using Linux operating systems.

How to remove all the files in a directory?

Suppose you have a directory called /home/vivek/data/. To list files type the ls command:
$ ls

Understanding rm command option that deleted all files in a directory

  • -r : Remove directories and their contents recursively.
  • -f : Force option. In other words, ignore nonexistent files and arguments, never prompt. Dangerous option. Be careful.
  • -v : Verbose option. Show what rm is doing on screen.

Deleting hidden vs non-hidden files

In Linux, any file or directory that starts with a dot character called a dot file. It is to be treated as hidden file. To see hidden files pass the -a to the ls command:
ls
ls -a
ls -la
To remove all files except hidden files in a directory use:
rm /path/to/dir/*
rm -rf /path/to/dir/*
rm *
In this example, delete all files including hidden files, run:
rm -rf /path/to/dir1/<*,.*>
rm -rfv /path/to/dir1/

  • 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

Bash remove all files from a directory including hidden files using the dotglob option

If the dotglob option set, bash includes filenames beginning with a ‘.’ in the results of pathname expansion. In other words, turn on this option to delete hidden files:

See GNU/bash man page for the shopt command online here:
man bash
help shopt

Linux Remove All Files In Directory

As I said earlier one can use the unlink command too. The syntax is:
unlink filename
For example, delete file named foo.txt in the current working directory, enter:
unlink foo.txt
It can only delete a single file at a time. You can not pass multiple files or use wildcards such as *. Therefore, I strongly recommend you use the rm command as discussed above.

Conclusion

In this quick tutorial, you learned how to remove or delete all the files in a directory using the rm command. Linux offers a few more options to find and delete files. Please see the following tutorials:

Читайте также:  Pci ven 10ec dev 8129 cc 0200 windows 10

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Linux / UNIX: How To Empty Directory

How To Empty Directory In Linux and Unix

  1. rm command – Delete one or more files or directories.
  2. find command – Find and delete all files from a specific directory.

Linux Empty Directory Using the rm Command

First, consider the following directory structure displayed using the tree command

To delete all files from /tmp/foo/ directory (i.e. empty /tmp/foo/ directory), enter:
$ cd /tmp/foo/
$ rm *
OR
$ rm /tmp/foo/*

Delete All Files Using the Find Command

Consider the following directory structure:

To delete all files from /tmp/bar/ directory (including all files from sub-directories such as /tmp/bar/dir1), enter:
$ cd /tmp/bar/
$ find . -type f -delete
OR
$ find /tmp/bar/ -type f -delete
The above find command will delete all files from /tmp/bar/ directory. It will not delete any sub-directories. To remove both files and directories, try:
find /path/to/target/dir/ -delete
The find commands options are as follows:

  • 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

  • -type f : Delete on files only.
  • -type d : Remove folders only.
  • -delete : Delete all files from given directory name.

How to remove a full directory and all files in Linux

To remove a directory that contains other files or sub-directories, use the following rm command command. In the example, I am going to empty directory named “docs” using the rm -rf command as follows:
rm -rf /tmp/docs/*
Get verbose outputs:
rm -rfv /tmp/docs/*

The rm command options are as follows:

  • -r : Delete directories and their contents recursively on Linux or Unix-like systems.
  • -f : Forceful removal. In other words, ignore nonexistent files and delete whatever found.
  • -v : Verbose outputs. For example, explain what is being done on screen.

Conclusion

You learned how to use the rm and find command to delete all files and sub-directories on Linux/macOS/*BSD and Unix-like systems. In other words, this is useful to empty folders on Linux. For more information see rm command help page here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Remove All Files from a Directory in Linux

In this tutorial, we are going to learn how to use rm command to remove all files safely from a directory. This document helps you delete non-hidden files, files with specific extensions, hidden files inside a directory.

01. To delete all non-hidden files from a directory, type:

02. To remove all the file with the extension .txt from a directory, type:

03. To delete all non-hidden files and sub-directories along with all of their contents from a directory, run:

04. To delete all hidden files and directories from a folder, type:

05. To delete all the files from inside a folder but not removing its sub-directories:

06. To remove a folder whose name has space, make sure to always use quotes like:

You can also use backslack to remove spaces by escaping the space.

To remove the directory named ‘Good Morning’, type:

07. You can see what is being done when deleting all files in directory pass the -v option to the rm command:

08. To remove all the file from a directory having extension .sh you can use find command too,

Note: In place of «*.sh» just give «*» to delete all the files.

Understanding rm command option

rm : Remove (unlink) the FILE(s).
-f : ignore nonexistent files and arguments, never prompt
-r : remove directories and their contents recursively
-v: see what is happening

Читайте также:  Installation media creation tool для windows 10

Conclusion

You need to be careful while removing the file on the Linux system. Using the command ‘rm’ will not store files in the trash. On the other hand, be careful while using wildcard like ‘*’.

Источник

Delete All Files And Folders In Linux

H ow do I delete all files and folders stored in /home/jerry/movies directories under Linux operating systems?

You need to the rm command.

Open a terminal or shell (bash) prompt and type the following command to delete everything in /home/jerry/movies

OR you can use the following single command:

  • 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

Please note that all folders and files including movies will be deleted. Once deleted you will not able to get back your data. So be careful when typing rm -rf command.

🐧 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.

/.local/share/Trash/files# rm -rf *

Thanks for the tip about deleting all files from directory. This site always helps me for Linux tips. Thanks again !!

If i want to delete a directory containing files for which i dont have the access

can i delete those files too

If yes can you share me the command

rm -rfv nFolders/

Hi,
I am new to linux environment and using Centos 6.2 Version OS and found that some of the folder getting deleted automatically and the deleted files and folder are from recent access. How it is happening ? Do i need to change any basic settings after OS installed? Please support me to resolve this issue.

how do we delete files in a hidden directory on startup

What I am trying to accomplish here is in my system I have android sdk installed but it gives device unauthorized …. I found a solution which asks me kill the adb server using adb kill-server to delete all files in /home/user/.android which i do by cd /home/user/.android/ and rm -rf *

I wrote this in sequence up in /home/user/.profile file but except for adb kill-server nothing worked

I also tried rm -rf /home/user/.android/* too in the . profile file but again no result. I thought sudo might be missing but using it too no result.

Источник

How to Remove Files and Directories in Linux Command Line [Beginner’s Tutorial]

How to delete a file in Linux? How to delete a directory in Linux? Let’s see how to do both of these tasks with one magical command called rm.

How to delete files in Linux

Let me show you various cases of removing files.

1. Delete a single file

If you want to remove a single file, simply use the rm command with the file name. You may need to add the path if the file is not in your current directory.

If the file is write protected i.e. you don’t have write permission to the file, you’ll be asked to confirm the deletion of the write-protected file.

You can type yes or y and press enter key to confirm the deletion. Read this article to know more about Linux file permissions.

2. Force delete a file

If you want to remove files without any prompts (like the one you saw above), you can use the force removal option -f.

3. Remove multiple files

To remove multiple files at once, you can provide all the filenames.

You can also use wildcard (*) and regex instead of providing all the files individually to the rm command. For example, if you want to remove all the files ending in .hpp in the current directory, you can use rm command in the following way:

4. Remove files interactively

Of course, removing all the matching files at once could be a risky business. This is why rm command has the interactive mode. You can use the interactive mode with the option -i.

It will ask for confirmation for each of the file. You can enter y to delete the file and n for skipping the deletion.

You just learned to delete files. Let’s see how to remove directory in Linux.

How to remove directories in Linux

There is a command called rmdir which is short for remove directory. However, this rmdir command can only be used for deleting empty directories.

If you try to delete a non-empty directory with rmdir, you’ll see an error message:

There is no rmdir force. You cannot force rmdir to delete non-empty directory.

This is why I am going to use the same rm command for deleting folders as well. Remembering rm command is a lot more useful than rmdir which in my opinion is not worth the trouble.

1. Remove an empty directory

To remove an empty directory, you can use the -d option. This is equivalent to the rmdir command and helps you ensure that the directory is empty before deleting it.

2. Remove directory with content

To remove directory with contents, you can use the recursive option with rm command.

This will delete all the contents of the directory including its sub-directories. If there are write-protected files and directories, you’ll be asked to confirm the deletion.

3. Force remove a directory and its content

If you want to avoid the confirmation prompt, you can force delete.

4. Remove multiple directories

You can also delete multiple directories at once with rm command.

Summary

Here’s a summary of the rm command and its usage for a quick reference.

Purpose Command
Delete a single file rm filename
Delete multiple files rm file1 file2 file3
Force remove files rm -f file1 file2 file3
Remove files interactively rm -i *.txt
Remove an empty directory rm -d dir
Remove a directory with its contents rm -r dir
Remove multiple directories rm -r dir1 dir 2 dir3

I hope you like this tutorial and learned to delete files and remove directories in Linux command line. If you have any questions or suggestions, please leave a comment below.

Источник

Читайте также:  Windows sp3 iso 2013
Оцените статью