clear multiple directories with rm
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to clear multiple directories stored in an array. Here's a simplified example (I have more directories).
#!/bin/bash
$IMAGES_DIR="/Users/michael/scripts/imagefiles"
$BACKUP_DIR="/Users/michael/scripts/imagebackups"
...
array=( $IMAGES_DIR $BACKUP_DIR )
for i in $array[@]
do
if [ "$(ls -A $i)" ]; then # check that directory has files in it
rm "$i/"* # remove them
fi
done
I get errors for each directory, e.g.:
rm: /Users/michael/scripts/imagefiles/*: No such file or directory
bash shell-script shell rm
add a comment |Â
up vote
1
down vote
favorite
I am trying to clear multiple directories stored in an array. Here's a simplified example (I have more directories).
#!/bin/bash
$IMAGES_DIR="/Users/michael/scripts/imagefiles"
$BACKUP_DIR="/Users/michael/scripts/imagebackups"
...
array=( $IMAGES_DIR $BACKUP_DIR )
for i in $array[@]
do
if [ "$(ls -A $i)" ]; then # check that directory has files in it
rm "$i/"* # remove them
fi
done
I get errors for each directory, e.g.:
rm: /Users/michael/scripts/imagefiles/*: No such file or directory
bash shell-script shell rm
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to clear multiple directories stored in an array. Here's a simplified example (I have more directories).
#!/bin/bash
$IMAGES_DIR="/Users/michael/scripts/imagefiles"
$BACKUP_DIR="/Users/michael/scripts/imagebackups"
...
array=( $IMAGES_DIR $BACKUP_DIR )
for i in $array[@]
do
if [ "$(ls -A $i)" ]; then # check that directory has files in it
rm "$i/"* # remove them
fi
done
I get errors for each directory, e.g.:
rm: /Users/michael/scripts/imagefiles/*: No such file or directory
bash shell-script shell rm
I am trying to clear multiple directories stored in an array. Here's a simplified example (I have more directories).
#!/bin/bash
$IMAGES_DIR="/Users/michael/scripts/imagefiles"
$BACKUP_DIR="/Users/michael/scripts/imagebackups"
...
array=( $IMAGES_DIR $BACKUP_DIR )
for i in $array[@]
do
if [ "$(ls -A $i)" ]; then # check that directory has files in it
rm "$i/"* # remove them
fi
done
I get errors for each directory, e.g.:
rm: /Users/michael/scripts/imagefiles/*: No such file or directory
bash shell-script shell rm
asked Dec 24 '17 at 2:07
Michael Riordan
385
385
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
How about accomplishing it all in a single command?
You can capture the file existence check, globbing and removal with one find
call. In the case of GNU's version of find
we'd have this:
for f in "$array[@]"; do
find "$f" -type f -delete
done
If you don't have GNU find
use this invocation:
find "$f" -type f -exec rm -f +
(If instead of clearing files from the entire directory hierarchy you only want to clear files that are immediate children then add -maxdepth 1
before -type f
.)
But wait, there's more....
As John1024 wisely notes you can forgo the loop altogether by passing the array as the first parameter to find
:
find "$array[@]" -type f -delete
That's because: 1) find
will accept multiple directories to be searched and processed in one execution 2) the shell will split the array such that each element (directory) becomes an individual positional parameter to find
.
1
Simpler:find "$array[@]" -type f -delete
â John1024
Dec 24 '17 at 3:53
1
Good one @John1024 ... I shoulda thought of that.
â B Layer
Dec 24 '17 at 3:54
add a comment |Â
up vote
1
down vote
Change your code to this:
#!/bin/bash
IMAGES_DIR="/Users/michael/scripts/imagefiles"
BACKUP_DIR="/Users/michael/scripts/imagebackups"
array=( $IMAGES_DIR $BACKUP_DIR )
for i in "$array[@]"
do
if [ "$(ls -A "$i")" ]; then
rm "$i:?"/*
fi
done
Errors:
- Placing
$
on the left hand side of variable assignments - Not quoting the
$i
inif [ "$(ls -A $i)" ];then
- Use
"$var:?"
to ensure this,rm "$i/"*
never expands to/*
Let me test it, thanks for the HUP!
â George Udosen
Dec 24 '17 at 3:03
Ok tested on two directories and worked!
â George Udosen
Dec 24 '17 at 3:27
add a comment |Â
up vote
-1
down vote
Please find the below awk oneliner to achieve the same , As tested it worked fine
i="/root/";ls -ltr /root/| grep "^-rw" | awk -v i="$i" 'print "rm -rvf" " " i$9' | sh
for example i have assigned variable i = path /root/ you can change as per your requirement
i="/root/" ===> path
See Why you shouldn't parse the output of ls(1)
â steeldriver
Dec 24 '17 at 13:58
@steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
â Praveen Kumar BS
Dec 25 '17 at 2:53
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
How about accomplishing it all in a single command?
You can capture the file existence check, globbing and removal with one find
call. In the case of GNU's version of find
we'd have this:
for f in "$array[@]"; do
find "$f" -type f -delete
done
If you don't have GNU find
use this invocation:
find "$f" -type f -exec rm -f +
(If instead of clearing files from the entire directory hierarchy you only want to clear files that are immediate children then add -maxdepth 1
before -type f
.)
But wait, there's more....
As John1024 wisely notes you can forgo the loop altogether by passing the array as the first parameter to find
:
find "$array[@]" -type f -delete
That's because: 1) find
will accept multiple directories to be searched and processed in one execution 2) the shell will split the array such that each element (directory) becomes an individual positional parameter to find
.
1
Simpler:find "$array[@]" -type f -delete
â John1024
Dec 24 '17 at 3:53
1
Good one @John1024 ... I shoulda thought of that.
â B Layer
Dec 24 '17 at 3:54
add a comment |Â
up vote
3
down vote
How about accomplishing it all in a single command?
You can capture the file existence check, globbing and removal with one find
call. In the case of GNU's version of find
we'd have this:
for f in "$array[@]"; do
find "$f" -type f -delete
done
If you don't have GNU find
use this invocation:
find "$f" -type f -exec rm -f +
(If instead of clearing files from the entire directory hierarchy you only want to clear files that are immediate children then add -maxdepth 1
before -type f
.)
But wait, there's more....
As John1024 wisely notes you can forgo the loop altogether by passing the array as the first parameter to find
:
find "$array[@]" -type f -delete
That's because: 1) find
will accept multiple directories to be searched and processed in one execution 2) the shell will split the array such that each element (directory) becomes an individual positional parameter to find
.
1
Simpler:find "$array[@]" -type f -delete
â John1024
Dec 24 '17 at 3:53
1
Good one @John1024 ... I shoulda thought of that.
â B Layer
Dec 24 '17 at 3:54
add a comment |Â
up vote
3
down vote
up vote
3
down vote
How about accomplishing it all in a single command?
You can capture the file existence check, globbing and removal with one find
call. In the case of GNU's version of find
we'd have this:
for f in "$array[@]"; do
find "$f" -type f -delete
done
If you don't have GNU find
use this invocation:
find "$f" -type f -exec rm -f +
(If instead of clearing files from the entire directory hierarchy you only want to clear files that are immediate children then add -maxdepth 1
before -type f
.)
But wait, there's more....
As John1024 wisely notes you can forgo the loop altogether by passing the array as the first parameter to find
:
find "$array[@]" -type f -delete
That's because: 1) find
will accept multiple directories to be searched and processed in one execution 2) the shell will split the array such that each element (directory) becomes an individual positional parameter to find
.
How about accomplishing it all in a single command?
You can capture the file existence check, globbing and removal with one find
call. In the case of GNU's version of find
we'd have this:
for f in "$array[@]"; do
find "$f" -type f -delete
done
If you don't have GNU find
use this invocation:
find "$f" -type f -exec rm -f +
(If instead of clearing files from the entire directory hierarchy you only want to clear files that are immediate children then add -maxdepth 1
before -type f
.)
But wait, there's more....
As John1024 wisely notes you can forgo the loop altogether by passing the array as the first parameter to find
:
find "$array[@]" -type f -delete
That's because: 1) find
will accept multiple directories to be searched and processed in one execution 2) the shell will split the array such that each element (directory) becomes an individual positional parameter to find
.
edited Dec 24 '17 at 4:36
answered Dec 24 '17 at 3:46
B Layer
3,8991525
3,8991525
1
Simpler:find "$array[@]" -type f -delete
â John1024
Dec 24 '17 at 3:53
1
Good one @John1024 ... I shoulda thought of that.
â B Layer
Dec 24 '17 at 3:54
add a comment |Â
1
Simpler:find "$array[@]" -type f -delete
â John1024
Dec 24 '17 at 3:53
1
Good one @John1024 ... I shoulda thought of that.
â B Layer
Dec 24 '17 at 3:54
1
1
Simpler:
find "$array[@]" -type f -delete
â John1024
Dec 24 '17 at 3:53
Simpler:
find "$array[@]" -type f -delete
â John1024
Dec 24 '17 at 3:53
1
1
Good one @John1024 ... I shoulda thought of that.
â B Layer
Dec 24 '17 at 3:54
Good one @John1024 ... I shoulda thought of that.
â B Layer
Dec 24 '17 at 3:54
add a comment |Â
up vote
1
down vote
Change your code to this:
#!/bin/bash
IMAGES_DIR="/Users/michael/scripts/imagefiles"
BACKUP_DIR="/Users/michael/scripts/imagebackups"
array=( $IMAGES_DIR $BACKUP_DIR )
for i in "$array[@]"
do
if [ "$(ls -A "$i")" ]; then
rm "$i:?"/*
fi
done
Errors:
- Placing
$
on the left hand side of variable assignments - Not quoting the
$i
inif [ "$(ls -A $i)" ];then
- Use
"$var:?"
to ensure this,rm "$i/"*
never expands to/*
Let me test it, thanks for the HUP!
â George Udosen
Dec 24 '17 at 3:03
Ok tested on two directories and worked!
â George Udosen
Dec 24 '17 at 3:27
add a comment |Â
up vote
1
down vote
Change your code to this:
#!/bin/bash
IMAGES_DIR="/Users/michael/scripts/imagefiles"
BACKUP_DIR="/Users/michael/scripts/imagebackups"
array=( $IMAGES_DIR $BACKUP_DIR )
for i in "$array[@]"
do
if [ "$(ls -A "$i")" ]; then
rm "$i:?"/*
fi
done
Errors:
- Placing
$
on the left hand side of variable assignments - Not quoting the
$i
inif [ "$(ls -A $i)" ];then
- Use
"$var:?"
to ensure this,rm "$i/"*
never expands to/*
Let me test it, thanks for the HUP!
â George Udosen
Dec 24 '17 at 3:03
Ok tested on two directories and worked!
â George Udosen
Dec 24 '17 at 3:27
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Change your code to this:
#!/bin/bash
IMAGES_DIR="/Users/michael/scripts/imagefiles"
BACKUP_DIR="/Users/michael/scripts/imagebackups"
array=( $IMAGES_DIR $BACKUP_DIR )
for i in "$array[@]"
do
if [ "$(ls -A "$i")" ]; then
rm "$i:?"/*
fi
done
Errors:
- Placing
$
on the left hand side of variable assignments - Not quoting the
$i
inif [ "$(ls -A $i)" ];then
- Use
"$var:?"
to ensure this,rm "$i/"*
never expands to/*
Change your code to this:
#!/bin/bash
IMAGES_DIR="/Users/michael/scripts/imagefiles"
BACKUP_DIR="/Users/michael/scripts/imagebackups"
array=( $IMAGES_DIR $BACKUP_DIR )
for i in "$array[@]"
do
if [ "$(ls -A "$i")" ]; then
rm "$i:?"/*
fi
done
Errors:
- Placing
$
on the left hand side of variable assignments - Not quoting the
$i
inif [ "$(ls -A $i)" ];then
- Use
"$var:?"
to ensure this,rm "$i/"*
never expands to/*
edited Dec 24 '17 at 3:33
answered Dec 24 '17 at 2:57
George Udosen
1,112318
1,112318
Let me test it, thanks for the HUP!
â George Udosen
Dec 24 '17 at 3:03
Ok tested on two directories and worked!
â George Udosen
Dec 24 '17 at 3:27
add a comment |Â
Let me test it, thanks for the HUP!
â George Udosen
Dec 24 '17 at 3:03
Ok tested on two directories and worked!
â George Udosen
Dec 24 '17 at 3:27
Let me test it, thanks for the HUP!
â George Udosen
Dec 24 '17 at 3:03
Let me test it, thanks for the HUP!
â George Udosen
Dec 24 '17 at 3:03
Ok tested on two directories and worked!
â George Udosen
Dec 24 '17 at 3:27
Ok tested on two directories and worked!
â George Udosen
Dec 24 '17 at 3:27
add a comment |Â
up vote
-1
down vote
Please find the below awk oneliner to achieve the same , As tested it worked fine
i="/root/";ls -ltr /root/| grep "^-rw" | awk -v i="$i" 'print "rm -rvf" " " i$9' | sh
for example i have assigned variable i = path /root/ you can change as per your requirement
i="/root/" ===> path
See Why you shouldn't parse the output of ls(1)
â steeldriver
Dec 24 '17 at 13:58
@steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
â Praveen Kumar BS
Dec 25 '17 at 2:53
add a comment |Â
up vote
-1
down vote
Please find the below awk oneliner to achieve the same , As tested it worked fine
i="/root/";ls -ltr /root/| grep "^-rw" | awk -v i="$i" 'print "rm -rvf" " " i$9' | sh
for example i have assigned variable i = path /root/ you can change as per your requirement
i="/root/" ===> path
See Why you shouldn't parse the output of ls(1)
â steeldriver
Dec 24 '17 at 13:58
@steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
â Praveen Kumar BS
Dec 25 '17 at 2:53
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
Please find the below awk oneliner to achieve the same , As tested it worked fine
i="/root/";ls -ltr /root/| grep "^-rw" | awk -v i="$i" 'print "rm -rvf" " " i$9' | sh
for example i have assigned variable i = path /root/ you can change as per your requirement
i="/root/" ===> path
Please find the below awk oneliner to achieve the same , As tested it worked fine
i="/root/";ls -ltr /root/| grep "^-rw" | awk -v i="$i" 'print "rm -rvf" " " i$9' | sh
for example i have assigned variable i = path /root/ you can change as per your requirement
i="/root/" ===> path
answered Dec 24 '17 at 7:56
Praveen Kumar BS
1,010128
1,010128
See Why you shouldn't parse the output of ls(1)
â steeldriver
Dec 24 '17 at 13:58
@steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
â Praveen Kumar BS
Dec 25 '17 at 2:53
add a comment |Â
See Why you shouldn't parse the output of ls(1)
â steeldriver
Dec 24 '17 at 13:58
@steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
â Praveen Kumar BS
Dec 25 '17 at 2:53
See Why you shouldn't parse the output of ls(1)
â steeldriver
Dec 24 '17 at 13:58
See Why you shouldn't parse the output of ls(1)
â steeldriver
Dec 24 '17 at 13:58
@steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
â Praveen Kumar BS
Dec 25 '17 at 2:53
@steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
â Praveen Kumar BS
Dec 25 '17 at 2:53
add a comment |Â
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%2f412749%2fclear-multiple-directories-with-rm%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