Nasser Nasser - 4 months ago 14
Bash Question

bulk file renaming in bash, to remove name with spaces, leaving trailing digits

Can a bash/shell expert help me in this? Each time I use PDF to split large pdf file (say its name is

X.pdf
) into separate pages, where each page is one pdf file, it creates files with this pattern

"X 1.pdf"
"X 2.pdf"
"X 3.pdf" etc...


The file name "X" above is the original file name, which can be anything. It then adds one space after the name, then the page number. Page numbers always start from 1 and up to how many pages. There is no option in adobe PDF to change this.

I need to run a shell command to simply remove/strip out all the "X " part, and just leave the digits, like this

1.pdf
2.pdf
3.pdf
....
100.pdf ...etc..


Not being good in pattern matching, not sure what regular expression I need.

I know I need something like

for i in *.pdf; do mv "$i$" ........; done


And it is the
.......
part I do not know how to do.

This only needs to run on Linux/Unix system.

Answer

Use sed..

for i in *.pdf; do mv "$i" $(sed 's/.*[[:blank:]]//' <<< "$i");  done

And it would be simple through rename

rename 's/.*\s//' *.pdf
Comments