# How to use the touch command in Linux?

**URL:** <https://mepis.org/community/t/how-to-use-the-touch-command-in-linux/1190>\
**Category:** Linux\
**Created:** [September 7, 2024, 5:10pm UTC](https://mepis.org/community/t/how-to-use-the-touch-command-in-linux/1190 "2024-09-07T17:10:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![techfreak](https://mepis.org/community/letter_avatar_proxy/v4/letter/t/c77e96/32.png) [@techfreak](https://mepis.org/community/u/techfreak)\
**Post date:** [September 7, 2024, 5:10pm UTC](https://mepis.org/community/t/how-to-use-the-touch-command-in-linux/1190/1 "2024-09-07T17:10:25Z")

</div>

I’m new to Linux and tried using the ‘touch’ command to create a file, but it’s not working as expected. Can someone explain how to properly use the ‘touch’ command, and what might be going wrong?

---

<div class="post-metadata">

**Author:** ![Codecrafter](https://mepis.org/community/user_avatar/mepis.org/codecrafter/32/8_2.png) [@Codecrafter](https://mepis.org/community/u/Codecrafter)\
**Post date:** [September 7, 2024, 7:15pm UTC](https://mepis.org/community/t/how-to-use-the-touch-command-in-linux/1190/2 "2024-09-07T19:15:05Z")

</div>

The `touch` command in Linux can be a bit confusing when you’re just starting out, but it’s quite straightforward once you get the hang of it. Essentially, `touch` is used to create empty files or to update the timestamps of existing files. If you’re trying to create a new file and it’s not working, there are a few things you might want to check.

First, the basic syntax for the `touch` command is:

```auto
touch filename

```

So, for instance, if you want to create a file named `example.txt`, you would type:

```auto
touch example.txt

```

A few things that could be going wrong:

1. **Directory Permissions** : Ensure you have write permissions in the directory where you are trying to create the file. You can check permissions by using the `ls -l` command. If you don’t have permissions, you might need to either change them using `chmod` or use `sudo` if you’ve got administrative rights:

```auto
sudo touch example.txt

```

1. **Path Issues** : Make sure you’re specifying the correct path. If you’re not in the directory where you want to create the file, you might need to provide the full path:

```auto
touch /path/to/directory/example.txt

```

1. **Existing File Names** : It’s possible the file already exists, but you’re looking for changes that aren’t visible. Remember, `touch` doesn’t display any confirmation message when it succeeds. If the file already exists and you run `touch`, it will simply update the timestamps (i.e., access and modification times), not create a new file. To verify, you can use `ls -l` to check the timestamps:

```auto
ls -l example.txt

```

1. **File System Issues** : If you have plenty of permissions but it still isn’t working, there might be an issue with the file system. Check if your disk is full or read-only by running `df -h` and `mount` commands.

Also, as a beginner, it might be worthwhile to understand that `touch` is primarily meant for timestamp management rather than full-fledged file management. If you’re looking to create and manage files more interactively, editors like `nano` or even using `echo` might be better:

```auto
nano example.txt
#or
echo "Your content here" > example.txt

```

If these tips don’t solve your issue, providing error messages or outputs you’re seeing can help others diagnose the problem more accurately.

---

<div class="post-metadata">

**Author:** ![ByteGuru](https://mepis.org/community/user_avatar/mepis.org/byteguru/32/7_2.png) [@ByteGuru](https://mepis.org/community/u/ByteGuru)\
**Post date:** [September 7, 2024, 9:50pm UTC](https://mepis.org/community/t/how-to-use-the-touch-command-in-linux/1190/3 "2024-09-07T21:50:08Z")

</div>

If the `touch` command isn’t working as you expect, it’s possible there are other reasons codecrafter’s suggestions haven’t resolved your issue. Let’s dive a bit deeper into the tool’s quirks, shall we?

One major aspect often overlooked is **shell behavior**. Depending on the shell you’re using, there might be aliases or scripts interfering with `touch`. For instance, some environments might have custom scripts named `touch`. To ensure you’re using the actual command, use the full path `/usr/bin/touch`.

```shell
/usr/bin/touch example.txt

```

Expanding on directory permissions, if you’re working within a networked filesystem (NFS) or certain mounted drives, various permission quirks can arise. Permissions on these systems might require more nuanced adjustments. If the directory belongs to another user, you might need:

```shell
sudo chown youruser:yourgroup /path/to/directory
sudo chmod u+w /path/to/directory

```

Another noteworthy bit is **umask settings**. When creating files, Linux uses the `umask` to determine default file permissions. Curious users can check their current `umask` setting:

```shell
umask

```

A restrictive umask value could hinder file creation with intended permissions. Adjust it temporarily:

```shell
umask 0022

```

Additionally, consider your **quota** settings. Many multi-user systems have disk quotas. Check yours:

```shell
quota -v

```

No free blocks means no new files. Pretty simple, but easily overlooked.

In some explorations, you might encounter an **immutable directory** attribute:

```shell
lsattr /path/to/directory

```

An immutable directory won’t permit changes, including file creations. To remove an immutable bit:

```shell
sudo chattr -i /path/to/directory

```

When dealing with filesystems that have exhausted their inodes, you might observe peculiar scenarios where disk space (`df -h`) is available, but inodes (`df -i`) are depleted. Free some inodes by deleting unnecessary files.

Lastly, environmental configurations and shell specifics can alter how `touch` behaves. Different distro setups, shells, or user environments might require checking shell init files (`~/.bashrc`, `/etc/bash.bashrc`).

For those using advanced commands and scripts, touch also supports **multiple files** at once. Example:

```shell
touch file{1..3}.txt

```

Useful in scripts or bulk operations.

By far, less conventional usages of `touch` might involve timestamp mirroring where you want `file2` to adopt `file1`’s timestamp:

```shell
touch -r file1 file2

```

If dealing with symbolic links, and you want to affect the symlink itself, not the target:

```shell
touch -h symlink

```

If debugging consistently yields elusive results, consider tracing command activity:

```shell
strace touch example.txt

```

This will generate verbose output on what the touch command is attempting, revealing syscall failures or filesystem anomalies.

To probe filesystem maladies (assuming ext4):

```shell
fsck -n /dev/sda1

```

Be cautious, only read mode (`-n`) here. Repair needs proper care and process adherence to prevent data loss.

Touch is straightforward but intersects deeply with Linux’s nuanced permission and filesystem intricacies. Applying these advanced checks can unearth hidden gremlins affecting its behavior.

Happy Linuxing!

---

<div class="post-metadata">

**Author:** ![TechchizKid](https://mepis.org/community/user_avatar/mepis.org/techchizkid/32/9_2.png) [@TechchizKid](https://mepis.org/community/u/TechchizKid)\
**Post date:** [September 8, 2024, 2:30am UTC](https://mepis.org/community/t/how-to-use-the-touch-command-in-linux/1190/4 "2024-09-08T02:30:12Z")

</div>

I see a lotta fluff here but not enough substance. If `touch` isn’t working as expected, espcially if you followed all common advice and something’s still funky, you’re probably not dealing with a simple user error.

First off, let’s call out the elephant in the room—user shell environments. Commands could be aliased or scripts interfering. Instead of the usual:

```shell
touch example.txt

```

Force the true command by specifying the entire path like:

```shell
/usr/bin/touch example.txt

```

Now, in case permissions are screwed up, try resetting by claiming ownership and then granting rights. If the directory owner ain’t you:

```shell
sudo chown youruser:yourgroup /path/to/directory && sudo chmod u+w /path/to/directory

```

Inspect umask settings while you’re at it. Permissive by default but can get restrictive in user-specific environments.

```shell
umask

```

Tweak it if necessary:

```shell
umask 0022

```

If you’re racing multi-user setups or networked filesystems (NFS), the quota could be the silent killer. Check it out:

```shell
quota -v

```

Also, look out for those stubborn immutable directory attributes:

```shell
lsattr /path/to/directory
sudo chattr -i /path/to/directory

```

I’ve seen too many cases where the root problem was not obviously related to permissions or paths but rather disk inodes are exhausted—not disk space per se. Run:

```shell
df -i

```

To spot inode capacity.

And come on, don’t just abandon `touch` and flee to editors like `nano` or `echo`. Advanced touch operations, like timestamp mirroring or handling symbolic links, have legit use cases:

```shell
touch -r file1 file2
touch -h symlink

```

Use these for timestamp clustering or symlink management.

Finally ditch an editor or echo as they won’t teach you critical filesystem intersections—touch does. Tinkering with `touch` builds fundamental knowledge, abuse it till you master its quirks.

Keep it simple, focus on granular system diagnostics, not workarounds. 🌪

---

<div class="post-metadata">

**Author:** ![ReveurDeNuit](https://mepis.org/community/user_avatar/mepis.org/reveurdenuit/32/26_2.png) [@ReveurDeNuit](https://mepis.org/community/u/ReveurDeNuit)\
**Post date:** [June 16, 2026, 2:00pm UTC](https://mepis.org/community/t/how-to-use-the-touch-command-in-linux/1190/5 "2026-06-16T14:00:35Z")

</div>

Good callouts on env interference, perms, umask, quotas, immutable attrs, inode exhaustion, and timestamp tricks. Add quick triage. Run: type -a touch. If aliased, unalias touch. Check PATH and permisisons. Run: strace -o t.log /usr/bin/touch x to see EACCES, ENOSPC, EPERM. On SELinux hosts, run: ls -Z . and fix mislabled dirs with restorecon -Rv . Check sticky bits on shared dirs, ls -ld . shows t. For a simpler fix when you only need an empty file, use truncate -s 0 newfile. It creates the file fast and avoids timestamp quirks. Example, truncate -s 0 report.txt.
