LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 01-15-2018, 07:29 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Renaming a set of files.


Two questions:
(a)
Is the string expression in the following two commands interpreted the same way whatever expression is equal to?
ls <expression>
and
mv <expression> <some_path>

If the question seems ambiguous I'm ready to supply additional information/examples.

(b)
Assume this is the contents of directory ~/.
Code:
bill@darkstar:~/work$ ls
IMSLP25713-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegro.pdf*
IMSLP25714-PMLP15396-Mozart_Pf_Concerto_24_K491_Larghetto.pdf*
IMSLP25715-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegretto.pdf*
IMSLP38122-PMLP15396-Piano_concerto_24.pdf*
Is there a command, let's call it command_1 that, after having being run, will make ls to have the following output?
Code:
 
bill@darkstar:~/work$ ls
IMSLP25713-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegro.txt*
IMSLP25714-PMLP15396-Mozart_Pf_Concerto_24_K491_Larghetto.txt*
IMSLP25715-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegretto.txt*
IMSLP38122-PMLP15396-Piano_concerto_24.txt*
bill@darkstar:~/work$
Of course there may be an infinite number of commands with this effect, but command_1 is only supposed to rename the four files. Also, command_1 may be a pipe, or a series of commands written one after the other in a script (I mean a program for the shell).

Last edited by stf92; 01-15-2018 at 07:32 PM.
 
Old 01-16-2018, 01:58 AM   #2
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,620
Blog Entries: 40

Rep: Reputation: Disabled
I have subscribed this thread, in case that someone can clarify a little on the actual question. But from what I understand right know, I deduce a potential problem with special characters and/or white space which might interfere with the ls and mv commands and alter the result of their execution.
 
Old 01-16-2018, 02:19 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
After a little thinking, I've seen my question has been ill-born. As I understand it, both mv and ls get there arguments interpreted by the shell before they can even be invoked by the OS. According to this there would be no difference between ls and mv in the sense I meant in post #1.

There are three chars, asterisk, question mark and period. I know the have a special for some parts of the OS/utilities, and that meaning is not the meaning they have when appearing in a regular expresion. I had read about the three of them but I now don't remember under what context.

Anyway, if I am right, the answer to question (a) in post #1 is yes. I should reread, if I could find the source, about those three characters and see under what circumstances they have that meaning.
 
Old 01-16-2018, 02:40 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,786

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
to a) you are right, arguments evaluated by the shell and the result is passed to the command (ls or mv or anything else). You can set -vx to check it:
Code:
user@ehost:/tmp$ ls -1       # this is the command typed
ls -1                        # this is the same (in this case)
+ ls --color=auto -1         # this is how shell interpreted and what will be really executed
2016-01                      # result
2016-07
2016-08
2016-09
2016-10
2016-11
2016-12
aaa
.....
to b) did you check the command rename already?
 
1 members found this post helpful.
Old 01-16-2018, 03:33 AM   #5
Stéphane Ascoët
Member
 
Registered: Feb 2004
Location: Fleury-les-Aubrais, 120 km south of Paris
Distribution: Devuan, Debian, Mandrake, Freeduc (the one I used to work on), Slackware, MacOS X
Posts: 251

Rep: Reputation: 49
Question Changing extension of several files

Quote:
Originally Posted by stf92 View Post
(b)
Assume this is the contents of directory ~/.
Code:
bill@darkstar:~/work$ ls
IMSLP25713-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegro.pdf*
IMSLP25714-PMLP15396-Mozart_Pf_Concerto_24_K491_Larghetto.pdf*
IMSLP25715-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegretto.pdf*
IMSLP38122-PMLP15396-Piano_concerto_24.pdf*
Is there a command, let's call it command_1 that, after having being run, will make ls to have the following output?
Code:
 
bill@darkstar:~/work$ ls
IMSLP25713-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegro.txt*
IMSLP25714-PMLP15396-Mozart_Pf_Concerto_24_K491_Larghetto.txt*
IMSLP25715-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegretto.txt*
IMSLP38122-PMLP15396-Piano_concerto_24.txt*
bill@darkstar:~/work$
Of course there may be an infinite number of commands with this effect, but command_1 is only supposed to rename the four files. Also, command_1 may be a pipe, or a series of commands written one after the other in a script (I mean a program for the shell).
Is there next to them some other files that mustn't be renamed*? Did you try a web search with terms like the ones I put in the title of this answer(wich in reality is a couple of questions!)*?
 
Old 01-16-2018, 04:09 AM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by pan64 View Post
to a) you are right, arguments evaluated by the shell and the result is passed to the command (ls or mv or anything else). You can set -vx to check it:
Code:
user@ehost:/tmp$ ls -1       # this is the command typed
ls -1                        # this is the same (in this case)
+ ls --color=auto -1         # this is how shell interpreted and what will be really executed
2016-01                      # result
2016-07
2016-08
2016-09
2016-10
2016-11
2016-12
aaa
.....
So, to come back to the triplet asterisk, interrogation mark, period as mentioned in post #3, it is the shell who gives those chars a special meaning, a meaning they don't have in regular expresions! E.g.
Code:
ls foo*
as a regexp that would list
foo
fooo
foooo
and so on. Am I right?
Quote:
to b) did you check the command rename already?
I always thought it was an alias or link to mv. I will make some experimentation with it, thanks.
 
Old 01-16-2018, 04:13 AM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by Stéphane Ascoët View Post
Is there next to them some other files that mustn't be renamed*? Did you try a web search with terms like the ones I put in the title of this answer(wich in reality is a couple of questions!)*?
In fact the renaming problem I need to solve is rather more complex than that. I used the simplest example not to complicate matters, so the "extension" thing seemed to me sufficeintly simple and clear.

No other files.
 
Old 01-16-2018, 04:53 AM   #8
Stéphane Ascoët
Member
 
Registered: Feb 2004
Location: Fleury-les-Aubrais, 120 km south of Paris
Distribution: Devuan, Debian, Mandrake, Freeduc (the one I used to work on), Slackware, MacOS X
Posts: 251

Rep: Reputation: 49
Post Renaming

Quote:
Originally Posted by stf92 View Post
and so on. Am I right?
Yes
Quote:
I always thought it was an alias or link to mv. I will make some experimentation with it, thanks.
Rename should do it, that makes me think that I should think about this command too instead of suffering with mv
 
Old 01-16-2018, 05:45 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,786

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
ls foo*
it is not regexp, but glob. A bit different. It is interpreted by the shell, bash. regexp is not handled that way. Unfortunately there are a lot of tools out there with different regexp implementations, so better to check the tool you want to use (sed/grep/awk/perl/python/java/whatever).
in shell, . is not a magic char (again it is not regexp but glob), only ? and * have special meaning.
 
1 members found this post helpful.
Old 01-16-2018, 07:45 AM   #10
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,486

Rep: Reputation: 2485Reputation: 2485Reputation: 2485Reputation: 2485Reputation: 2485Reputation: 2485Reputation: 2485Reputation: 2485Reputation: 2485Reputation: 2485Reputation: 2485
Quote:
ls
'IMSLP25713-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegro.pdf*'/
'IMSLP25714-PMLP15396-Mozart_Pf_Concerto_24_K491_Larghetto.pdf*'/
'IMSLP25715-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegretto.pdf*'/
'IMSLP38122-PMLP15396-Piano_concerto_24.pdf*'/
With the above files in the same directory and the above being the only files with a .pdf* extension the command below changes all to a .txt* extension. It will of course, change and file with the .pdf* extension to .txt*

Code:
rename .pdf* .txt* *.pdf*
Quote:
ls
'IMSLP25713-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegro.txt*'/
'IMSLP25714-PMLP15396-Mozart_Pf_Concerto_24_K491_Larghetto.txt*'/
'IMSLP25715-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegretto.txt*'/
'IMSLP38122-PMLP15396-Piano_concerto_24.txt*'/
If what you are trying to do is more complicated than that more specifics will be needed.

Last edited by yancek; 01-16-2018 at 07:49 AM.
 
1 members found this post helpful.
Old 01-16-2018, 03:32 PM   #11
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
INTENTIONALLY left blank.

Last edited by stf92; 01-16-2018 at 04:08 PM.
 
Old 01-16-2018, 04:03 PM   #12
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by yancek View Post
If what you are trying to do is more complicated than that more specifics will be needed.
Thanks a lot. This is what ls reports at present:
Code:
bill@darkstar:~/work$ ls
IMSLP25713-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegro.pdf*
IMSLP25714-PMLP15396-Mozart_Pf_Concerto_24_K491_Larghetto.pdf*
IMSLP25715-PMLP15396-Mozart_Pf_Concerto_24_K491_Allegretto.pdf*
IMSLP38122-PMLP15396-Piano_concerto_24.pdf*
bill@darkstar:~/work$
That is, all in the same directory and no other files in it.
I'd like to rename such that ls output will be:
Code:
bill@darkstar:~/work$ ls
Mozart_Pf_Concerto_24_K491_Allegro.pdf*
Mozart_Pf_Concerto_24_K491_Larghetto.pdf*
Mozart_Pf_Concerto_24_K491_Allegretto.pdf*
Piano_concerto_24.pdf*
bill@darkstar:~/work$
Of course I could do it by writing a program. After all, after the program has stored the filenames in a variable, then it can proceed to manipulate strings, I mean, pure string manipulation. Once it has the the original as well as the new names, I can then proceed to move files. But I suspect there is a much simpler and effective way.
 
Old 01-16-2018, 04:06 PM   #13
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by pan64 View Post
ls foo*
it is not regexp, but glob. A bit different. It is interpreted by the shell, bash. regexp is not handled that way. Unfortunately there are a lot of tools out there with different regexp implementations, so better to check the tool you want to use (sed/grep/awk/perl/python/java/whatever).
in shell, . is not a magic char (again it is not regexp but glob), only ? and * have special meaning.
Oh yes, you are right. I knew it but had forgotten it! I see these things are truely (globbing) essential for one to be able to do anything with unix/linux.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Renaming a set of files. stf92 Slackware 22 03-09-2014 04:39 AM
Help with renaming files EchoPhyber Linux - Newbie 4 03-26-2010 08:38 AM
Renaming files mashcaster Linux - General 3 05-08-2009 03:14 PM
Renaming files Viablade Linux - Newbie 11 10-10-2008 01:54 PM
renaming files starwarsfan982 Linux - Software 7 10-30-2006 02:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 03:16 PM.

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