Shell Script To Encrypt Any Text File

In cryptography, encryption is the process of transforming information (referred to as plaintext) using an algorithm (called cipher) to make it unreadable to anyone except those possessing special knowledge, usually referred to as a key.

Mcrypt is a simple crypting program, a replacement for the old unix crypt(1). When encrypting or decrypting a file, a new file is created with the extension .nc and mode 0600. The new file keeps the modification date of the original. The original file may be deleted by specifying the -u parameter. If no files are specified, the standard input is encrypted to the standard output.

Sample Shell Script Wrapper To Encrypt Any Text File

# Write a shell script to encrypt any text file
# -------------------------------------------------------------------------
# Copyright (c) 2001 nixCraft project <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# -------------------------------------------------------------------------
echo -n "Enter a file name : "
read file
if [ ! -f $file ]
	echo "$file not a file!"
	exit 1
echo -n "Enter a Password : "
read password
# do encryption using UNIX crypt command
# this command will prompt for a password
crypt $password < $file > $file.cpy
echo  "$file.cpy created as encrypted file"
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:
Category List of Unix and Linux commands
Disk space analyzers df ncdu pydf
File Management cat tree
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities NetHogs dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • avinash Feb 5, 2012 @ 20:43

    can u please give me a script file for squid server configuration,
    in which the user have the privilege to input the access list.
    pleease upload it soon.

  • FARAZ AHMAD Apr 6, 2011 @ 13:59

    Thanx I want to create text file having multiple data and to print it.

  • dileep Sep 21, 2010 @ 13:04

    if any one tell the scripts to encrypt the password while adding multiple user…

  • scream May 24, 2009 @ 17:05

    if i encrypt squid.conf file, will system able to read encrypted file?

  • bassam Nov 18, 2008 @ 2:38

    please how i can install crypt ?
    i type crypt and tap he give me cryptsetup!!

    can u please help us what is this mean?
    thank you
    good job

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.