change a character in filename to another

New to Debian (Or Linux in general)? Ask your questions here!

Re: change a character in filename to another

Postby dilberts_left_nut » 2017-08-16 02:40

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...
User avatar
dilberts_left_nut
 
Posts: 4703
Joined: 2009-10-05 07:54
Location: enzed

Re: change a character in filename to another

Postby ruffwoof » 2017-08-16 02:55

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.
Debian oldstable, twm, yad, stalonetray
Acer Aspire M3201 (2GB), AMD Phenom X4
ruffwoof
 
Posts: 216
Joined: 2016-08-20 21:00

Re: change a character in filename to another

Postby RU55EL » 2017-08-16 04:33

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
RU55EL
 
Posts: 336
Joined: 2014-04-07 03:42
Location: /home/russel

Re: change a character in filename to another

Postby pylkko » 2017-08-16 04:57

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
pylkko
 
Posts: 1213
Joined: 2014-11-06 19:02

Re: change a character in filename to another

Postby dasein » 2017-08-16 04:59

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.)
User avatar
dasein
 
Posts: 7775
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: change a character in filename to another

Postby kopper » 2017-08-16 05:12

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 9.2 Stable with i3
Secure your stuff: Securing Debian Manual
Don't break your stuff: Source List Management DontBreakDebian
kopper
 
Posts: 79
Joined: 2016-09-30 14:30

Re: change a character in filename to another

Postby Agvaniot » 2017-08-16 06:01

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
Agvaniot
 
Posts: 42
Joined: 2017-07-26 13:07

Re: change a character in filename to another

Postby dilberts_left_nut » 2017-08-16 06:44

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
dilberts_left_nut
 
Posts: 4703
Joined: 2009-10-05 07:54
Location: enzed

Re: change a character in filename to another

Postby Lysander » 2017-08-16 13:26

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
Lysander
 
Posts: 364
Joined: 2017-02-23 10:07
Location: London

Re: change a character in filename to another

Postby RU55EL » 2017-08-16 23:03

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
RU55EL
 
Posts: 336
Joined: 2014-04-07 03:42
Location: /home/russel

Re: change a character in filename to another

Postby pylkko » 2017-08-17 03:52

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
pylkko
 
Posts: 1213
Joined: 2014-11-06 19:02

Re: change a character in filename to another

Postby RU55EL » 2017-08-17 04:03

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.
User avatar
RU55EL
 
Posts: 336
Joined: 2014-04-07 03:42
Location: /home/russel

Previous

Return to Beginners Questions

Who is online

Users browsing this forum: Capitain_Jack and 7 guests

fashionable