Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

when I want to touch files like report-05/07/13 with command touch report-$(date +%D) it gives me an error like this:

touch: cannot touchreport-07/05/13': No such file or directory`

How can I build one?

By the way there is "NO FOLDER" it is JUST THE FILENAME.

share|improve this question
1  
Did the folder report-07/ exist? –  galegosimpatico Jul 5 '13 at 6:44
add comment

3 Answers 3

up vote 2 down vote accepted

the / sign is for path separator.

When you execute that command the result will be

report-07/05/13 but the shell will interpret like this

report-07 - Parent Directory 
05 - subdirectory
03 - filename

If indeed you want the directory report-07/05 then first you need to create it with:

mkdir -p report-`date +%m\/%d`
touch report-`date +%D`

If you want is just a file named date-M.D.Y then it will be easier to change the separator

touch report-`date +%m.%d.%y`
share|improve this answer
    
if i want to use / instead of . can i use \/? –  Hojat Taheri Jul 5 '13 at 7:10
    
no really, it will still search for the directory. # touch test\/test touch: cannot touch ‘test/test’: No such file or directory –  BitsOfNix Jul 5 '13 at 7:13
add comment

touch can only create files, not directories. You have to create the directory first e.g. by calling mkdir -p $(dirname report-$(date +%D)).

The -p stands for parent and creates all missing directories in the given path. And dirname will strip of the last component of a path, passing only the directory to mkdir.

share|improve this answer
add comment

Forward slashes are path separators in Unix, and as such cannot be used as part of a filename. If your specification is actually a full path, you can do this:

file=report-$(date +%D)
mkdir -p "${file%/*}"
touch "$file"
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.