I have a folder of Markdown files. The files use the following filename convention: [Time-based ID] [Title]. With this convention, you have filenames such as 202204101548 Banana bread or 202204101549 Banana cake.

Problem is, I want to delete a bunch of these files.

It would be too time consuming to delete them one by one, so that's out of the question. And that's all I can think of. I'm not knowledgeable enough about Linux to come up with a solution of my own. And attempting to gain the necessary knowledge is a bit out of my league right now. So, I'm asking you for your help.

I know that my request may sound a little lazy by my part, but I think that at the very least, some of you will have a nice problem to chew on.

The time-based ID of the files:

202106130712 202106200737 202106200757 202106200743 202106200845 202106130905 202106200728 202106200732 202106200752 202106200813 202106130847 202106200804 202106200815 202106200808 202106191046 202106191110 202106201003 202106191117 202106200724 202106090937 202106090918 202106101133 202106101134 202106110731 202106101135 202106110708 202106110713 202106110718 202106110751 202106110732 202106110756 202106110714 202106130714 202106201030 202106201032 202106130948 202106120959 202106101026 202106141032 202106130942 202106121006 202106121027 202106130914 202106130936 202106130940 202106130841 202106190907 202106101028 202106120908 202106130943 202106130839 202106101027 202106101024 202106190911 202106140742 202106140802 202106140735 202106101132 202106190947 202106190958 202106121022 202106130832 

Thanks in advance for any help you can provide me. I'll provide information or act on request.

3 Answers

Since all filenames start with 2021, try for a dry-run (from within the directory containing the files):

find -type f -name "2021*" 

and if satisfied with the output, (from within the directory containing the files) delete with:

find -type f -name "2021*" -delete 

To exactly delete using a list of "time-based IDs" from a file, try this for a dry-run (change file to the name of the file containing the list of IDs each on a new line exactly like the example in your question):

while IFS= read l; do find -type f -name "$l*" done < file 

If satisfied with the output, delete with:

while IFS= read l; do find -type f -name "$l*" -delete done < file 
3

Use wildcards.

mv *[0-9]* 

deletes files that have a digit.

mv *[12]* 

will delete all files with a 1 or a 2.

mv 2021[01][3-6]* 

will delete 202103* through 202106* and 202113* through 202116*

If you want to do a dry-run you can replace "rm" by "ls" and it will echo on screen what would be deleted. So ...

ls *[1-3][a-c]* 

will list all files containing the combinations 1a, 1b, 1c, 2a, 2b, 2c, 3a, 3b, 3c.

3

You can always use the rm command. Say I have 20 files named text01.txt, text02.txt, text03.txt etc. I can use:

rm -f text* 

The -f options means force (doesn't ask if you want to delete the file) and all the aforementioned files will get deleted. in your case try

rm -f *banana...(w/e) 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy