Remove everything except csv file Bash ScriptWhy does `rm -f !(/var/www/wp)` leave files behind in /var/www?Use & (ampersand) in single line bash loophow to remove all double quotes from csv except specific fieldSyntax Error: unexpected end of file — Bash scriptSyntax Error: unexpected end of file — Bash scriptWhy is this simple bash script throwing an error on if/then/else?CSV copy script error reporting in bashsyntax error near unexpected token 'else' and possible incorrect indentationsSyntax error near unexpected token `done'Syntax error near unexpected token 'else'Error when run multiple commands in one line in bash
Cuban Primes
Are there microwaves to heat baby food at Brussels airport?
c++ conditional uni-directional iterator
tikz drawing rectangle discretized with triangle lattices and its centroids
Polynomial division: Is this trick obvious?
Does this "yield your space to an ally" rule my 3.5 group uses appear anywhere in the official rules?
Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?
How to not get blinded by an attack at dawn
Is the seat-belt sign activation when a pilot goes to the lavatory standard procedure?
Is there any good reason to write "it is easy to see"?
Why would company (decision makers) wait for someone to retire, rather than lay them off, when their role is no longer needed?
When did game consoles begin including FPUs?
History of the Frobenius Endomorphism?
Will there be more tax deductions if I put the house completely under my name, versus doing a joint ownership?
How to check if comma list is empty?
Should I communicate in my applications that I'm unemployed out of choice rather than because nobody will have me?
What is this weird d12 for?
Understanding Python syntax in lists vs series
Windows 10 lock screen - display my own random images
What do the "optional" resistor and capacitor do in this circuit?
Geometric inspiration behind Hal's Wolf
Were any toxic metals used in the International Space Station?
Will consteval functions allow template parameters dependent on function arguments?
Why did Varys remove his rings?
Remove everything except csv file Bash Script
Why does `rm -f !(/var/www/wp)` leave files behind in /var/www?Use & (ampersand) in single line bash loophow to remove all double quotes from csv except specific fieldSyntax Error: unexpected end of file — Bash scriptSyntax Error: unexpected end of file — Bash scriptWhy is this simple bash script throwing an error on if/then/else?CSV copy script error reporting in bashsyntax error near unexpected token 'else' and possible incorrect indentationsSyntax error near unexpected token `done'Syntax error near unexpected token 'else'Error when run multiple commands in one line in bash
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I would like to delete everything from a folder except csv files
I am trying with a bash script and I am getting this error:
syntax error near unexpected token `('
This is my script :
PATH=/tmp/
run_spark_local
rm -v !($PATH*.csv)
cp -r $PATH /data/logs/
I have also tried
rm -v !("$PATH*.csv")
linux bash wildcards rm
New contributor
|
show 1 more comment
I would like to delete everything from a folder except csv files
I am trying with a bash script and I am getting this error:
syntax error near unexpected token `('
This is my script :
PATH=/tmp/
run_spark_local
rm -v !($PATH*.csv)
cp -r $PATH /data/logs/
I have also tried
rm -v !("$PATH*.csv")
linux bash wildcards rm
New contributor
What isrun_spark_local
?
– Jesse_b
May 10 at 15:06
1
Turn on extglob-shopt -s extglob
.
– fd0
May 10 at 15:11
2
Clearing out/tmp
could have adverse consequences on other programs running on the system.
– Stephen Kitt
May 10 at 15:29
@Jesse_b a function to run my spark app
– MobZSPARK
May 10 at 16:06
2
This is why you don't use ALLCAPS variable names in your code.
– glenn jackman
May 10 at 18:53
|
show 1 more comment
I would like to delete everything from a folder except csv files
I am trying with a bash script and I am getting this error:
syntax error near unexpected token `('
This is my script :
PATH=/tmp/
run_spark_local
rm -v !($PATH*.csv)
cp -r $PATH /data/logs/
I have also tried
rm -v !("$PATH*.csv")
linux bash wildcards rm
New contributor
I would like to delete everything from a folder except csv files
I am trying with a bash script and I am getting this error:
syntax error near unexpected token `('
This is my script :
PATH=/tmp/
run_spark_local
rm -v !($PATH*.csv)
cp -r $PATH /data/logs/
I have also tried
rm -v !("$PATH*.csv")
linux bash wildcards rm
linux bash wildcards rm
New contributor
New contributor
edited May 10 at 15:23
Jeff Schaller♦
45.7k1165149
45.7k1165149
New contributor
asked May 10 at 15:03
MobZSPARKMobZSPARK
182
182
New contributor
New contributor
What isrun_spark_local
?
– Jesse_b
May 10 at 15:06
1
Turn on extglob-shopt -s extglob
.
– fd0
May 10 at 15:11
2
Clearing out/tmp
could have adverse consequences on other programs running on the system.
– Stephen Kitt
May 10 at 15:29
@Jesse_b a function to run my spark app
– MobZSPARK
May 10 at 16:06
2
This is why you don't use ALLCAPS variable names in your code.
– glenn jackman
May 10 at 18:53
|
show 1 more comment
What isrun_spark_local
?
– Jesse_b
May 10 at 15:06
1
Turn on extglob-shopt -s extglob
.
– fd0
May 10 at 15:11
2
Clearing out/tmp
could have adverse consequences on other programs running on the system.
– Stephen Kitt
May 10 at 15:29
@Jesse_b a function to run my spark app
– MobZSPARK
May 10 at 16:06
2
This is why you don't use ALLCAPS variable names in your code.
– glenn jackman
May 10 at 18:53
What is
run_spark_local
?– Jesse_b
May 10 at 15:06
What is
run_spark_local
?– Jesse_b
May 10 at 15:06
1
1
Turn on extglob-
shopt -s extglob
.– fd0
May 10 at 15:11
Turn on extglob-
shopt -s extglob
.– fd0
May 10 at 15:11
2
2
Clearing out
/tmp
could have adverse consequences on other programs running on the system.– Stephen Kitt
May 10 at 15:29
Clearing out
/tmp
could have adverse consequences on other programs running on the system.– Stephen Kitt
May 10 at 15:29
@Jesse_b a function to run my spark app
– MobZSPARK
May 10 at 16:06
@Jesse_b a function to run my spark app
– MobZSPARK
May 10 at 16:06
2
2
This is why you don't use ALLCAPS variable names in your code.
– glenn jackman
May 10 at 18:53
This is why you don't use ALLCAPS variable names in your code.
– glenn jackman
May 10 at 18:53
|
show 1 more comment
3 Answers
3
active
oldest
votes
You should avoid setting the PATH
variable. This is used by your shell to find valid commands, setting it to /tmp/
is going to prevent the script from being able to find the rm
and cp
commands altogether.
You can accomplish what you want with the following find
command:
find /tmp -not -name '*.csv' -not -path /tmp -exec rm -vr ;
Note: this will delete any subdirectories under /tmp as well. If you do not want this you must change to:
find /tmp -not -name '*.csv' -type f -exec rm -v ;
Another note: This will still recurse into the subdirectories and delete the files in them. If you do not want this you can use the maxdepth
argument:
find /tmp -not -name '*.csv' -maxdepth 1 -type f -exec rm -v ;
Extra note: I would never run a find ... -exec
command that you find online without verifying it will do what you need it to do first. You should run:
find /tmp -not -name '*.csv' -not -path /tmp
And verify it is finding only the files you want before adding the -exec rm -vr ;
bit.
find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ; This Worked well for me Thank you What if i want to do the same thing but in the hdfs . does this work ? hdfs dfs find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ;
– MobZSPARK
May 10 at 15:47
add a comment |
Please do not set $PATH it is a environment variable.
For BASH, so long as the extglob shell option is enabled (that is the default for a lot of linux distributions), it is just:
rm !(*.csv)
With a folder path it will be something like...
rm yourfolder/!(*.csv)
If you think extglob is not enabled in your environment, just do this:
shopt -s extglob
add a comment |
Instead of deleting everything else in /tmp
, I’d recommend only copying the files you’re interested in:
cp /tmp/*.csv /data/logs/
or even
mv /tmp/*.csv /data/logs/
if you don’t need to keep them in /tmp
.
This assumes that all the files you’re interested in are directly in /tmp
; your use of rm
suggests that they are.
Since /tmp
is a shared temporary directory, it could contain other files which other running processes expect to find there, and deleting everything in /tmp
apart from the CSV files could have adverse consequences. As others have mentioned, you shouldn’t change PATH
either since your shell uses that to find the commands you’re using.
or just moving for somewhere....
– Luciano Andress Martini
May 10 at 15:32
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
MobZSPARK 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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f518256%2fremove-everything-except-csv-file-bash-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should avoid setting the PATH
variable. This is used by your shell to find valid commands, setting it to /tmp/
is going to prevent the script from being able to find the rm
and cp
commands altogether.
You can accomplish what you want with the following find
command:
find /tmp -not -name '*.csv' -not -path /tmp -exec rm -vr ;
Note: this will delete any subdirectories under /tmp as well. If you do not want this you must change to:
find /tmp -not -name '*.csv' -type f -exec rm -v ;
Another note: This will still recurse into the subdirectories and delete the files in them. If you do not want this you can use the maxdepth
argument:
find /tmp -not -name '*.csv' -maxdepth 1 -type f -exec rm -v ;
Extra note: I would never run a find ... -exec
command that you find online without verifying it will do what you need it to do first. You should run:
find /tmp -not -name '*.csv' -not -path /tmp
And verify it is finding only the files you want before adding the -exec rm -vr ;
bit.
find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ; This Worked well for me Thank you What if i want to do the same thing but in the hdfs . does this work ? hdfs dfs find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ;
– MobZSPARK
May 10 at 15:47
add a comment |
You should avoid setting the PATH
variable. This is used by your shell to find valid commands, setting it to /tmp/
is going to prevent the script from being able to find the rm
and cp
commands altogether.
You can accomplish what you want with the following find
command:
find /tmp -not -name '*.csv' -not -path /tmp -exec rm -vr ;
Note: this will delete any subdirectories under /tmp as well. If you do not want this you must change to:
find /tmp -not -name '*.csv' -type f -exec rm -v ;
Another note: This will still recurse into the subdirectories and delete the files in them. If you do not want this you can use the maxdepth
argument:
find /tmp -not -name '*.csv' -maxdepth 1 -type f -exec rm -v ;
Extra note: I would never run a find ... -exec
command that you find online without verifying it will do what you need it to do first. You should run:
find /tmp -not -name '*.csv' -not -path /tmp
And verify it is finding only the files you want before adding the -exec rm -vr ;
bit.
find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ; This Worked well for me Thank you What if i want to do the same thing but in the hdfs . does this work ? hdfs dfs find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ;
– MobZSPARK
May 10 at 15:47
add a comment |
You should avoid setting the PATH
variable. This is used by your shell to find valid commands, setting it to /tmp/
is going to prevent the script from being able to find the rm
and cp
commands altogether.
You can accomplish what you want with the following find
command:
find /tmp -not -name '*.csv' -not -path /tmp -exec rm -vr ;
Note: this will delete any subdirectories under /tmp as well. If you do not want this you must change to:
find /tmp -not -name '*.csv' -type f -exec rm -v ;
Another note: This will still recurse into the subdirectories and delete the files in them. If you do not want this you can use the maxdepth
argument:
find /tmp -not -name '*.csv' -maxdepth 1 -type f -exec rm -v ;
Extra note: I would never run a find ... -exec
command that you find online without verifying it will do what you need it to do first. You should run:
find /tmp -not -name '*.csv' -not -path /tmp
And verify it is finding only the files you want before adding the -exec rm -vr ;
bit.
You should avoid setting the PATH
variable. This is used by your shell to find valid commands, setting it to /tmp/
is going to prevent the script from being able to find the rm
and cp
commands altogether.
You can accomplish what you want with the following find
command:
find /tmp -not -name '*.csv' -not -path /tmp -exec rm -vr ;
Note: this will delete any subdirectories under /tmp as well. If you do not want this you must change to:
find /tmp -not -name '*.csv' -type f -exec rm -v ;
Another note: This will still recurse into the subdirectories and delete the files in them. If you do not want this you can use the maxdepth
argument:
find /tmp -not -name '*.csv' -maxdepth 1 -type f -exec rm -v ;
Extra note: I would never run a find ... -exec
command that you find online without verifying it will do what you need it to do first. You should run:
find /tmp -not -name '*.csv' -not -path /tmp
And verify it is finding only the files you want before adding the -exec rm -vr ;
bit.
edited May 10 at 15:32
answered May 10 at 15:11
Jesse_bJesse_b
15.2k33574
15.2k33574
find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ; This Worked well for me Thank you What if i want to do the same thing but in the hdfs . does this work ? hdfs dfs find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ;
– MobZSPARK
May 10 at 15:47
add a comment |
find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ; This Worked well for me Thank you What if i want to do the same thing but in the hdfs . does this work ? hdfs dfs find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ;
– MobZSPARK
May 10 at 15:47
find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ; This Worked well for me Thank you What if i want to do the same thing but in the hdfs . does this work ? hdfs dfs find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ;
– MobZSPARK
May 10 at 15:47
find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ; This Worked well for me Thank you What if i want to do the same thing but in the hdfs . does this work ? hdfs dfs find /tmp -not -name '.csv' -not -path /tmp -exec rm -vr ;
– MobZSPARK
May 10 at 15:47
add a comment |
Please do not set $PATH it is a environment variable.
For BASH, so long as the extglob shell option is enabled (that is the default for a lot of linux distributions), it is just:
rm !(*.csv)
With a folder path it will be something like...
rm yourfolder/!(*.csv)
If you think extglob is not enabled in your environment, just do this:
shopt -s extglob
add a comment |
Please do not set $PATH it is a environment variable.
For BASH, so long as the extglob shell option is enabled (that is the default for a lot of linux distributions), it is just:
rm !(*.csv)
With a folder path it will be something like...
rm yourfolder/!(*.csv)
If you think extglob is not enabled in your environment, just do this:
shopt -s extglob
add a comment |
Please do not set $PATH it is a environment variable.
For BASH, so long as the extglob shell option is enabled (that is the default for a lot of linux distributions), it is just:
rm !(*.csv)
With a folder path it will be something like...
rm yourfolder/!(*.csv)
If you think extglob is not enabled in your environment, just do this:
shopt -s extglob
Please do not set $PATH it is a environment variable.
For BASH, so long as the extglob shell option is enabled (that is the default for a lot of linux distributions), it is just:
rm !(*.csv)
With a folder path it will be something like...
rm yourfolder/!(*.csv)
If you think extglob is not enabled in your environment, just do this:
shopt -s extglob
edited May 10 at 20:27
answered May 10 at 15:26
Luciano Andress MartiniLuciano Andress Martini
4,3401238
4,3401238
add a comment |
add a comment |
Instead of deleting everything else in /tmp
, I’d recommend only copying the files you’re interested in:
cp /tmp/*.csv /data/logs/
or even
mv /tmp/*.csv /data/logs/
if you don’t need to keep them in /tmp
.
This assumes that all the files you’re interested in are directly in /tmp
; your use of rm
suggests that they are.
Since /tmp
is a shared temporary directory, it could contain other files which other running processes expect to find there, and deleting everything in /tmp
apart from the CSV files could have adverse consequences. As others have mentioned, you shouldn’t change PATH
either since your shell uses that to find the commands you’re using.
or just moving for somewhere....
– Luciano Andress Martini
May 10 at 15:32
add a comment |
Instead of deleting everything else in /tmp
, I’d recommend only copying the files you’re interested in:
cp /tmp/*.csv /data/logs/
or even
mv /tmp/*.csv /data/logs/
if you don’t need to keep them in /tmp
.
This assumes that all the files you’re interested in are directly in /tmp
; your use of rm
suggests that they are.
Since /tmp
is a shared temporary directory, it could contain other files which other running processes expect to find there, and deleting everything in /tmp
apart from the CSV files could have adverse consequences. As others have mentioned, you shouldn’t change PATH
either since your shell uses that to find the commands you’re using.
or just moving for somewhere....
– Luciano Andress Martini
May 10 at 15:32
add a comment |
Instead of deleting everything else in /tmp
, I’d recommend only copying the files you’re interested in:
cp /tmp/*.csv /data/logs/
or even
mv /tmp/*.csv /data/logs/
if you don’t need to keep them in /tmp
.
This assumes that all the files you’re interested in are directly in /tmp
; your use of rm
suggests that they are.
Since /tmp
is a shared temporary directory, it could contain other files which other running processes expect to find there, and deleting everything in /tmp
apart from the CSV files could have adverse consequences. As others have mentioned, you shouldn’t change PATH
either since your shell uses that to find the commands you’re using.
Instead of deleting everything else in /tmp
, I’d recommend only copying the files you’re interested in:
cp /tmp/*.csv /data/logs/
or even
mv /tmp/*.csv /data/logs/
if you don’t need to keep them in /tmp
.
This assumes that all the files you’re interested in are directly in /tmp
; your use of rm
suggests that they are.
Since /tmp
is a shared temporary directory, it could contain other files which other running processes expect to find there, and deleting everything in /tmp
apart from the CSV files could have adverse consequences. As others have mentioned, you shouldn’t change PATH
either since your shell uses that to find the commands you’re using.
edited May 10 at 15:34
answered May 10 at 15:31
Stephen KittStephen Kitt
186k26428509
186k26428509
or just moving for somewhere....
– Luciano Andress Martini
May 10 at 15:32
add a comment |
or just moving for somewhere....
– Luciano Andress Martini
May 10 at 15:32
or just moving for somewhere....
– Luciano Andress Martini
May 10 at 15:32
or just moving for somewhere....
– Luciano Andress Martini
May 10 at 15:32
add a comment |
MobZSPARK is a new contributor. Be nice, and check out our Code of Conduct.
MobZSPARK is a new contributor. Be nice, and check out our Code of Conduct.
MobZSPARK is a new contributor. Be nice, and check out our Code of Conduct.
MobZSPARK is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f518256%2fremove-everything-except-csv-file-bash-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is
run_spark_local
?– Jesse_b
May 10 at 15:06
1
Turn on extglob-
shopt -s extglob
.– fd0
May 10 at 15:11
2
Clearing out
/tmp
could have adverse consequences on other programs running on the system.– Stephen Kitt
May 10 at 15:29
@Jesse_b a function to run my spark app
– MobZSPARK
May 10 at 16:06
2
This is why you don't use ALLCAPS variable names in your code.
– glenn jackman
May 10 at 18:53