Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

change a character in filename to another

New to Debian (Or Linux in general)? Ask your questions here!
Message
Author
User avatar
dilberts_left_nut
Administrator
Administrator
Posts: 5346
Joined: 2009-10-05 07:54
Location: enzed
Has thanked: 13 times
Been thanked: 66 times

Re: change a character in filename to another

#16 Post by dilberts_left_nut »

Agvaniot wrote:im trying to take a dotted filiename "a.dotted.filename" and replace the "." with space
for example : "a dotted filename"
ive tried to come up with bunch of codes:

Code: Select all

dottedfilename=$1
${dottedfilename##*.*/' '/} | clipit

Code: Select all

sed -e  's/./ /g'` a.dotted.filename 
For the sed part:

A . is a wildcard that matches any character, you need to escape it with a \ to match an actual .
AdrianTM wrote:There's no hacker in my grandma...

ruffwoof
Posts: 298
Joined: 2016-08-20 21:00

Re: change a character in filename to another

#17 Post by ruffwoof »

dasein wrote:Speaking of people who have a long history of giving bad answers...
ruffwoof wrote:creating a filename of ;rm -rf / is blocked for obvious reasons.
Certainly not for the reason you seem to imply. The utterly innocuous ls -l / is "blocked" for exactly the same "reason"

I'll say it again, people who don't know what they are talking about should refrain from giving bad answers. It's profoundly unclear to me how such a notion could be perceived as in any way controversial. And it's mind-boggling to see anyone, much less a staff member, defend the practice.

This isn't funny. Really. No one comes here to be misinformed or misled. And folks who repeatedly give technically inaccurate answers out of ignorance are no better than (actually no different from) folks who would give bad answers just for lulz.

It's truly sad and pathetic that FDN's devotion to "free speech" supersedes their devotion to technical accuracy. It's an egregious and corrosive philosophy.

For instance, there is not a single accurate statement in this quote from GarryR
How ever if they do that, and want to open the file from the command line
they won't be able to.

You can not have file names with spaces on a linux system...
I will note once again that the initial problem looks like homework. There are exactly two characters that absolutely, positively cannot be used in a *nix filename. Determining which characters these are is left as an exercise for the curious.
1 thread, 3 posts, 3 incorrect assumptions, 3 insults ... zero positive contribution.

To clarify, forward slash and null are disallowed in filenames, the obvious part for the forward slash is that that is reserved for pathnames. It is permissible for instance to create a filename of ;rm -rf * ... which wouldn't be good practice as a simple attempt to remove that file using rm ;rm -rf * would be interpreted as two separate command sequences of rm (which would simply fail with a warning message) and rm -rf * which would recursively delete the * wildcard expansion.

It was clarified that the question wasn't homework yet you continue to proclaim that to be a lie.

A assumption that visitors come here purely for accurate answers and that no one should respond to threads without 100% certainty of accuracy ... is simply wrong. It's a Debian Users forum within which participants can commonly share and learn across all ability/skill levels. Otherwise a board comprised of 'go off and do a internet search' replies could be condensed to a single front end web page. Wrong answers/questions ... followed by discussion and a correct conclusion aids in developing knowledge, and serves to build a reference for those who might later be pondering a similar question. Fear of asking the wrong question or potentially giving a wrong answer suppresses development of knowledge. Others have come away with something positive; You have wasted your time, contributed nothing positive, learnt nothing, excepting perhaps if you have the sense to take it on board to review your own philosophy and assumptions.

User avatar
RU55EL
Posts: 546
Joined: 2014-04-07 03:42
Location: /home/russel

Re: change a character in filename to another

#18 Post by RU55EL »

Agvaniot wrote:im trying to take a dotted filiename "a.dotted.filename" and replace the "." with space
for example : "a dotted filename"
[...]
From the Gnome desktop:
Simple: select the file name, hit F2 to rename the file, hit the right arrow key to move to the end of the file name, while holding the control key hit the back arrow key (that should place the cursor between the "f" and the "." in "a.dotted.filename, stop pressing the control key and hit backspace once, then hit the space bar once. You have now changed one of the dots to a space. Simply repeat this for the other dot, and you are done. Hit enter to complete the file name change.

Single quotes also work for creating a file name with spaces in Debian:

Code: Select all

touch 'a dotted filename'
[Edit] Changed backspace key to back arrow key...
Last edited by RU55EL on 2017-08-16 23:01, edited 1 time in total.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: change a character in filename to another

#19 Post by pylkko »

RU55EL wrote: From the Gnome desktop:
Simple: select the file name, hit F2 to rename the file, hit the right arrow key to move to the end of the file name, while holding the control key hit the backspace key (that should place the cursor between the "f" and the "." in "a.dotted.filename, stop pressing the control key and hit backspace once, then hit the space bar once. You have now changed one of the dots to a space. Simply repeat this for the other dot, and you are done. Hit enter to complete the file name change.
]
That will modify a file. However, in most computer science classes they want an answer that is generic, so that you could use it to change 1000 files all in different folders or something to that extent. You will need a small perl script to do it efficiently, and that is what the code in the OP is trying to do, only struggling with the escaping special chars.
Last edited by pylkko on 2017-08-16 05:01, edited 1 time in total.

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: change a character in filename to another

#20 Post by dasein »

dilberts_left_nut wrote:For the sed part:

A . is a wildcard that matches any character, you need to escape it with a \ to match an actual .
There's one method.
RU55EL wrote:Single quotes also work for creating a file name with spaces in Debian:

Code: Select all

touch 'a dotted filename'
There's another.

Wow, for something that "can't be done," there sure are a lot of ways to do it.

(And there's at least two more.)

kopper
Posts: 137
Joined: 2016-09-30 14:30

Re: change a character in filename to another

#21 Post by kopper »

For a thread in "Beginner Questions" section, this surely has people heated up. :D

Another one:
user@device:~ touch hi\ whats\ up
user@device:~ ls
hi whats up
Debian 10.2 Stable with i3
Secure your stuff: Securing Debian Manual
Don't break your stuff: Source List Management DontBreakDebian

Agvaniot
Posts: 42
Joined: 2017-07-26 13:07

Re: change a character in filename to another

#22 Post by Agvaniot »

dilberts_left_nut wrote:
Agvaniot wrote:im trying to take a dotted filiename "a.dotted.filename" and replace the "." with space
for example : "a dotted filename"
ive tried to come up with bunch of codes:

Code: Select all

dottedfilename=$1
${dottedfilename##*.*/' '/} | clipit

Code: Select all

sed -e  's/./ /g'` a.dotted.filename 
For the sed part:

A . is a wildcard that matches any character, you need to escape it with a \ to match an actual .
thank you very much sir.
i come up with this solution :

Code: Select all

sed -i 's/\./ /g' filewithdottedstring
i have 2 more questions in order to understand it fully
1. is "." not considered the current directory rather than a wildcard of any character?
2. if we sad that "." is wildcard that match any character still its within the single quoted string within sed doesnt supposed to surpress its special meaning?
thanks again sir

User avatar
dilberts_left_nut
Administrator
Administrator
Posts: 5346
Joined: 2009-10-05 07:54
Location: enzed
Has thanked: 13 times
Been thanked: 66 times

Re: change a character in filename to another

#23 Post by dilberts_left_nut »

Agvaniot wrote:i have 2 more questions in order to understand it fully
1. is "." not considered the current directory rather than a wildcard of any character?
When used as a 'file' argument and interpreted by the shell, yes.
In your sed example it is part of a regex expression, and processes that interpret those, treat it as the special character.
2. if we sad that "." is wildcard that match any character still its within the single quoted string within sed doesnt supposed to surpress its special meaning?
thanks again sir
The quoted string is the regex expression that is passed to sed, it is not interpreted by the shell (because it is quoted to prevent that). You can also, in appropriate circumstances, use nested single and double quotes, but that way madness lies ... :)
The proverbial 'picket fence' regexes will also do your head in so YMMV.
AdrianTM wrote:There's no hacker in my grandma...

User avatar
Lysander
Posts: 643
Joined: 2017-02-23 10:07
Location: London
Been thanked: 1 time

Re: change a character in filename to another

#24 Post by Lysander »

ruffwoof wrote: It was clarified that the question wasn't homework yet you continue to proclaim that to be a lie.
Well, let's be honest, if the OP is trying to get help with an issue of dubious origin and is called out on it, he will of course protest his innocence. We can't know for certain either way. One can only go on the most likely scenario given experience of a] Linux use and b] internet users. I personally remain skeptical.
ruffwoof wrote:A assumption that visitors come here purely for accurate answers and that no one should respond to threads without 100% certainty of accuracy is simply wrong. It's a Debian Users forum within which participants can commonly share and learn across all ability/skill levels.
I can't speak for other forum users, but in my own case, I come to learn from those with more experience, more knowledge, more applicable expertise than myself. I want to learn from people who definitely do know and have experience of the issues in hand, not those who nearly know and who provide makeshift responses to tide me over. Likewise, a student needs to learn from a master - not someone with a passable, meagerly applicable level of experience which wastes their time and risks development of bad habits.
ruffwoof wrote:Otherwise a board comprised of 'go off and do a internet search' replies could be condensed to a single front end web page.
Debian is different from other distros in that working knowledge of Debian requires understanding of its ethic. The forums encourage people to do their own work and learning wherever possible so that they can a] learn how to research in computing and b] be better long-term members of the community as a result. The forums are not here for "how do I get Z to work from scratch" but "I want Z to work, I read about it and tried X and Y to get it, what am I missing that didn't make Z happen?". When I first started posting here I was told a couple of times [or more] to RTFM. It was not what I wanted to hear, but it was very good advice because it made me more self-sufficient.

EDIT: and to add more pedantry to this post, I would say 'comprising' is preferable to 'comprised of' in this context.
ruffwoof wrote:Fear of asking the wrong question or potentially giving a wrong answer suppresses development of knowledge.
Well, there is such as thing as a stupid question, unlike what some forums would have you believe. You talk about fear like it's always a bad thing. Fear is often there as a protection mechanism. I can't believe that someone with excellent knowledge of a topic would be 'scared' of giving an answer, and I can't believe that someone who has done thorough research on a topic would be scared to ask a question. Asking lazy or idiotic questions is no substitute for doing one's own research.

User avatar
RU55EL
Posts: 546
Joined: 2014-04-07 03:42
Location: /home/russel

Re: change a character in filename to another

#25 Post by RU55EL »

pylkko wrote:[...]That will modify a file. However, in most computer science classes they want an answer that is generic, so that you could use it to change 1000 files all in different folders or something to that extent. You will need a small perl script to do it efficiently, and that is what the code in the OP is trying to do, only struggling with the escaping special chars.
Huh, would use Python to automate the task.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: change a character in filename to another

#26 Post by pylkko »

Ok, how exactly would you do that? Mind you that in perl solutions like this are one liners. In python you would need the OS module then crawl the structure somehow?

User avatar
RU55EL
Posts: 546
Joined: 2014-04-07 03:42
Location: /home/russel

Re: change a character in filename to another

#27 Post by RU55EL »

pylkko wrote:Ok, how exactly would you do that? Mind you that in perl solutions like this are one liners. In python you would need the OS module then crawl the structure somehow?
I'm not familiar with Perl, apparently it would be simpler than using Python.

Post Reply