Trying to script deleting files from wildcards in a text file
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I'm trying to script deleting files whose filenames match wildcards in a text file.
I'm using the following
if [ -z "$1" ]; then
echo -e "Usage: $(basename $0) FILEn"
exit 1
fi
if [ ! -e "$1" ]; then
echo -e "$1: File doesn't exist.n"
exit 1
fi
while read -r line; do
[ -n "$line" ] && rm -- "$line"
done < "$1"
in the file list
there are lines
file*
test*
I get the following if i run this
rm: cannot remove âÂÂfile*âÂÂ: No such file or directory
rm: cannot remove âÂÂtest*âÂÂ: No such file or directory
I think the * isn't accepted to delete files such as
file 1
file2
file2.txt
test 001 more teskt.txt
Sorry I'm no linux expert. Maybe someone has an easy answer, maybe replace the * with something?
command-line bash scripts
New contributor
add a comment |Â
up vote
3
down vote
favorite
I'm trying to script deleting files whose filenames match wildcards in a text file.
I'm using the following
if [ -z "$1" ]; then
echo -e "Usage: $(basename $0) FILEn"
exit 1
fi
if [ ! -e "$1" ]; then
echo -e "$1: File doesn't exist.n"
exit 1
fi
while read -r line; do
[ -n "$line" ] && rm -- "$line"
done < "$1"
in the file list
there are lines
file*
test*
I get the following if i run this
rm: cannot remove âÂÂfile*âÂÂ: No such file or directory
rm: cannot remove âÂÂtest*âÂÂ: No such file or directory
I think the * isn't accepted to delete files such as
file 1
file2
file2.txt
test 001 more teskt.txt
Sorry I'm no linux expert. Maybe someone has an easy answer, maybe replace the * with something?
command-line bash scripts
New contributor
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm trying to script deleting files whose filenames match wildcards in a text file.
I'm using the following
if [ -z "$1" ]; then
echo -e "Usage: $(basename $0) FILEn"
exit 1
fi
if [ ! -e "$1" ]; then
echo -e "$1: File doesn't exist.n"
exit 1
fi
while read -r line; do
[ -n "$line" ] && rm -- "$line"
done < "$1"
in the file list
there are lines
file*
test*
I get the following if i run this
rm: cannot remove âÂÂfile*âÂÂ: No such file or directory
rm: cannot remove âÂÂtest*âÂÂ: No such file or directory
I think the * isn't accepted to delete files such as
file 1
file2
file2.txt
test 001 more teskt.txt
Sorry I'm no linux expert. Maybe someone has an easy answer, maybe replace the * with something?
command-line bash scripts
New contributor
I'm trying to script deleting files whose filenames match wildcards in a text file.
I'm using the following
if [ -z "$1" ]; then
echo -e "Usage: $(basename $0) FILEn"
exit 1
fi
if [ ! -e "$1" ]; then
echo -e "$1: File doesn't exist.n"
exit 1
fi
while read -r line; do
[ -n "$line" ] && rm -- "$line"
done < "$1"
in the file list
there are lines
file*
test*
I get the following if i run this
rm: cannot remove âÂÂfile*âÂÂ: No such file or directory
rm: cannot remove âÂÂtest*âÂÂ: No such file or directory
I think the * isn't accepted to delete files such as
file 1
file2
file2.txt
test 001 more teskt.txt
Sorry I'm no linux expert. Maybe someone has an easy answer, maybe replace the * with something?
command-line bash scripts
command-line bash scripts
New contributor
New contributor
edited 3 hours ago
wjandrea
7,23342256
7,23342256
New contributor
asked 3 hours ago
Marco
165
165
New contributor
New contributor
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
Pathname expansion happens after variable expansion, but only on unquoted parts of the command line.
rm -- $line # <- no double quotes to expand wildcards
This wouldn't work for filenames containing spaces, as word splitting also happens after variable expansion.
You can use Perl to expand globs:
perl -lne 'unlink glob' -- list.txt
Normal glob needs whitespace backslashed, but you can switch to File::Glob for a different behaviour if it's more convenient to you:
perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt
To clarify, this won't work as expected if$line
contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
â wjandrea
3 hours ago
1
or just iterate over glob . . . less mess that way
â Sergiy Kolodyazhnyy
3 hours ago
Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
â Marco
3 hours ago
@Marco For a thorough solution, you'd have to escape every character in$IFS
.
â wjandrea
3 hours ago
1
No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
â choroba
3 hours ago
 |Â
show 3 more comments
up vote
-1
down vote
Thanks all
perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt
work well upto now
New contributor
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Pathname expansion happens after variable expansion, but only on unquoted parts of the command line.
rm -- $line # <- no double quotes to expand wildcards
This wouldn't work for filenames containing spaces, as word splitting also happens after variable expansion.
You can use Perl to expand globs:
perl -lne 'unlink glob' -- list.txt
Normal glob needs whitespace backslashed, but you can switch to File::Glob for a different behaviour if it's more convenient to you:
perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt
To clarify, this won't work as expected if$line
contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
â wjandrea
3 hours ago
1
or just iterate over glob . . . less mess that way
â Sergiy Kolodyazhnyy
3 hours ago
Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
â Marco
3 hours ago
@Marco For a thorough solution, you'd have to escape every character in$IFS
.
â wjandrea
3 hours ago
1
No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
â choroba
3 hours ago
 |Â
show 3 more comments
up vote
4
down vote
accepted
Pathname expansion happens after variable expansion, but only on unquoted parts of the command line.
rm -- $line # <- no double quotes to expand wildcards
This wouldn't work for filenames containing spaces, as word splitting also happens after variable expansion.
You can use Perl to expand globs:
perl -lne 'unlink glob' -- list.txt
Normal glob needs whitespace backslashed, but you can switch to File::Glob for a different behaviour if it's more convenient to you:
perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt
To clarify, this won't work as expected if$line
contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
â wjandrea
3 hours ago
1
or just iterate over glob . . . less mess that way
â Sergiy Kolodyazhnyy
3 hours ago
Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
â Marco
3 hours ago
@Marco For a thorough solution, you'd have to escape every character in$IFS
.
â wjandrea
3 hours ago
1
No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
â choroba
3 hours ago
 |Â
show 3 more comments
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Pathname expansion happens after variable expansion, but only on unquoted parts of the command line.
rm -- $line # <- no double quotes to expand wildcards
This wouldn't work for filenames containing spaces, as word splitting also happens after variable expansion.
You can use Perl to expand globs:
perl -lne 'unlink glob' -- list.txt
Normal glob needs whitespace backslashed, but you can switch to File::Glob for a different behaviour if it's more convenient to you:
perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt
Pathname expansion happens after variable expansion, but only on unquoted parts of the command line.
rm -- $line # <- no double quotes to expand wildcards
This wouldn't work for filenames containing spaces, as word splitting also happens after variable expansion.
You can use Perl to expand globs:
perl -lne 'unlink glob' -- list.txt
Normal glob needs whitespace backslashed, but you can switch to File::Glob for a different behaviour if it's more convenient to you:
perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt
edited 3 hours ago
answered 3 hours ago
choroba
6,15911728
6,15911728
To clarify, this won't work as expected if$line
contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
â wjandrea
3 hours ago
1
or just iterate over glob . . . less mess that way
â Sergiy Kolodyazhnyy
3 hours ago
Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
â Marco
3 hours ago
@Marco For a thorough solution, you'd have to escape every character in$IFS
.
â wjandrea
3 hours ago
1
No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
â choroba
3 hours ago
 |Â
show 3 more comments
To clarify, this won't work as expected if$line
contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
â wjandrea
3 hours ago
1
or just iterate over glob . . . less mess that way
â Sergiy Kolodyazhnyy
3 hours ago
Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
â Marco
3 hours ago
@Marco For a thorough solution, you'd have to escape every character in$IFS
.
â wjandrea
3 hours ago
1
No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
â choroba
3 hours ago
To clarify, this won't work as expected if
$line
contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?â wjandrea
3 hours ago
To clarify, this won't work as expected if
$line
contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?â wjandrea
3 hours ago
1
1
or just iterate over glob . . . less mess that way
â Sergiy Kolodyazhnyy
3 hours ago
or just iterate over glob . . . less mess that way
â Sergiy Kolodyazhnyy
3 hours ago
Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
â Marco
3 hours ago
Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
â Marco
3 hours ago
@Marco For a thorough solution, you'd have to escape every character in
$IFS
.â wjandrea
3 hours ago
@Marco For a thorough solution, you'd have to escape every character in
$IFS
.â wjandrea
3 hours ago
1
1
No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
â choroba
3 hours ago
No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
â choroba
3 hours ago
 |Â
show 3 more comments
up vote
-1
down vote
Thanks all
perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt
work well upto now
New contributor
add a comment |Â
up vote
-1
down vote
Thanks all
perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt
work well upto now
New contributor
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
Thanks all
perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt
work well upto now
New contributor
Thanks all
perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt
work well upto now
New contributor
New contributor
answered 9 mins ago
Marco
165
165
New contributor
New contributor
add a comment |Â
add a comment |Â
Marco is a new contributor. Be nice, and check out our Code of Conduct.
Marco is a new contributor. Be nice, and check out our Code of Conduct.
Marco is a new contributor. Be nice, and check out our Code of Conduct.
Marco 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%2faskubuntu.com%2fquestions%2f1079014%2ftrying-to-script-deleting-files-from-wildcards-in-a-text-file%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