Create Hidden File or Directory using Shell Command


In order to create a hidden file using a shell (Bash, Zsh, or sh) you can make use of the touch, nano, or vi commands by adding a . (dot) at the start of the file name,



Part 1: Create Hidden Files

Example: Create a hidden file using the touch command
# touch .my-hidden-file.txt

# ls

# ls -ltrha
total 8.0K
drwxr-xr-x 1 root root 4.0K Apr  2 02:19 ..
-rw-r--r-- 1 root root    0 Apr  2 02:19 .my-hidden-file.txt
drwxr-xr-x 2 root root 4.0K Apr  2 02:19 .

As you can see I created a hidden file using the touch command touch .{file-name}, you can also add an extension to the file name.

Example: Create a hidden file using the nano command
# nano .hidden-file.txt

To view the content of the hidden file you need to make sure you again use . (dot) at the start of your file name.

# cat .hidden-file.txt 
This is my hidden file.


Part 2: Create Hidden Directories

create hidden files and directories using shell commands

You can make use of mkdir command with the directory name that starts with a dot just like files.

mkdir .my-hidden-directory

# ls -la
total 16
drwxr-xr-x 3 root root 4096 Apr  2 02:27 .
drwxr-xr-x 1 root root 4096 Apr  2 02:19 ..
-rw-r--r-- 1 root root   24 Apr  2 02:23 .hidden-file.txt
drwxr-xr-x 2 root root 4096 Apr  2 02:27 .my-hidden-directory
-rw-r--r-- 1 root root    0 Apr  2 02:19 .my-hidden-file.txt
Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap