Results 1 to 5 of 5

Thread: Mass renaming files with no extension from cmd line

  1. #1
    Join Date
    Apr 2008
    Location
    Australia
    Beans
    228
    Distro
    Ubuntu Development Release

    [Solved] Mass renaming files with no extension from cmd line

    Hello,

    How do I rename files with no extension to .txt in the console? I would be doing about 150 at a time.

    Cheers,
    Last edited by kreggz; September 3rd, 2008 at 05:03 AM.

  2. #2
    Join Date
    Aug 2006
    Beans
    137

    Re: Mass renaming files with no extension from cmd line

    Code:
    for i in `ls`; do mv $i $i.txt; done
    Do you think 150 at a time does require a special power?

  3. #3
    Join Date
    Feb 2006
    Location
    Vancouver, BC, Canada
    Beans
    318

    Re: Mass renaming files with no extension from cmd line

    Here's one way to do it:
    Code:
    ruby -e 'ARGV.each {|f| next unless File.file?(f); next if f =~ /\./; File.rename(f, f+".txt") }' *
    This will rename only plain files (not directories, symlinks, or devices) whose names do not contain a "." character. It correctly handles filenames with spaces. It examines every file in the current working directory.



    The previous poster's code would rename every file and directory in the current working directory, except filenames with spaces, which it would choke on.
    If you followed that advice, you may now have a directory full of files with names like "file.txt.txt" and "movie.avi.txt". You could fix that problem by running this command:

    Code:
    rename 's/\.txt$//' *

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Mass renaming files with no extension from cmd line

    the most common way is to use find
    Code:
    find . -type f ! -name "*.*" -exec mv "{}" "{}"".txt"  \;

  5. #5
    Join Date
    Apr 2008
    Location
    Australia
    Beans
    228
    Distro
    Ubuntu Development Release

    Re: Mass renaming files with no extension from cmd line

    Thanks guys!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •