2

I'm trying to rename files in a directory based on the inode number. That seems to be the unique number that will keep the files (pictures) in the order in which they were taken. I want to do this as a complete directory vs individual files.

ls -i gives me the inode number for all files, but I need to pipe data that to rename or mv for all files.

Current Output: ls -li

46923838 -rw-r--r-- 1 me me 6937237 Jan 24 15:26 DSC_1167.JPG

46923839 -rw-r--r-- 1 me me 6692804 Jan 24 15:26 DSC_1168.JPG

46923840 -rw-r--r-- 1 me me 6735745 Jan 24 15:26 DSC_1169.JPG

46923841 -rw-r--r-- 1 me me 7709324 Jan 24 15:26 DSC_1170.JPG

46923842 -rw-r--r-- 1 me me 7645673 Jan 24 15:26 DSC_1171.JPG

Desired Results:

46923838 -rw-r--r-- 1 me me 6937237 Jan 24 15:26 46923838.JPG

46923839 -rw-r--r-- 1 me me 6692804 Jan 24 15:26 46923839.JPG

46923840 -rw-r--r-- 1 me me 6735745 Jan 24 15:26 46923840.JPG

46923841 -rw-r--r-- 1 me me 7709324 Jan 24 15:26 46923841.JPG

46923842 -rw-r--r-- 1 me me 7645673 Jan 24 15:26 46923842.JPG

Update: I need to take the "exif:DateTimeOriginal:" and send it as the new name of the file.

desktop 2016-10-22 # identify -verbose DSC_1140.JPG | grep exif:DateTimeOriginal: exif:DateTimeOriginal: 2016:10:22 16:29:33

desktop 2016-10-22 # identify -verbose DSC_1140.JPG | grep filename: filename: DSC_1140.JPG

  • 4
    There is nothing to suggest that the inode numbers will be allocated in the order that the pictures are taken. – icarus Jan 26 '17 at 7:04
  • Thanks for the feedback! I see the error in my thinking. How can I get the data from the Properties>Image>Date Taken field? "stat" for the pictures is telling me it has no birth date. Date Taken: 2016:10:12 11:13:26 me@desktop ~/2016-10-22 $ stat Pic_160 File: ‘Pic_160’ Size: 6924612 Blocks: 13528 IO Block: 4096 regular file Device: 801h/2049d Inode: 44171294 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ me) Gid: ( 1000/ me) Access: 2017-01-25 23:16:44.764049619 -0500 Modify: 2017-01-24 15:15:38.922054000 -0500 Change: 2017-01-25 23:16:24.944048721 -0500 Birth: – kentuckian6 Jan 26 '17 at 20:42
  • 1
    See also exiftool -d '%FT%T.jpg' '-Filename<DateTimeOriginal' ./*.JPG to rename them based on the time the picture was taken as stored in the EXIF tags. – Stéphane Chazelas Jan 27 '17 at 0:11
  • Thanks Stéphane Chazelas ! Exactly what I was looking for. – kentuckian6 Jan 27 '17 at 1:30
  • Sorry to anyone for updating/changing the question. Just showing the "progress" I was making. – kentuckian6 Jan 27 '17 at 1:31
2

Apart from hardlink(s), inode of a file is guaranteed to be unique, within the same filesystem. Iterating over the .JPG files in the current directory, and renaming (mv-ing) with help from stat to get the inode:

for i in *.JPG; do echo mv -- "$i" "$(stat -c '%i' "$i")"; done

echo will spit out the mv command that would be run. Remove echo for actual action:

for i in *.JPG; do mv -- "$i" "$(stat -c '%i' "$i")"; done

Also, if you want the final filenames to have the .JPG extension too:

for i in *.JPG; do mv -- "$i" "$(stat -c '%i' "$i").JPG"; done
  • 2
    Guaranteed to be unique, but not guaranteed to not also be in use as a filename. Just as a point. – Wildcard Jan 26 '17 at 23:58
  • Guaranteed to be unique except for hard links (in effect two hard links are the same files but they are different directory entries pointing to it. Above, you'd end up renaming all of them to the same file. Not really losing data though). – Stéphane Chazelas Jan 27 '17 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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