I am need to rename the below files. I only want to remove .job-209675 from the filenames that begin with SOA but keeping the original filenames intact as there are other files in this directory

for example
Before: ./SOA_990377897NW0001_1557340549734.xml.job-2074645
After: ./SOA_990377897NW0001_1557340549734.xml

  • Also consider Larry Wall's rename command. Beware there is more than one command named rename (it depends on, so check which is installed). – ctrl-alt-delor May 27 at 21:41

If a variable, filename, contains one of your files' name, then ${filename%.job-*} would be the name without the bit after the last dot.

To loop over the filenames, renaming each one in turn,

for filename in SOA_*.job-*; do
    echo mv -i "$filename" "${filename%.job-*}"

The echo is there for protection. You should run the loop once and inspect the output, then remove the echo if you think it's correct. The -i option to mv causes it to ask for confirmation before overwriting an existing file.

This assumes that all relevant files (and nothing else) are matched by the SOA_*.job-* pattern.

  • Thank you for the quick response I will try the above, this will require to be placed in script correct? Then ran – Jack May 27 at 16:52
  • 1
    @Jack You could place it in a script, but it so short that you should be able to paste it into a terminal. The important bit is that the current working directory is the directory where you have the files. – Kusalananda May 27 at 17:01
  • @Jack Thanks for thanking me (in the now removed edit to the question). Please consider "accepting" the answer. This is the best way to show gratitude on this site. Accepting an answer not only marks the question as resolved, but also signals to future readers that the accepted answer actually solved the issue. More information about this is available here: unix.stackexchange.com/help/someone-answers – Kusalananda May 27 at 18:51

Your Answer

