2

So I have these files:

 Movies % ls -l *."pdf - yts"
-rw-r--r--@ 1   staff  1848890 Mar 18 00:11 Scanned Document 1.pdf - abc
-rw-r--r--@ 1   staff  1848890 Mar 18 00:11 Scanned Document 2.pdf - abc
-rw-r--r--@ 1   staff  1848890 Mar 18 00:11 Scanned Document 3.pdf - abc
-rw-r--r--@ 1   staff  1848890 Mar 18 00:11 Scanned Document 4.pdf - abc
 Movies %

and I need to rename the extensions from .pdf - abc to just .pdf.

I tried this:

mv *."pdf - yts" "*.pdf"

but got this error:

zsh: no matches found: *.pdf

Is there a one-liner that I can use here? Or is my command not formatted correctly?

nohillside
  • 100,768
  • 7
    Something doesn't quite add up: your ls -l *."pdf - yts" doesn't match the - abc suffixes you show on the files. – grg Jul 15 '23 at 06:07
  • And the error message just says *.pdf -- it doesn't mention - yts – Barmar Jul 16 '23 at 03:12
  • Since you are trying to move several files in one command, zmv would be a suitable. See man zshcontrib and serarch for zmv to see, how this is used. Note that you have to do an autoload zmv first, to make this command available. – user1934428 Jul 17 '23 at 07:25

3 Answers3

6

This solution uses zsh's zmv-

Navigate to the directory where these files exist and load zmv.

autoload zmv

Test the results of zmv by using the -n option.

zmv -n '(*).pdf - abc' '$1.pdf'

Remove the -n option if the results are what you desire.

fd0
  • 10,708
5

There is a utility called rename that you can install that will do just this.

rename "pdf - abc" "pdf" *."pdf - abc"

If it isn't on your system, you can install it with brew install rename. There are some packages where rename is called rename.ul

Note that your comments used the string "yts", but your example listing has "abc" in it.

  • 2
    On Ventura at least, rename is not a standard macOS included CLI tool, nor is rename.ul, so your qualification of 'most systems' doesn't hold true here. You need to update your answer to point the OP and other readers at a source of the executable, such as from brew (e.g. brew install rename). Note, it is a Perl script, so other infrastructure would be prerequisite, but brew would handle that. – Andy Griffiths Jul 15 '23 at 13:57
  • @AndyGriffiths - thanks for the comments, I updated my answer to show where to get package. I've had it on my system for a long time and forgot it wasn't standard. – NuclearFission Jul 16 '23 at 15:10
  • Ty... note that rename.ul seems to be specific to Debian systems and derivatives such as Ubuntu, specifically it indicates it comes from the util-linux package. So it is unlikely to be encountered in the macOS universe. See https://unix.stackexchange.com/a/444524/7952 – Andy Griffiths Jul 16 '23 at 15:38
  • I think your suggestion of using rename is a great answer and as a side note brew is not a standard macOS included CLI tool. It's a third party package manager. – fd0 Jul 16 '23 at 16:55
4

mv -h (help) shows the usage of mv:

usage: mv [-f | -i | -n] [-hv] source target
       mv [-f | -i | -n] [-v] source ... directory

This shows mv takes one source and target, or one or more sources and a target directory.

If your command were to succeed, it would be equivalent to

mv Scanned Document 1."pdf - yts" Scanned Document 2."pdf - yts" Scanned Document 3."pdf - yts" Scanned Document 4."pdf - yts" "*.pdf"

This is trying to move the 4 documents to the directory "*.pdf", which does not exist, hence the error.


To rename multiple files to multiple targets, you can use a for loop.

for file in *."pdf - abc"; do mv "$file" "${file/ - abc/}"; done
  • for file in *."pdf - abc"; do …; done runs with $file variable set to each file resulting from the glob *."pdf - abc".
  • ${file/ - abc/} finds - abc in the $file and replaces it with , removing it.

You also don't need to use the command line for this.

grg
  • 201,078
  • The error message is coming from zsh, not mv. – Barmar Jul 16 '23 at 03:13
  • Why was this answer accepted? It has nothing to do with the error message. – Barmar Jul 16 '23 at 14:11
  • @Barmar I didn’t say the error message was produced by mv, but rather that mv was being used incorrectly. – grg Jul 16 '23 at 16:10
  • That's true, but fixing that won't solve the problem because it's happening in the shell before mv runs. This answer is only useful after they solve the wildcard problem. – Barmar Jul 17 '23 at 01:31