Remove files + files from subdirectories in Directory
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I would like to remove all files from a directory /data which includes 8 other sub directories.
Is there a command which will recursively clear all subdirectories but not remove the actual folders?
rm
New contributor
add a comment |
up vote
0
down vote
favorite
I would like to remove all files from a directory /data which includes 8 other sub directories.
Is there a command which will recursively clear all subdirectories but not remove the actual folders?
rm
New contributor
Related: unix.stackexchange.com/q/182033/117549
– Jeff Schaller
6 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to remove all files from a directory /data which includes 8 other sub directories.
Is there a command which will recursively clear all subdirectories but not remove the actual folders?
rm
New contributor
I would like to remove all files from a directory /data which includes 8 other sub directories.
Is there a command which will recursively clear all subdirectories but not remove the actual folders?
rm
rm
New contributor
New contributor
New contributor
asked 6 hours ago
nm97
11
11
New contributor
New contributor
Related: unix.stackexchange.com/q/182033/117549
– Jeff Schaller
6 hours ago
add a comment |
Related: unix.stackexchange.com/q/182033/117549
– Jeff Schaller
6 hours ago
Related: unix.stackexchange.com/q/182033/117549
– Jeff Schaller
6 hours ago
Related: unix.stackexchange.com/q/182033/117549
– Jeff Schaller
6 hours ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
The following would delete any non-directory files, like regular files, symbolic links, named pipes, sockets etc., in or under the /data
directory:
find /data ! -type d -delete
For implementations of find
that does not have the non-standard predicate -delete
, use -exec rm -f +
in its place:
find /data ! -type d -exec rm -f +
This would find all non-directory files in or under /data
and would execute rm -f
on as large batches of these as possible.
add a comment |
up vote
0
down vote
Use find command
find /data -type f -exec rm -rf ;
will delete only files due to the type selection type f for files.
1
No need for -r if you're removing files...
– Jeff Schaller
6 hours ago
add a comment |
up vote
0
down vote
You can use the find
command for this.
To create a test case to reproduce your description, let me do this:
1. cd /tmp
2. mkdir -p testing/a,b,c
3. cd testing/
4. touch a,b,c/1,2,3
To verify there are multiple directories each containing multiple files:
$ find -type f
./c/3
./c/2
./c/1
./b/3
./b/2
./b/1
./a/3
./a/2
./a/1
You can now use find
again to delete whatever it finds:
find -type f -delete
If you now run find -type f
again it will not return any results, because the files are gone, but you can see that the directories still exist:
$ ls
a b c
The find
command is very powerful. You can discover more about it using man find
.
1
The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
– Jeff Schaller
6 hours ago
@JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that-delete
is a GNUism :)
– cryptarch
6 hours ago
1
@JeffSchaller,-delete
is from FreeBSD (1996). Only added to GNUfind
in 4.2.3 (2004) (and to NetBSD find in 2007)
– Stéphane Chazelas
5 hours ago
1
@StéphaneChazelas-delete
was added in January 2017 to OpenBSDfind
, if you want to be complete...
– Kusalananda
5 hours ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
The following would delete any non-directory files, like regular files, symbolic links, named pipes, sockets etc., in or under the /data
directory:
find /data ! -type d -delete
For implementations of find
that does not have the non-standard predicate -delete
, use -exec rm -f +
in its place:
find /data ! -type d -exec rm -f +
This would find all non-directory files in or under /data
and would execute rm -f
on as large batches of these as possible.
add a comment |
up vote
3
down vote
The following would delete any non-directory files, like regular files, symbolic links, named pipes, sockets etc., in or under the /data
directory:
find /data ! -type d -delete
For implementations of find
that does not have the non-standard predicate -delete
, use -exec rm -f +
in its place:
find /data ! -type d -exec rm -f +
This would find all non-directory files in or under /data
and would execute rm -f
on as large batches of these as possible.
add a comment |
up vote
3
down vote
up vote
3
down vote
The following would delete any non-directory files, like regular files, symbolic links, named pipes, sockets etc., in or under the /data
directory:
find /data ! -type d -delete
For implementations of find
that does not have the non-standard predicate -delete
, use -exec rm -f +
in its place:
find /data ! -type d -exec rm -f +
This would find all non-directory files in or under /data
and would execute rm -f
on as large batches of these as possible.
The following would delete any non-directory files, like regular files, symbolic links, named pipes, sockets etc., in or under the /data
directory:
find /data ! -type d -delete
For implementations of find
that does not have the non-standard predicate -delete
, use -exec rm -f +
in its place:
find /data ! -type d -exec rm -f +
This would find all non-directory files in or under /data
and would execute rm -f
on as large batches of these as possible.
edited 6 hours ago
answered 6 hours ago
Kusalananda
114k15218349
114k15218349
add a comment |
add a comment |
up vote
0
down vote
Use find command
find /data -type f -exec rm -rf ;
will delete only files due to the type selection type f for files.
1
No need for -r if you're removing files...
– Jeff Schaller
6 hours ago
add a comment |
up vote
0
down vote
Use find command
find /data -type f -exec rm -rf ;
will delete only files due to the type selection type f for files.
1
No need for -r if you're removing files...
– Jeff Schaller
6 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Use find command
find /data -type f -exec rm -rf ;
will delete only files due to the type selection type f for files.
Use find command
find /data -type f -exec rm -rf ;
will delete only files due to the type selection type f for files.
answered 6 hours ago
francois P
909114
909114
1
No need for -r if you're removing files...
– Jeff Schaller
6 hours ago
add a comment |
1
No need for -r if you're removing files...
– Jeff Schaller
6 hours ago
1
1
No need for -r if you're removing files...
– Jeff Schaller
6 hours ago
No need for -r if you're removing files...
– Jeff Schaller
6 hours ago
add a comment |
up vote
0
down vote
You can use the find
command for this.
To create a test case to reproduce your description, let me do this:
1. cd /tmp
2. mkdir -p testing/a,b,c
3. cd testing/
4. touch a,b,c/1,2,3
To verify there are multiple directories each containing multiple files:
$ find -type f
./c/3
./c/2
./c/1
./b/3
./b/2
./b/1
./a/3
./a/2
./a/1
You can now use find
again to delete whatever it finds:
find -type f -delete
If you now run find -type f
again it will not return any results, because the files are gone, but you can see that the directories still exist:
$ ls
a b c
The find
command is very powerful. You can discover more about it using man find
.
1
The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
– Jeff Schaller
6 hours ago
@JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that-delete
is a GNUism :)
– cryptarch
6 hours ago
1
@JeffSchaller,-delete
is from FreeBSD (1996). Only added to GNUfind
in 4.2.3 (2004) (and to NetBSD find in 2007)
– Stéphane Chazelas
5 hours ago
1
@StéphaneChazelas-delete
was added in January 2017 to OpenBSDfind
, if you want to be complete...
– Kusalananda
5 hours ago
add a comment |
up vote
0
down vote
You can use the find
command for this.
To create a test case to reproduce your description, let me do this:
1. cd /tmp
2. mkdir -p testing/a,b,c
3. cd testing/
4. touch a,b,c/1,2,3
To verify there are multiple directories each containing multiple files:
$ find -type f
./c/3
./c/2
./c/1
./b/3
./b/2
./b/1
./a/3
./a/2
./a/1
You can now use find
again to delete whatever it finds:
find -type f -delete
If you now run find -type f
again it will not return any results, because the files are gone, but you can see that the directories still exist:
$ ls
a b c
The find
command is very powerful. You can discover more about it using man find
.
1
The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
– Jeff Schaller
6 hours ago
@JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that-delete
is a GNUism :)
– cryptarch
6 hours ago
1
@JeffSchaller,-delete
is from FreeBSD (1996). Only added to GNUfind
in 4.2.3 (2004) (and to NetBSD find in 2007)
– Stéphane Chazelas
5 hours ago
1
@StéphaneChazelas-delete
was added in January 2017 to OpenBSDfind
, if you want to be complete...
– Kusalananda
5 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use the find
command for this.
To create a test case to reproduce your description, let me do this:
1. cd /tmp
2. mkdir -p testing/a,b,c
3. cd testing/
4. touch a,b,c/1,2,3
To verify there are multiple directories each containing multiple files:
$ find -type f
./c/3
./c/2
./c/1
./b/3
./b/2
./b/1
./a/3
./a/2
./a/1
You can now use find
again to delete whatever it finds:
find -type f -delete
If you now run find -type f
again it will not return any results, because the files are gone, but you can see that the directories still exist:
$ ls
a b c
The find
command is very powerful. You can discover more about it using man find
.
You can use the find
command for this.
To create a test case to reproduce your description, let me do this:
1. cd /tmp
2. mkdir -p testing/a,b,c
3. cd testing/
4. touch a,b,c/1,2,3
To verify there are multiple directories each containing multiple files:
$ find -type f
./c/3
./c/2
./c/1
./b/3
./b/2
./b/1
./a/3
./a/2
./a/1
You can now use find
again to delete whatever it finds:
find -type f -delete
If you now run find -type f
again it will not return any results, because the files are gone, but you can see that the directories still exist:
$ ls
a b c
The find
command is very powerful. You can discover more about it using man find
.
answered 6 hours ago
cryptarch
2514
2514
1
The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
– Jeff Schaller
6 hours ago
@JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that-delete
is a GNUism :)
– cryptarch
6 hours ago
1
@JeffSchaller,-delete
is from FreeBSD (1996). Only added to GNUfind
in 4.2.3 (2004) (and to NetBSD find in 2007)
– Stéphane Chazelas
5 hours ago
1
@StéphaneChazelas-delete
was added in January 2017 to OpenBSDfind
, if you want to be complete...
– Kusalananda
5 hours ago
add a comment |
1
The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
– Jeff Schaller
6 hours ago
@JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that-delete
is a GNUism :)
– cryptarch
6 hours ago
1
@JeffSchaller,-delete
is from FreeBSD (1996). Only added to GNUfind
in 4.2.3 (2004) (and to NetBSD find in 2007)
– Stéphane Chazelas
5 hours ago
1
@StéphaneChazelas-delete
was added in January 2017 to OpenBSDfind
, if you want to be complete...
– Kusalananda
5 hours ago
1
1
The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
– Jeff Schaller
6 hours ago
The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
– Jeff Schaller
6 hours ago
@JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that
-delete
is a GNUism :)– cryptarch
6 hours ago
@JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that
-delete
is a GNUism :)– cryptarch
6 hours ago
1
1
@JeffSchaller,
-delete
is from FreeBSD (1996). Only added to GNU find
in 4.2.3 (2004) (and to NetBSD find in 2007)– Stéphane Chazelas
5 hours ago
@JeffSchaller,
-delete
is from FreeBSD (1996). Only added to GNU find
in 4.2.3 (2004) (and to NetBSD find in 2007)– Stéphane Chazelas
5 hours ago
1
1
@StéphaneChazelas
-delete
was added in January 2017 to OpenBSD find
, if you want to be complete...– Kusalananda
5 hours ago
@StéphaneChazelas
-delete
was added in January 2017 to OpenBSD find
, if you want to be complete...– Kusalananda
5 hours ago
add a comment |
nm97 is a new contributor. Be nice, and check out our Code of Conduct.
nm97 is a new contributor. Be nice, and check out our Code of Conduct.
nm97 is a new contributor. Be nice, and check out our Code of Conduct.
nm97 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481152%2fremove-files-files-from-subdirectories-in-directory%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Related: unix.stackexchange.com/q/182033/117549
– Jeff Schaller
6 hours ago