Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In the Windows Command Prompt you can do this kind of thing:

  • ren *.doc *.txt
  • copy my-precious-document.doc *-backup.doc

Is there a way to do this is the Linux (including OS X) world? It's a very strange thing to discover command line commands that worked better on DOS...

share|improve this question

3 Answers 3

Not really. In Windows, globbing (in Windows, only wildcards) is performed by the command; in *nix, globbing is done by the shell. Your first example would turn out something like this:

mv a.doc b.doc c.doc a.txt b.txt c.txt

Obviously, the utility has no clue what to do with this, and it shouldn't (ignoring things like mv assuming you're trying to move into a directory). To perform something like what you want to do, you need to use something like this:

find . -name "*.doc" -print0 | xargs -0 -I {} mv {} {}.txt
# Or...
find . -name "*.doc" -exec mv {} {}.txt \;

Conveniently, there is also a utility included with Perl that can be used for the purpose of renaming files, appropriately called rename.

rename 's/\.txt$//' *.doc
share|improve this answer
    
it is usually better to use the -exec option with find instead of piping it to xargs. xargs can choke when there is a space in the file name piped to it from find. –  MaQleod Nov 22 '11 at 2:37
2  
@MaQleod: -print0. –  Hello71 Nov 22 '11 at 2:54

Note that there are two versions of rename that ship with various distros.

The Perl rename is default in ubuntu and variants as answered by @Hello71

Other distributions may ship the util-linux-ng package where the syntax is:

rename <from> <to> <files>

To carry out the operating in the OP, the command would be

rename .doc .txt *.doc

This replaces '.doc' in the filename with '.txt' for any files ending in .doc

OSX doesn't ship with rename, but if you have Perl installed on OSX, you can just copy the rename script over to your OSX machine from a linux installation. The Perl version is far more flexible than util-linux (above). It should be in /usr/bin/rename on Ubuntu type distros. The script expects perl to be in /usr/bin/perl so you may need to edit it if it is elsewhere on OSX

share|improve this answer
    
Cool. I'm using OS X (should have specified), it doesn't look like there is a standard port of util-linux? (Or if there is one, I can't figure it out.) –  Steve Bennett Nov 22 '11 at 4:46
    
I don't think there is a standard port, though people have resorted to compiling rename under OSX before. However, there are OSX gui apps that can do mass renames with this type of flexibility –  Paul Nov 22 '11 at 4:56
    
Yeah, but command line tools are useful –  Steve Bennett Nov 22 '11 at 6:34
    
Oh for sure. The Perl rename command is literally perl script, so if you have Perl installed on OSX, you can just copy the rename script over to your OSX machine. The Perl version is far more flexible than util-linux. It should be in /usr/bin/rename. It expects perl to be in /usr/bin/perl so you may need to edit it if it is elsewhere on OSX. –  Paul Nov 22 '11 at 7:18
    
use macports, search for "rename", there are a few options to choose from; you want p5-file-rename (or renameutils, unrelated but perhaps also useful). After installing macports (if you don't already have it) => sudo port install p5-file-rename.. A symlink should be create to invoke the script as rename. (ps: indeed it would be strange to discover command line utils that worked better under dos. But it's not the case here, either.) –  michael_n Jun 2 '12 at 8:13

for f in *.doc; do cp "${f}" "${f//.doc/}-backup.doc"; done will work. This would append -backup.doc to the end of each file. ${f//.doc/} will remove all the .docs from the original filename, so you don't end up with .doc. Personally, though, when I make a backup, I just add .bkup to the filename, so if I wanted to backup all the .doc's in a directory to a separate backup folder, I'd navigate to the folder I want to cp from and do something like this for f in *.doc; do cp "${f}" "~/backups/${f}.bkup"; done

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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