Delete folder that contain subfolders and files on linux
You need to delete that folder recursively,
rm -r folder-name
From rm --help
,
-r, -R, --recursive remove directories and their contents recursively
how to remove file from folder and subfolder single command linux
You can use "find" command with "delete" option. This will remove files with specified name in current directory and sub directories.
find . -name "http.log2019*" -delete
How to remove folders with a certain name
If the target directory is empty, use find, filter with only directories, filter by name, execute rmdir:
find . -type d -name a -exec rmdir {} \;
If you want to recursively delete its contents, replace -exec rmdir {} \;
with -delete
or -prune -exec rm -rf {} \;
. Other answers include details about these versions, credit them too.
How to delete all files or Sub-folders (both) in a folder except 2 folders with shell script
You haven't specified which shell you're using, but if you're using bash then extended globs can help:
printf '%s\n' !(@(conf|foldername2)/)
If you're happy with the list of files and directories produced by that, then pass the same glob to rm -rf
:
rm -rf !(@(conf|foldername2)/)
Inside a script, you may need to enable extglob using shopt -s extglob
. Later, you can change -s
to -u
to unset the option.
If you're using a different shell, then you can add some more options to your find
command:
find -maxdepth 1 ! -name 'conf' -a ! -name 'foldername2' -exec rm -rf {} +
Try it without the -exec
part first to print the matches rather than deleting everything.
How to delete all subdirectories with a specific name
If find
finds the correct directories at all, these should work:
find dir -type d -name "subdir1" -exec echo rm -rf {} \;
or
find dir -type d -name "subdir1" -exec echo rm -rf {} +
(the echo
is there for verifying the command hits the files you wanted, remove it to actually run the rm
and remove the directories.)
Both piping to xargs
and to while read
have the downside that unusual file names will cause issues. Also, find -delete
will only try to remove the directories themselves, not their contents. It will fail on any non-empty directories (but you should at least get errors).
With xargs
, spaces separate words by default, so even file names with spaces will not work. read
can deal with spaces, but in your command it's the unquoted expansion of $tar
that splits the variable on spaces.
If your filenames don't have newlines or trailing spaces, this should work, too:
find ... | while read -r x ; do rm -rf "$x" ; done
How to delete files/subfolders in a specific directory at the command prompt in Windows
You can use this shell script to clean up the folder and files within C:\Temp
source:
del /q "C:\Temp\*"
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q
Create a batch file (say, delete.bat) containing the above command. Go to the location where the delete.bat file is located and then run the command: delete.bat
Batch removing a sub folder in several parent folders
find . -name y -type d -exec sh -c '
for d; do echo mv "$d"/* "$d"/..; echo rmdir "$d"; done' _ {} +
Remove the echo
s if the results look like what you expect.
How to delete a subfolder with only a single file under the parent folder
Considering all of the subfolders
are in the same folder with no "sub-sub" depth, find
command prints subfolders with this one-liner and bash
handles the rest:
Sample folder and files:
$ find .
.
./subfolder_1
./subfolder_1/file1
./subfolder_1/file2
./subfolder_1/file3
./subfolder_2
./subfolder_2/file4
./subfolder_3
./subfolder_3/file5
./subfolder_3/file6
./subfolder_4
./subfolder_5
./subfolder_5/file7
One liner to remove subfolders containing only one file:
$ find . -not -empty -type d -print0 | while read -d '' -r dir; do files=("$dir"/*); if((${#files[@]} == "1")); then rm -r $dir exit; fi; done
List of what is remained after removing
$ find .
.
./subfolder_1
./subfolder_1/file1
./subfolder_1/file2
./subfolder_1/file3
./subfolder_3
./subfolder_3/file5
./subfolder_3/file6
./subfolder_4
Extra
List of subfolders with the number of files included:
$ find . -not -empty -type d -print0 | while read -d '' -r dir; do files=("$dir"/*); printf "${#files[@]} $dir\n"; done
6 .
3 ./subfolder_1
1 ./subfolder_2
2 ./subfolder_3
1 ./subfolder_5
List of subfolders containing only one file:
$ find . -not -empty -type d -print0 | while read -d '' -r dir; do files=("$dir"/*); if((${#files[@]} == "1")); then printf "${#files[@]} $dir\n" exit; fi; done
1 ./subfolder_2
1 ./subfolder_5
Related Topics
Phusion Passenger Nginx Module Installer V3.0.17 Issue on Debian 6.0.5 Amd64 Due to Broken Package
Why Does This Shell Pipeline Exit
One-Liner: Print All Lines Except the Last 3
Limit Output of All Linux Commands
What Is the Valid Address Space for a User Process? (Os X and Linux)
Why This Bash Function Prints Only First Word of Whole String
Is Ethernet Checksum Exposed via Af_Packet
Sched_Fifo Higher Priority Thread Is Getting Preempted by the Sched_Fifo Lower Priority Thread
How to Add Text at the Beginning of Specific Lines Using Sed
How to Use a Samba Server Location for Gopath
Why Can't I Access This Folder
What Is the Use Case for Epollet
Killing Linux Process by Piping the Id
Preventing to Bash Script from Running in Parallel or Overlap Using Cron
Call Printf System Subroutine to Output a Integer Number Error in Assembly Code