Removing all files and directory in linux

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

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 ‘*’.

Источник

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
Читайте также:  Приложение киоск для windows

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:

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

Источник

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.

Источник

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 Files and Directories in Linux?

Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.

One of the basic file system administration tasks involves creating, modifying, and deleting different types of files and directories. Knowing some basic tools and concepts for file deletion comes in handy and can save you time.

Linux offers several tools that can help us accomplish file removal tasks. Often we need to remove not just a single, but a bunch of files and directories, based on some criteria. It is helpful to know a few common commands and their combinations to get our task done easily.

  • Use the below commands with caution especially the ones which use regular expressions or some search patterns with the find command. An incorrect expression or pattern may delete essential data/system files or simply non-intended files.
  • Always secure the latest backup of important data and system files.
  • Take caution before running such commands especially when running it with Sudo or as superuser (root).

Not so popular. To remove a single file permanently, we can use unlink command.

Remove single file

There’s a more commonly used command for removing files, i.e., rm command, which supports removing one or more files simultaneously.

rm prompts you to confirm file deletion for files that are write-protected else it proceeds to directly remove the file. To make rm always prompt before deleting a file, use -i flag:

rm command deletes without displaying any messages on the screen. To list what rm command is actually doing, use rm with -v flag.

To remove write-protected files without prompting for confirmation, use -f flag.

Remove multiple files

Multiple files can be removed by specifying multiple filenames to rm as arguments.

rm also supports regular expressions. If you want to delete all files named file-name-* , you can use:

We can also specify multiple files using regular expressions. If we want to delete three files that match file-name-1 , file-name-2 and file-name-3 , we can use something like:

Remove directory

An empty directory can be deleted using rm command with -d option.

Options supported for file removal can also be combined with directory removal using -d flag as well. For example:

To delete a directory that is non-empty use -r flag.

If you don’t want any prompts before deleting the directory and its contents, use -rf flag. This WILL delete everything inside the directory including the directory itself without any confirmation. Do use caution while using it especially as root.

Find and remove files

For more complex requirements, we can use the find command with different options. To remove all files matching pattern inside a path given by

If we want to remove anything that matches a pattern including directories inside , we can slightly modify the above command as:

Modern versions of the find command support the delete function internally. Here -delete flag replaces rm command while -depth instructs find command to first process the contents of the directory before the directory itself :

The above example is more efficient when working with a large number of files as it doesn’t spawn a new external process for rm command for each matched file. But not all versions of find command (yet) may support -delete flag. As an alternate, we have the option to use xargs command with find as shown in the next example below:

Or use exec command’s + terminator to chain together everything found by find command like xargs:

By default, the find command uses -print flag which we usually skip writing. With xargs , we need to avoid new-line character between each filename. As such -print0 flag instructs find to print null character after every found filename.

Similarly, we specify -0 flag with xargs so that both commands couple up. rm command here deletes the file passed on to by piped find input. For example, the below command will search and remove all *.tmp files from the current user’s home directory (given by

find command offers several ways to search files and directories including ownership, permission, timestamp, etc. We’ll be covering a few such ways that can help us in specific file removal tasks.

Find and remove files by a user

To delete everything inside a given directory owned by a specific user , use:

In the above example, -mindepth flag with value 1 prevents the directory given by from deletion if it matches the find pattern and criteria. Or to delete everything inside a given directory belonging to a particular group , use:

If you do not want to traverse the sub-directories under the search path, you also have the option to use -maxdepth with appropriate directory depth value.

Here’s an example run:

If you want to specify user-id and group-id instead, you can try something like:

Find and remove empty directories

To delete all empty directories inside a given path , you can use:

Instead, to delete all empty files inside a given path , use:

Find and remove files older than X

Sometimes, you may need to delete files older than x days. find has options to read file’s creation ( ctime ), access ( atime ) and modification ( mtime ) times. We can use mtime option with find to find and delete files modified more than x days ago.

For instance, to delete all files with extension log inside /var/tmp with a modification time of 30 days ago or more, we can use:

Find and remove files by permission

Now, we can also delete files based on some specific permission, like:

Or if we want to use a symbolic form for permission, use:

Summing Up

Linux offers unlink , rm and rmdir commands that are simple and can be easily extended with the use of regular expressions. For more advanced needs, you have the option to use a combination of tools like find and xargs to achieve what you need. Other than the examples in this article, you’ve got the option to use find with any of its available flags to customize your search. Refer man pages for respective commands to learn more about them.

Always run find commands without rm or -delete flag and analyze its output to know which files or directories will be impacted by the execution of an actual command. Having proper backup configuration and policy helps not only in case of accidental deletions but also in cases of hardware failures and cyber-attacks.

Источник

Читайте также:  Gnu fortran для windows
Оцените статью