Trying to rename file extensions to match MIME filetype
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have to confess here. I've come to the realization of just how lost and over my head I am with this. I thought I had some little experience with scripts, but I apparently don't. When I look at the scripts you people have very kindly supplied here, it looks almost completely alien to me. I was hoping I would be able to pick up what was going on as I saw the examples, perhaps take a little from this script and a little from that script... but I find I wouldn't know what parts to take or how they fit together in the first place. There are a few scripts here that would work well enough if it weren't for small problems. And if I actually knew what I was doing, I would probably be able to fix or modify them to stop them from going wrong instead of saying "waaa, it don't work! fix it!" I don't want to be "that guy" mooching programs off people because he's too thick to do it himself.
I have to stick my nose in a book and learn the basic stuff about BASH scripting before I can truly appreciate the help you have given so far... or at least be able to take what you've given me and be able to modify it into something I can use in my situation. I don't even know if I can back out of this thread gracefully, all I know is I desperately need a clue and a few days to get intimate with a "Scripting For Dummies" book.
Don't worry - it still happens to me, and I've been writing shell scripts since the mid 1980s. The other thing that keeps happening is that bash keeps gaining more features... Keeping up can be surprising.
With your problem, break it down into two parts. One is to create a list of file names that are mis-identified.
The second problem is just changing the name of a list of files.
If you expect this is truly a one time deal - separate scripts work. I've even used awk to take a list of file names and create a file with lines of "mv firstname.ext firstname.newext"... Just so I can verify that the new name is right. (I think I would use bash now as it can do the substitutions itself)
IF that works, then just "sh file-of-mv-commands" and you are done.
Might keep the first script around for a "just in case" - but be sure to comment it so that you know what it is for and what it is doing when you need it next year...
I am with jpollard and others from the point of view, don't give up ... your just getting started
I found the best advice given to me was that a script does your command line things but in an order of your choosing (of course some times the order may be out of your control )
So looking at your scenario, I see it thus:
1. Find all png files under a certain path
2. Loop (while would be my choice) over each file getting the type information (file command with appropriate switch seems to be the go)
3. Test that file type is of the type being searched for to change (namely jpeg) ... here you have a multitude of choices so you could do a regular expression (using =~) or parameter substitution (as
suggested by others), probably ${var#*/} comes to mind
4. For true tests above use a simply move (mv) command to rename the file
Try following some of these and perform each task individually at the command line and progress from there
Here are some good links to help you on the learning bash way:
I am with jpollard and others from the point of view, don't give up ... your just getting started
I found the best advice given to me was that a script does your command line things but in an order of your choosing (of course some times the order may be out of your control )
So looking at your scenario, I see it thus:
1. Find all png files under a certain path
2. Loop (while would be my choice) over each file getting the type information (file command with appropriate switch seems to be the go)
3. Test that file type is of the type being searched for to change (namely jpeg) ... here you have a multitude of choices so you could do a regular expression (using =~) or parameter substitution (as
suggested by others), probably ${var#*/} comes to mind
I suspect a "file *" would do - but that assumes the directory has less than about 10,000 files... If the command gets a "too many parameters" type of error message, you do have to break it down more ("file *.jpeg" might cut it down), or use a loop for each file (a "find ... | while read filename ... type of thing).
Quote:
4. For true tests above use a simply move (mv) command to rename the file
Try following some of these and perform each task individually at the command line and progress from there
Here are some good links to help you on the learning bash way:
grep is the wrong tool - it will search the entire file for the string, and it is possible the search will find the byte sequence somewhere (besides being slow as it reads the entire file).
true , od then?
Code:
if [[ $(od -x -An -N2 $file) = " d8ff" ]]; then
echo "$file is a JPEG file"
fi
A JPEG file has multiple headers for the different types of JPEG segment.
for instance, a JPEG 2000 image starts with "jP", jp2, JP2. A compound JPEG 2000 image has jpm,
Then there is "jpx" for JPEG 2000 with extensions, jpm for compound image, mj2s/mjp2 for Motion JPEG 2000...
And there is an old format and more.
Take a look at /usr/share/misc/magic (which is interpreted by the "file" command) for the variations.
Better:
Code:
#!/bin/bash
if [ -f $1 ]; then
file $1 | grep 'JPEG' >/dev/null
if [[ $? = 0 ]]; then
echo is a JPEG file
else
echo NOT a JPEG file
fi
else
echo "no such file"
fi
grep will exit with a 0 on success, 1 on failure.
The surrounding if just checks that the file exists...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.