1

I'm trying to rename a lot of files in Kali Linux, nearly 16,000 of them. They're arranged in nearly 600 folders with random numbers for names, and the files in them are labelled as 0,1,2... (No file extn.) starting from 0 again in each new folder. I'm trying to convert the files into a .jpg format. The following code works only when I open the folder directory in terminal-

    find . -name '[0-9][0-9][0-9]' -exec sh -c 'mv $0 $0.jpg' {} \;
    find . -name '[0-9][0-9]' -exec sh -c 'mv $0 $0.jpg' {} \;
    find . -name '[0-9]' -exec sh -c 'mv $0 $0.jpg' {} \;

But to do this for each and every folder in the directory is going to be rather tedious. Any suggestions for a .sh executable script?

Edit: The extension has to be changed, the names, can remain as they are...

Athena
  • 155
  • 7
  • 1
    Write a script that loops for each folder found, then renames all the object in that folder, moves onto the next folder and does the same. Linux also has inbuilt rename functions you can access from the GUI. Select all the folders and hit rename. – Kapila Ramji Jan 12 '16 at 13:51
  • I did get the general idea of what I would have to do, but unfortunately, I've just started with Linux, and am not sure how to proceed... – Athena Jan 12 '16 at 13:58
  • 5
    ‘*convert the files into a .jpg format*’ Renaming a file does not change its format. – Biffen Jan 12 '16 at 14:07
  • To convert files to jpg from some other format on the command line, ImageMagick is one good tool. – Tom Zych Jan 12 '16 at 14:09
  • I think Kapila's suggestion to iterate through the directories is good. You may also be interested in `dirname` and `basename`. – Peter - Reinstate Monica Jan 12 '16 at 14:12
  • But my Windows partition now recognizes these renamed files, so...? – Athena Jan 12 '16 at 14:13
  • Possible duplicate of [Rename files and directories recursively under ubuntu /bash](http://stackoverflow.com/questions/15012631/rename-files-and-directories-recursively-under-ubuntu-bash) – Biffen Jan 12 '16 at 14:14
  • 2
    @Anaklusmosismyfavoriteweapon: Your Windows *thinks* those files are now JPEGs, but they still are the same files as before, with a different name. An application opening them and expecting JPEG format will a) fail, or b) ignore the name and read them as what they actually are. Either way, your files are NOT magically JPEGs now just by renaming. – DevSolar Jan 12 '16 at 14:14
  • As for finding files with digit-only names: `find . -regex '.*/[0-9]+'` – Biffen Jan 12 '16 at 14:17

2 Answers2

2

You can use a single find command:

find -type f -regextype posix-extended -regex '.*/[0-9]{1,3}' -exec mv -v {} {}.jpg \;
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • This worked perfectly... the files themselves seem to have originally been in a JPEG format, and Linux was reading them without a hitch as suggested by @DevSolar – Athena Jan 12 '16 at 14:29
  • Thanks, for the help, by the way. – Athena Jan 12 '16 at 15:48
0

If you don't mind installing another tool, then with rnm:

rnm -rs '/^\d{1,3}$/&.jpg/' -fo -dp -1 /path
  1. ^\d{1,3}$ is the match we are looking for and the & in the replace part refers to the matched portion in the filename.
  2. -fo : file only mode
  3. -dp : depth of dir (-1 means unlimited).
Jahid
  • 21,542
  • 10
  • 90
  • 108