DEV Community

Safal Bhattarai
Safal Bhattarai

Posted on

Linux Folder Structure Explained: /home, /root, and /tmp

In Part 1 we looked at the root directory (/) and /etc. In this part we cover three places you will use every day: /home, /root, and /tmp.

/home: where regular users live

Every regular user gets a personal folder inside /home. This is where your documents, downloads, projects, and personal settings are stored.

ls /home
Enter fullscreen mode Exit fullscreen mode
safal  guest
Enter fullscreen mode Exit fullscreen mode

Your own folder is /home/<username>. You can jump to it from anywhere with:

cd ~
Enter fullscreen mode Exit fullscreen mode

The ~ symbol is a shortcut for your home directory. You can also print it:

echo $HOME
Enter fullscreen mode Exit fullscreen mode
/home/safal
Enter fullscreen mode Exit fullscreen mode

Typical contents

ls ~
Enter fullscreen mode Exit fullscreen mode
Desktop  Documents  Downloads
Music    Pictures   Videos
Enter fullscreen mode Exit fullscreen mode

Hidden files (dotfiles)

Files and folders that start with a dot are hidden. Programs store your personal settings in them. Use -a to see them:

ls -a ~
Enter fullscreen mode Exit fullscreen mode
.  ..  .bashrc  .profile  .ssh
.config  .local  .cache
Enter fullscreen mode Exit fullscreen mode
Dotfile Purpose
.bashrc Bash settings and aliases
.profile Login shell settings
.ssh/ SSH keys and known hosts
.config/ Settings for many apps
.cache/ Cached data, safe to clear

Check how much space you use

du -sh ~
Enter fullscreen mode Exit fullscreen mode
4.2G    /home/safal
Enter fullscreen mode Exit fullscreen mode

Permissions

Home folders are private to each user by default:

ls -ld ~
Enter fullscreen mode Exit fullscreen mode
drwxr-x--- 15 safal safal 4096 Nov 2 10:15 /home/safal
Enter fullscreen mode Exit fullscreen mode

On many newer distributions, other users cannot read your home folder, which is good for privacy.

/root: the home of the root user

/root is the home directory of the root (administrator) user. It is not inside /home, so it stays available even if /home is on a separate disk that fails to mount.

ls -ld /root
Enter fullscreen mode Exit fullscreen mode
drwx------ 5 root root 4096 Nov 2 09:40 /root
Enter fullscreen mode Exit fullscreen mode

Only root can enter it. To look inside, use sudo:

sudo ls -a /root
Enter fullscreen mode Exit fullscreen mode

Do not confuse these three:

Path Meaning
/ Root of the whole filesystem
/root Home folder of the root user
root The administrator account

/tmp: temporary files

/tmp is a shared place for short-lived files. Programs use it for scratch data, and any user can write to it.

ls -ld /tmp
Enter fullscreen mode Exit fullscreen mode
drwxrwxrwt 18 root root 4096 Nov 2 10:20 /tmp
Enter fullscreen mode Exit fullscreen mode

Notice the t at the end of the permissions. This is the sticky bit. It means everyone can create files in /tmp, but you can only delete or rename your own files.

Create a safe temporary file

Use mktemp instead of inventing a file name, so two programs never clash:

mktemp
Enter fullscreen mode Exit fullscreen mode
/tmp/tmp.Xk3j9fA2bQ
Enter fullscreen mode Exit fullscreen mode

Create a temporary folder:

mktemp -d
Enter fullscreen mode Exit fullscreen mode

Files may disappear

Many systems clear /tmp on reboot, and some also delete old files automatically. Never store anything important there.

If you need temporary files that survive a reboot, look at /var/tmp, which we will cover in the /var part.

Security tip

Because /tmp is writable by everyone, avoid putting passwords, keys, or private data there. Also avoid predictable file names in scripts, and prefer mktemp.

Quick comparison

Directory Owner Survives reboot?
/home/user That user Yes
/root Root only Yes
/tmp Everyone Often no

Summary

  • /home/<user> is your personal space, with hidden dotfiles for settings.
  • /root is the private home folder of the administrator.
  • /tmp is shared and temporary, so never keep important data there.

Next in the series: Part 3, /var (logs, cache, and spool).

Found a mistake or have a tip? Leave a comment below.

Top comments (0)