Touch in the modern world

I thought I’d ask the community something that has become a question in my mind.

I saw two different tutorials today where the instructions state:

touch filename
vi filename

I know what touch does, but it isn’t needed at all. Whether you use vim or nano and probably most other editors you can just specify the new filename on the command line. Like so:

vi filename
nano filename

When you’re done making changes you just save the file.

Is this type of touch use just a holdover from an older more primitive editor?

I tend to use touch as a quick method of creating a file to make sure a disk can be written to. Touch is by definition a command line method of changing file name access and modification times, but you can do things like the following:

touch file1.txt file2.txt file3.txt

And it’ll create those three files. In my opinion that’s quicker than doing nano file1.txt, saving it and exiting and doing file2.txt, saving and exiting, and so on and so forth. Since Linux was derived from UNIX, and UNIX is a programmer’s operating system, it makes sense that the developers would want to save as many keystrokes as possible while doing things. (hackers are all about efficiency). There are also instances where nano or vi wouldn’t be available, such as really barebones installations of linux (vi might be available on everything, but not everyone is comfortable with using vi) …

3 Likes

LOL. I know what touch is for sure. :slight_smile:

Yeah we’ve discussed some of this before in other topics. Vi and or nano should always be installed, but we realize it isn’t.

My point was that in these cases using touch was an extra step. Vi or nano is available and even used as the second step. You don’t need to create the file first. That’s why I figured it was just a holdover from some time in the dim and distant past where maybe vi didn’t have the ability to create a file. It had to exist already.

lol no, I know you knew what it was, I was just stating that to make the whole response make sense, to me anyway lol

There is a subtle difference.
If you vi filename then do not insert anything and exit, it does not create the file.

1 Like

This is true. Mainly though I was talking about the tutorial that has you touch a file to create it and then immediately edit it with vi or nano. If they just want to edit a new file there is no need to touch it first.

1 Like

Touch is also often used to update a file’s access date and time (-a), modification date and time (-m), update the timestamp of a symbolic link (-h), avoid creating a new file (-c, e.g. only update the timestamp if the file exists), update the time stamp to be the same as a reference file (-r), update the time stamp to a specific date and time (-t), or update the date to a specified date (-d).

For a person using Linux as a personal computer, you are right, touch doesn’t have much use. However, in the IT world there might be administrative services that requires files to have a certain time stamp or require empty files to write to. In that regard, touch is VERY useful. The touch command just fills that specific niche.

2 Likes

It is used like that in Makefiles

1 Like

touch is one of the few things that can change file’s timestamp… it’s also useful to create flag files…

2 Likes

I like the use of touch in this context as it makes explicit that, after this command, the file exists. It is plainly redundant because of the behaviour of editors on file-paths for non-existing files.

Personally, I would actually prefer it if editors (launched from the command line) complained if files did not exist. That’s for the same reason that I prefer programs to explicitly declare variables, not bring them into existence because they are a target on the left-hand side of an assignment.

  • Steve
1 Like

I dont like any software that does things behind my back.

1 Like