ems
ems

Reputation: 11

How can I remove the last n characters of filenames in a certain directory (in Mac terminal)- unix?

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.

Upvotes: 1

Views: 7784

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207455

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.

Upvotes: 3

user1902824
user1902824

Reputation:

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.

Upvotes: 5

Related Questions