LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-26-2010, 08:31 PM   #1
youarefunny
Member
 
Registered: Mar 2010
Distribution: Arch Linux, FreeBSD
Posts: 31

Rep: Reputation: 0
Question Using tilde and spaces


I'm trying to use a tilde '~' for a filename. This works fine normally
Code:
rm ~/File
But when I have a file with a space in in and enclose it in quotes it takes it literally.

Code:
rm "~/Da File"
rm: cannot remove `~/Da File': No such file or directory
I have tried many things.

Code:
rm ~"/Da File"
rm '~/Da File'
rm $(~/Da File)
rm ~'/Da File'
rm "$(~/Da File)"
But no success. Any help is appreciated thank you.

Solution:
Quote:
Code:
rm ~/"Da File"
Reasons explained in the BASH reference guide (or man bash at your pleasure):
Quote:
If a word begins with an unquoted tilde character (‘~’), all of the characters up to the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix.

Last edited by youarefunny; 05-27-2010 at 04:44 PM. Reason: Solved
 
Old 05-26-2010, 08:49 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
try $HOME instead of ~
 
Old 05-27-2010, 01:31 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by youarefunny View Post
I'm trying to use a tilde '~' for a filename. This works fine normally
Code:
rm ~/File
But when I have a file with a space in in and enclose it in quotes it takes it literally.

Code:
rm "~/Da File"
rm: cannot remove `~/Da File': No such file or directory
I have tried many things.

Code:
rm ~"/Da File"
rm '~/Da File'
rm $(~/Da File)
rm ~'/Da File'
rm "$(~/Da File)"
But no success. Any help is appreciated thank you.
Here is a (probably an overkill) hint for you:

Code:
echo `echo ~`/'Da File'
.

Rather,

Code:
echo "'"`echo ~`/'Da File'"'"
.

Last edited by Sergei Steshenko; 05-27-2010 at 01:35 AM.
 
Old 05-27-2010, 01:44 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You missed
Code:
rm ~/"Da File"
Reasons explained in the BASH reference guide (or man bash at your pleasure):
Quote:
If a word begins with an unquoted tilde character (‘~’), all of the characters up to the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix.
 
Old 05-27-2010, 08:56 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Move to Linux-General (Not strictly a programming question)

It seems that ~ expands to $HOME, but only when not quoted?
 
Old 05-27-2010, 10:38 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes. If enclosed in double quotes ~ retains its literal meaning. If not quoted, bash performs tilde expansion and considers the tilde-prefix up to the first unquoted /. This means that in commands like
Code:
ls ~user/directory/*.png"
ls ~user/"dir with spaces"/*.ps"
ls ~user
~user is the tilde-prefix expanded to the user's home, whereas in
Code:
ls ~"user"
ls ~"/dir with spaces"
nothing is treated as a tilde-prefix and therefore there isn't any tilde expansion.
 
  


Reply

Tags
bash, file, filename, home directory, name, spaces



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File name followed by a tilde ~ malikah Linux - Newbie 2 02-17-2009 04:10 PM
tilde symbol for what purpose Karthikeyan Gurusamy Linux - Newbie 4 01-17-2006 04:22 AM
Tilde name shortening steveel Slackware 6 11-21-2005 03:55 AM
tilde in X nva Linux - General 2 06-30-2003 11:12 AM
Filenames with tilde rovoreed Programming 2 08-08-2001 04:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:35 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