Search code examples
greprm

Remove a certain amount of files with a sequence of characters in a directory


I stopped a process to troubleshoot something. Now, I would like to start the process where it left off in CentOS 6.4.

This script I stopped runs a perl script in a loop to process all of the files in /dev/shm/split/. These files were split into many parts from a larger file. An example of how they are named are as follows:

file.txt.aa
file.txt.ab
file.txt.ac

...and so on.

I have identified that the script left off at file.txt.fy. So, I would like to remove all of the files in /dev/shm/split/ that are from file.txt.aa through file.txt.fy.

I tried to create a whitelist for the rm command by doing:

ls /dev/shm/split/ > whitelist
cat whitelist | egrep -v 'file.txt.[aa-fz]' | tee whitelist.tmp

This did not do what I had intended.

Please help me! Thank you!


Solution

  • The problem with your command is that you cannot match two characters with the square bracket pattern in bash. You should use something like that instead:

    ls file.txt.[a-e]? file.txt.f[a-y]
    

    Basically decompose your range into two ranges, the first will match .aa to .ez, and the second .fa to .fy (included).

    Note that I have used the ls command here. I always find it a good idea to first echo or ls the commands/files you're going to run when the operations you do are potentially destructive. When you're sure it produces the right output, go on and use rm instead of ls.