I am trying to write an alias that puts a folder and its subfolders/files into an encrypted archive titled with the date the command is run. Preferably in format "YYYYMMDD FolderName Backup".

E.g. replace YYYYMMDD with 20150707 or whatever day the command is called.

I know how to set an alias by editing/creating the .bash_aliases file in the Home folder and adding a line like this:

alias cryptdoc="cd ~/Desktop/ && 7z a -pSome_Pass -r ~/Desktop/YYYYMMDD_Documents_Backup.7z' '/home/location/Documents/'"

A few quick notes on the code above:

  1. alias cryptdoc= sets up the alias.
  2. The quotes are included in case I have some directory with a space in the name so that I can write a command like "cd '~/My Documents/Folderhere'" which I might need to work without messing up the command.
  3. cd ~/Desktop/ is because I want the file to pop out on the desktop
  4. && is included to make sure I can do the first command and then the second as long as the first one works.
  5. 7z because it is better, a to add a file to an archive and -p to include a password within the command of my choosing. Keep in mind if you add a password after -p that it should look like this: -psomepasswordthatdoesntlookrightbutis. -r is because I want it recursive through my folders and files within the main folder.

I also realize that this means there will be a password in plain text on my machine in the alias file. But if someone has access to the alias file they also have access to my folder with these files in it anyway so it becomes irrelevant.

This should let me just go to terminal and type in cryptdoc and hit enter in order to create a 7-Zip file encrypted with my chosen password all ready for upload to some less-secure cloud storage. But How can I add a variable that actually grabs the YYYYMMDD at the time of typing cryptdoc into terminal and inserts it into the title of the document?


This should work fine for you. The date command is all that is needed.

alias cryptodoc="cd ~/Desktop/ && 7z a -pSome_Pass -r ~/Desktop/$(date +%Y%m%d)_Documents_Backup.7z ~/Documents/* 2>/dev/null"

the command for date +%Y%m%d means, run date, then + means format, then %Y means YYYY, %m means MM and %d means DD. Having the $( ) around the date command means to run it at the time of the command.

Hope this helps!

| improve this answer | |
  • You should use $(date +%Y%m%d), the back-ticks are deprecated. – A.B. Jul 8 '15 at 6:26
  • @A.B. Old habits die hard! =) Updated answer. – Terrance Jul 8 '15 at 6:36
  • Worked perfectly but what is the * for? I just left that off and replaced it with my path. – Fernando Jul 8 '15 at 13:34
  • @Fernando the * is a wildcard for all files in that folder. – Terrance Jul 8 '15 at 13:36
  • Got it! It was working but when I try to use it with the documents folder it starts but doesn't actually finish anything and then when I escape it throws a /home/fer/.gvfs/: WARNING: Permission denied WARNINGS for files: /home/fer/.gvfs/ : Permission denied – Fernando Jul 8 '15 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.