2

I must be severely obtuse, because even after reading the man for rename and tons of example scripts that others have given on various message boards, I still can't even begin to understand what the syntax for the rename command actually means.

I'm trying to learn bash commands, and I realize how dangerous it is to simply copy & run commands from the internet without actually understanding what and more importantly why they "do what they do". So, can someone please help me understand what the terms for the rename command mean, so that I can built my own working & accurate command?

For specifics, I've downloaded over 40 youtube videos into the same directory, and they all end in -fgrhegrvbfv (each one has totally random characters after the -). I simply wish to remove the - and the 12 characters after it in every filename. Many thanks!

2

The first parameter is a Perl snippet that transforms the old name (in $_) to the new one, and the rest of the parameters are the files to operate on. In your case, you'd do this:

rename 's/-.{12}$//' *
  • Thanks Joseph. So I can tell that the "s/" and the "$//" are constant in every example that I've seen online, and I assume that the "*" at the end simply tells it to do it to all files within the current directory. And I also assume that the "{12}" equals the number of characters that I wish to remove. However, I'm still clueless about the "-." means, and when I ran your example, it only changed one single file. Oh, and I also noticed that some of my files have dashes "-" within those last 12 characters. I'm sorry for being so ignorant, but I've never studied programming (let alone perl). Thx – Turp May 4 '17 at 2:58
  • 2
    The s/ stuff is a regular expression replacement. The syntax is s/regex/replacement/. The regex I gave you, -.{12}$, means match a dash, then 12 of anything (technically, anything except newline characters), then the end. Since there's nothing where the replacement goes, it just removes the matching part without replacing it with anything. Are you sure it's exactly 12 characters after the dash in each one? – Joseph Sible May 4 '17 at 3:08
0

Here's one way to do it:

$ ls
file2-fdsa  file-asdf
$ prename -nv  's/(.*)-(.*)/$1/' *-*                  
file-asdf renamed as file
file2-fdsa renamed as file2

The way this works is that we have two "groups" of matched characters. So first (.*) will be whatever is before - and second (.*) will be whatever after -. We can refer to them via $1 for group 1 and $2 for group 2 ( which is not used).

The *-* tells the shell to take all files with - in it, and pass them as arguments to rename command. That part has nothing to do with rename itself, but with the shell. It just makes our life a little easier.

Now, the whole command can be read: "Take all files that have - in them, and for each filename, break it down into two groups, and rename that file only what's in the first group".

Note couple things:

  • the -n flag is used to only do dry-run. Remove that flag is you want to actually perform the action.
  • the -v flag is used to get verbose output of what prename is actually doing
  • prename is same as rename. There's just a bit of a mess with debian packages and what's being maintained. See this for more info. Note that some shells like ksh have their own built-in rename command which works differently from perl's rename.
  • That's perfect, now I understand! Now I can play around with it and figure out how to manipulate it. Ya know, I just bought the Unix and Linux Administrator's Handbook (4th Edition), and although it covers Perl expressions (on only 2 pages), it doesn't even mention "rename" at all! – Turp May 5 '17 at 2:06
  • @Turp Yeah, there's so many utilities in the Linux and Unix world that it's hard to cover every one of them, but rename is pretty useful and frequently recommended. I would recommend that you focus on studying regular expressions in general, and more specifically perl-style regex, since you're already dealing with it. It'll make your life much easier and you can deal with rename. Because that's what it's all about - it's regular expressions, and that syntax isn't specific to the rename command at all. – Sergiy Kolodyazhnyy May 5 '17 at 2:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.