38

I am getting the error:

touch: cannot touch `/opt/tsrm/compliance/cme/log/20121207.log`: No such file or directory

on the touch command: touch $LOGFILE

I also checked the link: touch: cannot touch `foo': No such file or directory, But I didn't understand the answer.

Note: I was also getting mkdir: cannot create directory; I fixed this by adding the -p option.

Could this be something with the version of Linux I am working in?

Swagatika
  • 863
  • 3
  • 11
  • 18

4 Answers4

29

You do not have the path that holds the file:

/opt/tsrm/compliance/cme/log/

That's where the error come from.

daisy
  • 53,527
  • 78
  • 236
  • 383
9

As silly as this reply sounds, other than missing directory, there could be string delimiter surrounding the value in $LOGFILE

Working:

LOGFILE=/stuff/more_stuff/stuff.file
touch $LOGFILE

Not Working:

LOGFILE="/stuff/more_stuff/stuff.file"
touch $LOGFILE

Some Linux distro have the quirk above, so be careful ;)

Thomas
  • 6,242
  • 8
  • 26
  • 32
Dwi C Taniel
  • 91
  • 1
  • 1
9

Sounds like you misspelled the path. For example, this folder /stuff/more_stuff does not exist. This gives me the result:

user@linux:~ $ touch /stuff/more_stuff/stuff.file
touch: cannot touch `/stuff/more_stuff/stuff.file': No such file or directory

Both /stuff and /stuff/more_stuff need to exist in order for touch to work.

Kotte
  • 2,467
  • 22
  • 26
  • 1
    In my case I am trying to use `touch /etc/resolv.conf` and it says `touch: cannot touch '/etc/resolv.conf': No such file or directory`. `/etc` definitely does exist. – Aaron Franke Mar 09 '18 at 01:24
  • 2
    Have you checked if `resolv.conf` is a symlink to a file that doesn't exist? – Kotte Mar 09 '18 at 10:34
6

Yes there might be chances of miss typo or directory not exists .

LOGFILE="/opt/tsrm/compliance/cme/log/20121207.log"
LOG_DIR=`dirname $LOGFILE`
[ ! -d $LOG_DIR ] && mkdir -p $LOG_DIR
touch $LOGFILE
Rahul Patil
  • 24,281
  • 25
  • 80
  • 96