Find the answer to your Linux question:
Results 1 to 4 of 4
I've found info on batch renaming ( http://www.linuxforums.org/forum/lin...ing-files.html , Batch File Rename with awk and sed - Bash - Snipplr ) but I'm wondering how I'd go about having the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Location
    Melbourne, Victoria, Australia.
    Posts
    11

    Question Batch rename files to their MD5 (or SHA1) sum with BASH.


    I've found info on batch renaming (http://www.linuxforums.org/forum/lin...ing-files.html, Batch File Rename with awk and sed - Bash - Snipplr) but I'm wondering how I'd go about having the files named to their respective checksums?

  2. #2
    Just Joined! AdmiralBeotch's Avatar
    Join Date
    Feb 2009
    Location
    Las Vegas, NV
    Posts
    17
    Here ya go... Here's the command:

    Code:
    md5sum * | sed 's/^\(\w*\)\s*\(.*\)/\2 \1/' | while read LINE; do mv $LINE; done

    Here's what it does:
    Code:
    echo $RANDOM > file1.txt
    $ echo $RANDOM > file2.txt
    $ echo $RANDOM > file3.txt
    $ echo $RANDOM > file4.txt
    $ echo $RANDOM > file5.txt
    $ echo $RANDOM > file6.txt
    $ echo $RANDOM > file7.txt
    
    
    $ls
    total 36K
    drwxr-xr-x  2 admiral admiral 4.0K Feb  1 02:27 .
    drwxr-xr-x  8 admiral admiral 4.0K Feb  1 02:28 ..
    -rw-r--r--  1 admiral admiral    6 Feb  1 02:27 file1.txt
    -rw-r--r--  1 admiral admiral    5 Feb  1 02:27 file2.txt
    -rw-r--r--  1 admiral admiral    6 Feb  1 02:27 file3.txt
    -rw-r--r--  1 admiral admiral    6 Feb  1 02:27 file4.txt
    -rw-r--r--  1 admiral admiral    5 Feb  1 02:27 file5.txt
    -rw-r--r--  1 admiral admiral    5 Feb  1 02:27 file6.txt
    -rw-r--r--  1 admiral admiral    6 Feb  1 02:27 file7.txt
    
    $ md5sum * | sed 's/^\(\w*\)\s*\(.*\)/\2 \1/' | while read LINE; do mv $LINE; done
    $ ls
    total 36K
    drwxr-xr-x  2 admiral admiral 4.0K Feb  1 02:42 .
    drwxr-xr-x  8 admiral admiral 4.0K Feb  1 02:28 ..
    -rw-r--r--  1 admiral admiral    5 Feb  1 02:27 0e0c00262f7cc669cf69a927825a18e9
    -rw-r--r--  1 admiral admiral    6 Feb  1 02:27 5043348975568be5f9279b601a132078
    -rw-r--r--  1 admiral admiral    5 Feb  1 02:27 7a0c8deb59bc898fb7f4395ccb4af164
    -rw-r--r--  1 admiral admiral    5 Feb  1 02:27 85b30d1e0a33b316fe29c8cce8e4cef0
    -rw-r--r--  1 admiral admiral    6 Feb  1 02:27 85d12147f174d39429f63e7cf77b8d07
    -rw-r--r--  1 admiral admiral    6 Feb  1 02:27 ace7931e8090a82547d00859545015c2
    -rw-r--r--  1 admiral admiral    6 Feb  1 02:27 d9f2495a9f490f0fe60f599a5abe99a8

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Location
    Melbourne, Victoria, Australia.
    Posts
    11
    Thanks. I'll try that.

  4. #4
    Just Joined!
    Join Date
    Nov 2010
    Posts
    1
    What about renaming files but retaining their extension? I am trying to do that, but I am new to bash and regular expressions.

Posting Permissions

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