LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2016, 07:04 AM   #16
PunkTiger
LQ Newbie
 
Registered: Jul 2016
Distribution: Kubuntu 14.04 LTS, Peppermint 7
Posts: 8

Original Poster
Rep: Reputation: Disabled

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.

Last edited by PunkTiger; 07-05-2016 at 07:06 AM.
 
Old 07-05-2016, 07:11 AM   #17
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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...

Last edited by jpollard; 07-05-2016 at 07:18 AM.
 
Old 07-05-2016, 11:06 AM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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:

http://tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/TitleIndex

And as always, come back and ask when you get stuck ... we have all been there
 
Old 07-05-2016, 03:04 PM   #19
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by grail View Post
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:

http://tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/TitleIndex

And as always, come back and ask when you get stuck ... we have all been there
 
Old 07-05-2016, 04:44 PM   #20
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by jpollard View Post
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

Last edited by keefaz; 07-05-2016 at 04:46 PM.
 
Old 07-05-2016, 05:50 PM   #21
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Nope.

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...
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I edit filetype/mime associations for bash/sh/login shell? Ratamahatta Linux - General 6 03-12-2015 06:02 PM
[SOLVED] How do rename a file with multiple extensions manjeshjk Linux - Newbie 6 02-18-2013 04:13 PM
Script to rename files according to filetype SilversleevesX Linux - Newbie 14 03-01-2010 03:23 PM
mime-types/default actions for new file extensions? lumix Linux - Newbie 1 07-09-2008 03:31 PM
rename file extensions danimalz Linux - General 4 05-15-2006 02:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration