Меню Рубрики

Linux delete all files in directory

How to remove all the files in a directory?

I am trying to remove all files and subdirectories in a directory. I used rm -r to remove all files, but I want to remove all files and subdirectories, excluding the top directory itself.

For example, I have a top directory like images . It contains the files header.png , footer.png and a subdirectory.

Now I want to delete header.png , footer.png and the subdirectory, but not images .

How can I do this in linux?

14 Answers 14

If your top-level directory is called images , then run rm -r images/* . This uses the shell glob operator * to run rm -r on every file or directory within images .

To delete hidden files, you have to specify:

With shells whose globs include . and .. , this will lead to an error like

but it will delete hidden files.

An approach without errormessage is to use find/delete with mindepth. This is gnu-find.

Your find may lack the -mindepth or -delete predicate, in which case, you could do:

Источник

How to remove all files from a directory?

The closest I’ve gotten is

but that doesn’t work for files that don’t have an extension.

9 Answers 9

Linux does not use extensions. It is up to the creator of the file to decide whether the name should have an extension. Linux looks at the first few bytes to figure out what kind of file it is dealing with.

To remove all non-hidden files* in a directory use:

However, this will show an error for each sub-directory, because in this mode it is only allowed to delete files.

To remove all non-hidden files and sub-directories (along with all of their contents) in a directory use:

* Hidden files and directories are those whose names start with . (dot) character, e.g.: .hidden-file or .hidden-directory/ . Note that, in Bash, if the dotglob option (which is off by default) is set, rm will act on hidden files too, because they will be included when * is expanded by the shell to provide the list of filename arguments.

To remove a folder with all its contents (including all interior folders):

To remove all the contents of the folder (including all interior folders) but not the folder itself:

or, if you want to make sure that hidden files/directories are also removed:

To remove all the «files» from inside a folder(not removing interior folders):

Warning: if you have spaces in your path, make sure to always use quotes.

is equivalent to 2 separate rm -rf calls:

To avoid this issue, you can use ‘ single-quotes ‘ (prevents all expansions, even of shell variables) or » double-quotes » (allows expansion of shell variables, but prevents other expansions):

  • rm — stands for remove
  • -f — stands for force which is helpful when you don’t want to be asked/prompted if you want to remove an archive, for example.
  • -r — stands for recursive which means that you want to go recursively down every folder and remove everything.

Источник

Unix Command to Delete all files in a directory but preserve the directory

I am looking for a unix command to delete all files within a directory without deleting the directory itself. (note the directory does not contain subdirectories).

8 Answers 8

this should do the trick

EDIT: added -i just in case (safety first). directory should be a full or relative path (e.g. /tmp/foo or ../trash/stuffs )

it deletes all file inside the «yourdirectory» directory

You can use find /path/to/your/folder/ -delete to delete everything within that folder.

While a wildcard rm would braek with too many files («Argument list too long»), this works no matter how many files there are.

You can also make it delete only files but preserve any subdirectories:

You could also specify any other criteria find supports to restrict the «results».

you can remove all the files form the current directory using rm * if you want to remove from a specific directory, type rm /path/*

If you are in the directory where you want to remove all the files then the following command works fine:

If you want to Delete all file as well as all Directory that means all things then try this:

you can use rm -r /UrDir/*.* This would ignore the the files in the sub-directories

This will help you,

In this command,if mydir has any sub_directory! it’ll raise error an message and skip that sub_directory and remove rest of the files in main directory.

Not the answer you’re looking for? Browse other questions tagged unix or ask your own question.

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2020.9.18.37632

Источник

Linux Delete All Files In Directory Using Command Line

Linux Delete All Files In Directory

The procedure to remove all files from a directory:

    Open the terminal application

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

/data/

To delete all files in a directory named /home/vivek/data/, run:
$ rm /home/vivek/data/*
You can see what is being done when deleting all files in directory pass the -v option to the rm command:
$ rm -v /home/vivek/data/*
Verify using the ls command:
$ ls -l /home/vivek/data/

As you can see rm command failed to remove subdirectories /home/vivek/data/images and /home/vivek/data/scripts. To delete all files folders from a directory, run:
$ rm -rfv /home/vivek/data/

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/

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:

# Bash shell and may not work on other shells # Turn on dotglob (set) # shopt -s dotglob # Remove all files including hidden .files # rm -v

/project/oldfiles/* rm -vrf

/project/oldfiles/* # Turn off dotglob (unset) # shopt -u dotglob

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:

Источник

Remove only files in directory on linux NOT directories

What delete command can be run to remove only files in given directory

  • NOT directories
  • NOT sub-directories
  • NOT files in these sub-directories.

Some files don’t have extensions so rm *.* wont work.

There are thousands of files in this folder.

10 Answers 10

You can use find with -type f for files only and -maxdepth 1 so find won’t search for files in sub-directories of /path/to/directory . rm -i will prompt you on each delete so you can confirm or deny the delete. If you dont care about being asked for confirmation of each delete, change it to rm -fv ( -f for force the delete). The -v flag makes it so that with each delete, a message is printed saying what file was just deleted.

This should meet the criteria:

NOT directories
NOT subdirectories
NOT files in these subdirectories.

BUT this won’t prompt you for confirmation or output what it just deleted. Therefore best to run it without the -delete action first and check that they’re the correct files.

Since this is high on google search, the simplest answer is:

where $directoryPath is the directory you want to empty. Credits should go to cbm3384 (that for some reason has gotten negative votes for this answer, why?)

If you do not want to confirm:

If you don’t believe try man rm or

The above creates a directory structure, that has ‘helloX.txt’ in each folder (X is the directory level). rm 1/2/* deletes hello2.txt and leaves the other structure intact.

Also rm */*/* deletes only hello2.txt . It is the only that matches the pattern.

Just an example of a Makefile that cleans cakephp tmp-directory and leaves the directory structure intact:

Minus in front of the rm means «do not halt on errors» (unremoved directory returns an error). If you want some level to be saved, just remove that line, e.g. second rm line removes logs.

Let me know if you have a system that does something else (BSD?).

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

  • Mac os какую версию выбрать
  • Mac os какие файловые системы поддерживает
  • Mac os какие игры идут
  • Mac os какая система лучше
  • Mac os как читается