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

change a character in filename to another

#1 Post by Agvaniot »

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 


User avatar
golinux
Posts: 1579
Joined: 2010-12-09 00:56
Location: not a 'buntard!
Been thanked: 1 time

Re: change a character in filename to another

#3 Post by golinux »

Agreed. Who would want spaces in a file name in real life?
May the FORK be with you!

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

Re: change a character in filename to another

#4 Post by Agvaniot »

golinux wrote:Agreed. Who would want spaces in a file name in real life?
im trying to upload files to a sites and i need to state the full name of a file that is generaly
defaulted with . instead of spaces and send it to clipit for paste it in the site

User avatar
4D696B65
Site admin
Site admin
Posts: 2696
Joined: 2009-06-28 06:09
Been thanked: 85 times

Re: change a character in filename to another

#5 Post by 4D696B65 »

golinux wrote:Agreed. Who would want spaces in a file name in real life?
Windows users of course.

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

Re: change a character in filename to another

#6 Post by Agvaniot »

sry i led you to belive that i want to change the dotted filename with spaces instead of dots
what i want to achieve is only to get the spaces version to clipit in order to paste it in the site im uploading the file

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: change a character in filename to another

#7 Post by GarryRicketson »

A windows user would. On a linux system , using the cli it will not recognize a file name
with spaces, ...
The OP can change the file name to a file name with spaces, using a file manager,
just click the "rename", rename it with spaces.
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, (or shouldn't have them)
You can change a filename using 'mv'

Code: Select all

man mv 

Code: Select all

mv a.dotted.filename undotted
 
Example:
Won't work

Code: Select all

cat > name with spaces
cat: with: No such file or directory
cat: spaces: No such file or directory
$ 
 
Works:

Code: Select all

$ cat > name-like-this
 

Code: Select all

$ mv a.dotted.filename a dotted filename
usage: mv [-fi] source target
       mv [-fi] source ... directory
$ cp a.dotted.filename a dotted filename 
usage: cp [-fip] [-R [-H | -L | -P]] source target
       cp [-fip] [-R [-H | -L | -P]] source ... directory
$ cat > a.dotted.filename                
this is it
 
=======

Code: Select all

$ mv a.dotted.filename  a dotted file name
usage: mv [-fi] source target
       mv [-fi] source ... directory
$ mv a.dotted.filename undotted
 

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

Re: change a character in filename to another

#8 Post by Agvaniot »

your effort to explain this is very appreciated GarryRickeston.
in other words clipit cant handle a "spaced" filename.

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

Re: change a character in filename to another

#9 Post by dasein »

@Agvaniot:

The mere fact that GarryR can't figure out how do it doesn't mean it's impossible.

But as has been noted by several of us in this thread, this problem looks exactly like a homework problem--so much so that it's unlikely you'll convince anyone otherwise. But the good news is that homework problems are generally trivial to solve with the tiniest bit of research and thought,

What does your favorite search engine tell you about ways (yes, plural) to process spaces in a *nix filename?

@GarryR: for what I feel sure is the last time I will say it, if you can't give a good answer, would you please, for the love of whatever you hold Sacred, stop giving bad ones. Spreading ignorance and misinformation contributes less than nothing.
Last edited by dasein on 2017-08-15 20:13, edited 1 time in total.

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

Re: change a character in filename to another

#10 Post by pylkko »

Garry, I just opened a terminal window on my desktop and created a file (using touch) named "name with spaces". Then edited content into it and the did the cat that you say is impossible. Well, it is not impossible.

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

Re: change a character in filename to another

#11 Post by ruffwoof »

If I open leafpad, enter some text and then save as test file.txt then I can cat that using cat "test file.txt" or using a wildcard such as cat "test fi"*

I have a number of files with spaces in the filename as sometimes I create quite a long filename that describes the content in perhaps 6 or 7 words. Guess I should have really used - or _ instead of spaces, but it does work with spaces.

Other characters are disallowed, for instance creating a filename of ;rm -rf / is blocked for obvious reasons.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: change a character in filename to another

#12 Post by GarryRicketson »

Hmm I just now, tried,

Code: Select all

touch name like this
And it created a file named "name", ???
@GarryR: for what I feel sure is the last time I will say it, if you can't give a good answer, would you please, for the love of whatever you hold Sacred, stop giving bad ones
And you call what you said a "good answer",... ?Before you derail a topic, with your attacks and flames, think about what you say, and apply it to yourself, if you can't give a good answer, then don't reply.
Look, I do not understand what your problems are, but my answer was a response to the OP, and I also did tell them :
by GarryRicketson » 2017-08-15 12:35 The OP can change the file name to a file name with spaces, using a file manager,
just click the "rename", rename it with spaces.
They should then also be able to submit it to "clip it" , with the new name.
Post by Agvaniot » 2017-08-15 13:23
your effort to explain this is very appreciated GarryRickeston.
in other words clipit cant handle a "spaced" filename.
Not exactly, what I mean is what I said, most unix and linux systems, do not see a file name or dir name, with spaces, for example, if you do rename a file, using your file manage, make a name with spaces, IE : " with spaces ", then try to view / or open
the file using the cli, Example :

Code: Select all

less with spaces 
It will say "file not found".
pylkko wrote:Garry, I just opened a terminal window on my desktop and created a file (using touch) named "name with spaces". Then edited content into it and the did the cat that you say is impossible. Well, it is not impossible.
Interesting, as I showed above, it does not work for me,...
But in any event, agreed that does not mean it is 100% impossible, there may be some
way to configure the system so it does read file names that have spaces,...
If I did know a way to do this, I certainly would just share it with the OP and others,
instead of starting a flame war,... or just saying :
dasein wrote:Homework
@ dasein, If you can't or don't want to give a answer, then don't reply :mrgreen:

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

Re: change a character in filename to another

#13 Post by dasein »

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.

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 462 times

Re: change a character in filename to another

#14 Post by sunrat »

GarryRicketson wrote:Hmm I just now, tried,

Code: Select all

touch name like this
And it created a file named "name", ???
It would also create files "like " and "this". Try:

Code: Select all

touch "name like this"
GarryRicketson wrote:Not exactly, what I mean is what I said, most unix and linux systems, do not see a file name or dir name, with spaces, for example, if you do rename a file, using your file manage, make a name with spaces, IE : " with spaces ", then try to view / or open
the file using the cli, Example :

Code: Select all

less with spaces 

Code: Select all

less "with spaces"
pylkko wrote:Garry, I just opened a terminal window on my desktop and created a file (using touch) named "name with spaces". Then edited content into it and the did the cat that you say is impossible. Well, it is not impossible.
Interesting, as I showed above, it does not work for me,...
But in any event, agreed that does not mean it is 100% impossible, there may be some
way to configure the system so it does read file names that have spaces,...
You can use quotes and escape:

Code: Select all

cat with\ spaces
Bash autocomplete helps with that.
If I did know a way to do this, I certainly would just share it with the OP and others,
instead of starting a flame war,... or just saying :
dasein wrote:Homework
@ dasein, If you can't or don't want to give a answer, then don't reply :mrgreen:
Please don't post that something can't be done when really it's just that you don't know how.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: change a character in filename to another

#15 Post by GarryRicketson »

If you use

Code: Select all

 " 
to enclose the file name:

Code: Select all

$ cat >this.file.txt
This is it
$ 
 
==========
Worked:

Code: Select all

$ mv this.file.txt "this file txt" 
$ cat "this file txt"
This is it
$ 

 
So in other words , to change a.dotted.filename to a dotted filename:

Code: Select all

mv a.dotted.filename  "a dotted filename"
or use 'cp' instead if you want to keep the original file name.

I did not know this before, ... so there are ways to work with files that have spaces in
the names after all,...
=====EDited====
Posted at the same moment I was writing,...
by sunrat »
Please don't post that something can't be done when really it's just that you don't know how.
Well I really did not think it can be done, but when I saw what pylkko and ruffwoof said,
I did some searches, and find it can be done,.... I still think everything is easier just using
name without spaces,....
Any way, there also may be other ways to do this as well, Thanks sunrat,
you posted at the same time I was writing, and doing a search, any way, I had just now found this out, thanks for showing the OP how.

User avatar
dilberts_left_nut
Administrator
Administrator
Posts: 5346
Joined: 2009-10-05 07:54
Location: enzed
Has thanked: 12 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.)

Post Reply