LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-15-2014, 10:11 AM   #1
bootfinder
LQ Newbie
 
Registered: Apr 2014
Posts: 2

Rep: Reputation: Disabled
mkdir() question


I'm using c++ on Ubuntu 12.04

I would like to create a new directory in the home directory, but when I try:
mkdir("~/newdir")

it fails because it doesn't translate "~"

How do I get the absolute path to the home dir?
 
Old 04-15-2014, 10:41 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by bootfinder View Post
I'm using c++ on Ubuntu 12.04

I would like to create a new directory in the home directory, but when I try:
mkdir("~/newdir")

it fails because it doesn't translate "~"

How do I get the absolute path to the home dir?
You have to manually translate the HOME environment variable, OR use getpwuid(getuid()) to retrieve the password entry structure (struct passwd), then combine the home directory with the directory you want created - this gives a full path for mkdir.

An alternative to creating a full path - once the home directory has been retrieved you could use chdir(path) to ensure that you are in the right place, then use mkdir with the short name. This is reasonable if you are going to change the working directory to the just created directory for further use.

Of the two choices to determining the home directory, using the getpwuid(getuid()) method is the most reliable. Using the HOME environment variable allows the directory to be created where ever HOME points to, which doesn't HAVE to be the users home directory.
 
1 members found this post helpful.
Old 04-15-2014, 12:24 PM   #3
bootfinder
LQ Newbie
 
Registered: Apr 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
thanks for the info.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
mkdir Huamin Linux - Software 6 05-04-2012 05:42 AM
[SOLVED] mkdir: cannot creat directory. How to enable mkdir for user in usb dir? Foxbat1155 Linux - Newbie 13 02-16-2012 12:20 PM
[SOLVED] mkdir shamjs Programming 3 12-07-2011 01:15 AM
how to give options like '-v' for mkdir using system call mkdir() ? nehapawar Linux - Newbie 2 02-05-2010 03:13 AM
another shell question..., using mkdir haora Linux - Newbie 5 04-06-2005 04:21 PM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration