سؤال

I have a directory fill of files like the following:

1000.csv
1001.csv
aaa.csv
aac.csv

I want to remove the files like aaa.csv and aac.csv (basically anything that has [letters].csv, but keep the files that have [numbers].csv

Is there a way to do this in bash?

هل كانت مفيدة؟

المحلول

This should do what you ask but with the caveat that this is a very dangerous command so make sure you designate the right path!

find ./ -type f -name '*[a-z]*.csv' | xargs rm

"./" is where you should put the path to the directory of the files you want to remove. This will also remove any file that contains letters. Even 12h.csv.

نصائح أخرى

ls | grep [[:alpha:]*].csv | xargs rm

Should do what you want.

Edit:
ls will list the files in the directory, then pass that list to grep.
grep will take that list, and then filter out only the ones that begin with letters and then end in .csv, and will pass this new list to xargs.
xargs will take the list of files and pass it as an argument to rm.
rm will remove the files whose names it is passed.

The simplest way:

rm *[a-z]*.csv
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top