I was searching unsuccessfully for a solution to delete all the files inside a working directory except for the subdirectories inside.
I found a way to delete all the files inside all the directories, but I'm looking for a way to delete only the files on the same "level" I'm on.
Using rm command Here is the command to delete all files in your folder except the one with filename data. txt. Replace it with the filename of your choice. Here is the command to delete all files in your folder, except files data.
Details: ls myfolder/ lists all folders and files inside myfolder/ directory. grep -v "test2" matches everything except "test2" xargs rm -r pipes the output from previous command to rm -r command.
Select multiple files or folders that are not grouped together. Click the first file or folder, and then press and hold the Ctrl key. While holding Ctrl , click each of the other files or folders you want to select.
xargs rm -r says to delete any file output from the tail . The -r means to recursively delete files, so if it encounters a directory, it will delete everything in that directory, then delete the directory itself.
find . -maxdepth 1 -type f -print0 | xargs -0 rm
The find
command recursively searches a directory for files and folders that match the specified expressions.
-maxdepth 1
will only search the current level (when used with .
or the top level when a directory is used instead), effectively turning of the recursive search feature-type f
specifies only files, and all files@chepner recommended an improvement on the above to simply use
find . -maxdepth 1 -type f -delete
Not sure why I didn't think of it in the first place but anyway.
I think it is as easy as this:
$ rm *
when inside the working directory.
I've tested it and it worked - it deleted all files in the working directory and didn't affected any files inside any subdirectory.
Keep in mind, that it if you want to remove hidden files as well, then you need:
$ rm * .*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With