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
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.