Security

Protecting your files and folders

At times, files and folders are so important that we don’t even want them to be visible on the desktop, a desktop that you could be sharing with others! What if you had tax-related files on your desktop or other files with your complete information? You certainly wouldn’t want them even seen! In such cases, one option is to protect the said files. At times, files and folders need to be kept under lock and key. Whatever the reason for wanting to keep said files and folders safe, there are only two ways that I can think of to accomplish this task. The first way is quite easy, and it’s not technically lock and key, but using the chmod command, one can technically restrict access to a file or a folder. However, some people feel safer if they can add a padlock. This tutorial will learn to (i) protect files and folders and (ii) hide them in plain sight.

Protecting a file or folder

Method #1: Compression

The easiest way of safeguarding your files is to compress them and give them a password. On Linux, you can do this using the zip command.

# zipping a directory with a password

zip -p <password> <zip-file-name.zip> <directory to zip>

# zipping multiple files with a password

zip -p <password> <zip-file-name.zip> <file 1> <file 2><file n>

Alternatively, you can encrypt it.

Method #2: eCryptfs

eCryptfs is a free and open-source code for encrypting and decrypting files and folders with passwords.

To install eCryptfs:

sudo apt-get install ecryptfs-utils

To use eCryptfs:

  1. Create an empty folder (ex: security)
  2. then type the following:
mount -t encryptfs <foldern name> <mount point>

Ex: mount -t encryptfs security security

  1. It will ask you to choose a key type, choose one. It will also ask you for a passphrase, so specify a passphrase, and do not forget it! Next, it’ll ask you to specify a cipher, choose one. Then it will ask you for the key bytes; choose one (the longer, the better). Then, continue choosing. And you must remember your choices because when you want to decrypt them, you will have to provide this information.
  2. Now add the files you want to the security folder.
  3. Unmount it to encrypt it.

umount security

In order to decrypt the folder, you have to re-mount it (step 2), and then provide the same answers you provided during the first go. If your answers are not exactly the same, you will not be able to decrypt the folder!

Method #3: encFS

encFS is a free and open-source tool to encrypt and decrypt files and folders. In fact, encFS generates a virtual encrypted filesystem.

To install encFS:

sudo apt-get install encfs -y

To configure encFS:

mkdir -p /encrypt

mkdir -p /decrypt

encfs /encrypt /decrypt

Here, you’ll be prompted for a password, set it.

Then type:

mount

cd /decrypt

Once you’re in the decrypt directory, add the files you want. Then unmount.

fusermount -u /decrypt

If you want to mount again later on, then type:

encfs /encrypt /decrypt

There are probably other encryption tools, but these are some of the most well-known ones.

Hiding Files and Folders

Hiding a file or a folder in Linux means that it will become invisible to the naked eye and other users. In such cases, only the creator can seek the file accurately enough.

Method #1: Adding a dot

Be it files or folders, you can add a dot in front of them and cause them to become partially invisible. What this means is that in such cases, the file will still be visible when the “ls -la” command is issued, but for those simply exploring the folder, it won’t be visible.

Ex:

# adding a dot in front of a file

mv myfile.txt .myfile.txt

# adding a dot in front of a folder

mv myfolder .myfolder

mvdot

Method #2: Tilda

The next method is to use a tilda (~).

Ex:

# adding a tilda in front of a file

mv myfile.txt ~/myfile.txt

# adding a tilda in front of a folder

mv myfolder ~/myfolder

Method #3: Nautilus-hide

Nautilus Hide is a free and open-source python extension to the Nautilus File Manager that basically hides a file or folder without renaming it. It uses .hidden folders to hide said files. Further, the best part about nautilus-hide is that files can be hidden with the click of a button. There’s no need for lots of code or even renaming anything; you simply click on the hide button, and the file is well hidden.

It does have a few dependencies which you must install:

- cmake

- gettet

- python-nautilus

- xdotool

- nautilus

You can install all of the following via:

sudo apt-get install cmake gettext xdotool python3-nautilus nautilus -y

Then, let’s install nautilus-hide manually:

git clone https://github.com/brunonova/nautilus-hide.git

mkdir build

cd build

cmake [path to nautilus hide]

make

sudo make install

Restart nautilus

:

nautilus -q

Alternatively, you can also install it via command line:

sudo apt-get install nautilus-hide

Make sure that you restart your computer once the process has been completed. Now, please navigate to the file that you want to hide and RIGHT CLICK on it. It should give you an option to hide the files.

To view your files, you simply press Contorl+H (which will unhide the files). Pressing Control+H again will hide it again.

Protecting files is sometimes the only option to safeguard them. However, when you really want to protect your files from being seen, you can also resort to hiding them in plain sight. Much like a 2 step authentication, this 2 step process should keep your files fairly safe. In this tutorial, we learned about (I) protecting files and (ii) about hiding them in plain sight. We saw that there are many ways to carry these two steps out.

Happy Coding!

About the author

Kalyani Rajalingham

I'm a linux and code lover.