Linux remove all files in current directory

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

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

of course, you can always cd to your directory and then perform a «rm -i *» please note that the -i flag will force confirmation of each deletion, is there just for safety (nasty things will occurr if you misplace a / in your commandline and you provide a -r flag. )

I’m using rm -r * but it’s asking for a confirmation on each file deletion. To stop this it’s rm -rf * yes? Trying to delete all from current directory.

-r perform recursive deletion, -f forces deletion, assuming the rm command has not been aliased, I’d go with a rm -f *

it deletes all file inside the «yourdirectory» directory

I wouldn’t suggest to a unix newbie to use the -r switch, what if OP misplaces a / on the command line ?

Understandable but the issue is that i am clearing out spam from the remote qmail folder. There is thousands of messages to clear so I need a practical way of doing it without confirmation on each deletion.

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:

find /path/to/your/folder/ -type f -delete 

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

Источник

How to delete all files from current directory including current directory?

Under bash with GNU tools, I would do it like that (should be secure in most cases):

not under bash and without GNU tools, I would use:

TMP=`pwd -P` && cd "`dirname $TMP`" && rm -rf "./`basename $TMP`" && unset TMP 
  • end the argument list with — in cases our directory starts with a dash (non-bash: ./ before the filename)
  • pwd -P not just pwd in cases where we are not in a real directory but in a symlink pointing to it.
  • » s around the argument in cases the directory contains spaces
Читайте также:  Beginner linux command line

some random info (bash version):

EDIT: As kmkaplan noted, the — thing is not necessary, as pwd returns the complete path name which always starts with / on UNIX

what if you would like to include first a small dialog aka are you sure you want to delete this directory? and make alias for it all?

You can use rm -rfi instead of rm -rf and it will ask you «rm: remove directory `/tmp/dir_to_be_deleted’?», do you mean that?

rm -rfi would ask for every action in this directory, I was thinking something that will ask only once and delete everything.

read -n1 -p»Do you really want to delete ‘$(pwd -P)’ [yN]?» A && if [ «$(echo $A | tr Y y)» = «y» ]; then rm -rf — «$(pwd -P)» && cd .. ; fi

-P is the opposite of what I’d expect — if I’m in a symlinked dir and I try removing it, I’d expect the symlink to be unlinked, not the original dir, and certainly not leaving a broken symlink in its place.

olddir=`pwd` && cd .. && rm -rf "$olddir" 

The cd .. is needed, otherwise it will fail since you can’t remove the current directory.

kmkaplan, are you sure you can delete the current directory with rm? How many operating systems did you base that knowledge on?

dwc: yes I tested Linux, OpenBSD and MacOSX. But I am pretty sure every Unix would do the same and I even think every POSIX system will do it.

You can in Linux (just confirmed). Interestingly enough, once the directory is deleted, ls -al reports «total 0» instead of something like total X, with . and .. present

kmkaplan: sorry, thats correct! Didn’t thing about that. I always use — and most of the time it is necessary, but wiht pwd it’s not.

I think this would be possible under DOS / Windows CMD, but I can’t quite find a way to pipe the data between commands. Someone else may know the fix for that?

FOR /F %i IN ('cd') DO SET MyDir=%i | CD .. | RD /S %MyDir% 

operating system? on the *NIX-based stuff, you’re looking for ‘rm -rf directory/’

NOTE: the ‘-r’ flag for ‘recursive’ can be dangerous!

Читайте также:  Открытый программный код линукс

You just can go back the target folder’s parent folder, then use ‘rm -rf yourFolder’. or you can use ‘rm -rf *’ to delete all files and subfolders from the current folder.

Delete current directory with confirmation. Unlink if current dir is a symlink.

rmd() < if [[ -z "$1" ]]; then >&2 echo "usage: rmd " return 1 fi dir=$(realpath -se "$1") || return 2 if [[ -L "$dir" ]]; then read -s -k "?Unlink '$dir'? " echo if [[ $REPLY =~ ^[Yy]$ ]]; then unlink -- "$dir" else return 3 fi else read -s -k "?Recursively delete '$dir'? " echo if [[ $REPLY =~ ^[Yy]$ ]]; then rm -rf -- "$dir" else return 4 fi fi > rm.()
 /tmp/tmp.29mUflkHKU  base ❯ tree . . ├── bar -> foo └── foo 2 directories, 0 files  /tmp/tmp.29mUflkHKU  base ❯ cd bar  /tmp/tmp.29mUflkHKU/bar  base ❯ rm. Unlink /tmp/tmp.29mUflkHKU/bar?  /tmp/tmp.29mUflkHKU  base ❯ tree . . └── foo 1 directory, 0 files  /tmp/tmp.29mUflkHKU  base ❯ cd foo  /tmp/tmp.29mUflkHKU/foo  base ❯ rm. Recursively delete /tmp/tmp.29mUflkHKU/foo?  /tmp/tmp.29mUflkHKU  base ❯ tree . . 0 directories, 0 files 

Note: read -s -k is for zsh . Use read -n1 -p for bash (source)

Источник

Linux Delete All Files in Current Directory

You may need to delete files from your directory to free up space, clean up a project, or remove malware files. In this tutorial, we will learn how to delete all files in the current directory in Linux.

Delete all files in the directory

To delete all files in the directory use rm command followed by the path to the directory and a wildcard character «*».

This command will delete all files in the directory, but it will not delete any subdirectories or their contents. to delete subdirectories as well, you can add the «-r» flag to the command to make it recursive.

With that note: In Linux, it is best practice to navigate to a desired directory and perform the action from the current directory. Using an absolute path is more prone to accidentally choosing the incorrect path and may end up deleting the wrong files.

To delete all files in the directory /home/ubuntu/mydata/, type:

rm delete all files in a directory

Here all the non-hidden files in the directory are deleted. However, it returns an error for sub-directories. On a side note, you can exclude a specific file from deletion by rm -v !(filename.txt).

To remove all files and sub-directories from the directory /home/ubuntu/mydata, type:

Читайте также:  Linux find root path

rm delete all files and subdirectories in a directory

You can verify using ls -al /home/ubuntu/mydata to confirm all files and subdirectories in it are deleted.

Delete all files in the current directory

As mentioned before safest way would be to navigate to the directory and delete files from the current directory.

1. Navigate to the Directory

Use cd command to change the directory. For example, we are using /home/ubuntu/mydata.

Change to /home/ubuntu/mydata:

change to directory where the content needs to be deleted

Use pwd command to confirm the current path:

confirm current path

This will print out the absolute path of the directory.

2. List directory contents

List the directory content to make sure you going to delete the right files. For listing all files in the current directory use ls -al command.

list the directory contents

This command will list all the contents of the directory /home/ubuntu/mydata such as sub-directories and files ( including hidden files).

3. Delete all files in the Directory

Once we confirm we are in the right directory, we can delete all files in the current directory using rm -rv *.

from current directory remove all files and sub directories

This command will delete all the files and subdirectories in the current directory. The -v option gives a verbose output to show removed files and the directory.

Note: If you have any write-protected files and directories rm will prompt.

You can verify by typing ls -al

If you have any manually created hidden files, to remove them all use rm -rv .* command. This is because the asterisk (*) does not match files that begin with a dot, which are hidden files.

remove all files and subdirectories including manually created hidden files from a directory.

The individual directories . (indicates current directory) and .. (indicates parent directory ) will be still there — which cannot be deleted. No harm in keeping it.

Delete all files in directory without prompt

Use rm -rf * to delete all files in the directory without any prompt. Prompt occurs when rm encounters any errors such as write access protection. This option helps to avoid it.

For example, to delete all files in the directory /home/ubuntu/mydata without any prompt, type:

delete all files in a directory without prompt

We verify that the files have been deleted by running:

Conclusion

In this tutorial, we learned how to delete all files in a directory in Linux. Be cautious when using rm with asterisk command, as it can potentially delete important files. Always double-check the files you want to delete before executing the command.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

Оцените статью
Adblock
detector