0

Each time that I download a youtube file (e.g. abc.mp4), I immediately alter its date/time stamp by manually executing the following command in a bash terminal: touch -d "$(date)" "abc.mp4".

This works fine except when the file has an imbedded exclamation mark (e.g. ab!c.mp4). Then, the following command malfunctions: touch -d "$(date)" "ab!c.mp4".

Experimenting, I tried: touch -d "$(date)" "ab\!c.mp4" : no joy, the touch command created a new empty file with the name ab\!c.mp4.

My kludgy solution has been to manually rename the youtube file, removing the exclamation mark from its name, and then executing the touch command.

Is there a more elegant method that allows the exclamation mark to remain in the file name?

user2661923
  • 113
  • 7

2 Answers2

2

Try calling set +H before your touch command or just use single quotes: touch -d "$(date)" 'ab!c.mp4'.

Makkes
  • 1,768
  • 15
  • 19
2

Turn off history expansion (set +H) or use single quotes. For example:

$ echo "foo!bar"
bash: !bar": event not found
$ echo 'foo!bar'
foo!bar
$ set +H
$ echo "foo!bar"
foo!bar

From the manpage:

Only backslash (\) and single quotes can quote the history expansion character.

Also, for reference:

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.  The characters $ and ` retain their special meaning within double quotes.  The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \,  or <newline>.   A  double  quote  may  be quoted within double quotes by preceding it with a backslash.  If enabled, history expansion will be performed unless an !  appearing in double quotes is escaped using a backslash.  The backslash  preceding the !  is not removed.
William Pursell
  • 204,365
  • 48
  • 270
  • 300