LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
 
LinkBack 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,570

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

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 07:01 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration