1

I am trying to rename using "mv", because "rename" command doesn't work in my Mac terminal.

I have a bunch of files named

DTM001_ACGGT-TTAGGC.fq
DTM156_GGTTG-ACAGTG.fq

...etc

I wish to rename them to

DTM001.fq
DTM156.fq

I suppose the easier way is to remove the last 13 characters before the file extension?

I tried these links:

but none have worked for me, perhaps because I do not fully understand how to manipulate the answers for my specific case or some answers use "rename" command which I cannot access.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ems
  • 11
  • 1
  • 2

2 Answers2

4

The macOS Terminal is simply an interface to an interactive program called a shell. The default shell's name is bash.

What you are looking for is known as a shell script, or a bash script, to rename files.

The questions you referenced have the answer. To reiterate:

cd directory_with_the_files
for file in *.fq; do
    mv -vn "${file}" "${file%_*}.fq"
done

You can type this all in at the command line, or place it into a file and execute it with:

bash file_containing_the_commands

This will go through all .fq files in the current directory, renaming them to what you want. The -v option to mv simply means to print the rename as it happens (useful to know that it's doing something), and the -n flag means don't accidentally overwrite any files (in case you type something in wrong or come across duplicate numbers).

All the magic is happening in the ${file%_*}.fq, which says "remove everything after the first _ and add the .fq back". This is known as a "shell parameter expansion," which you can read more about in the Bash Reference Manual. It's somewhat obtusely worded, but here is the relevant bit to this particular use case:

${parameter%word}

The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the '%' case) deleted.

  • This is great, but, could you provide a specific example with my filenames? I get this message: `$file': not a valid identifier – ems May 28 '18 at 20:45
  • I tried changing $file for $DTM. I also tried changing $file for $DTM??? – ems May 28 '18 at 20:46
  • Sorry, I made a mistake. It should be `file`, not `$file` in the `for` construct. I've updated the answer. –  May 29 '18 at 21:02
  • It's really just an arbitrary name; you change it to `puppy` if you really wanted to. We call it `file` just because it helps make the program more understandable. When the `for` loop is going through the files in the directory, it tells the program to fill in a variable named `file` with the one it's working on. You pull the contents out of this variable using the `$name` or `${name}` syntax. It's a little confusing, but the mistake was that we tried to set the name of this variable to "whatever is in the variable `file`," which is not what we were intending to do. –  May 29 '18 at 21:24
3

The simplest way is to use rename - see instructions at the end for installation on a Mac.

So, in answer to your question, you can see what would happen if you replace (the command is actually s for "substitute") everything from the first underscore to the end of the filename with .fq:

rename --dry-run  's/_.*/.fq/'   *fq

'DTM001_ACGGT-TTAGGC.fq' would be renamed to 'DTM001.fq'
'DTM156_GGTTG-ACAGTG.fq' would be renamed to 'DTM156.fq'

If that looks good, remove the --dry-run and run it again for real.


You can use rename on your Mac, if you install it. By default, Apple doesn't ship a package manager with macOS. So, many folk use homebrew from the homebrew website.

If you have that, you can simply install rename with:

brew install rename

Then, you'll have a package manager and you can benefit for all sorts of lovely software including new, up-to-date versions of all the out-of-date, ancient versions of your favourite tools that Apple ships:

  • PHP
  • Perl
  • ImageMagick
  • GNU sed
  • GNU awk
  • GNU find
  • GNU Parallel
  • zeromq
  • htop
  • socat
  • sox
  • ffmpeg
  • youtube-dl
  • zenity
  • redis
  • feh
  • mosquitto
  • doxygen
  • pandoc etc.
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432