Created
May 2, 2017 18:39
-
-
Save gregorthebigmac/9d91e0ab822e3a330fb7d57e90da1ec8 to your computer and use it in GitHub Desktop.
A simple automation of Linux tar, using default settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This little app is meant to be run from your current working directory. | |
Output looks like this, if you compile and drop the bin in your user's bin folder: | |
[email protected] [~]# tard | |
Displaying contents of current directory: | |
/home/[user]: | |
file1.txt dir1 file2.txt dir2 file3.txt | |
file4.txt dir3 file5.txt dir4 file6.txt | |
[you get the idea. it's just running the "ls" command] | |
What file(s)/dir(s) would you like to tar? (type "quit" to exit) | |
>dir1 | |
You chose [dir1]. Good? y/n | |
>y | |
Done? y/n | |
>n | |
What file(s)/dir(s) would you like to tar? (type "quit" to exit) | |
>dir2 | |
You chose [dir2]. Good? y/n | |
>y | |
Done? y/n | |
>y | |
Use default name? y/n | |
>y | |
Okay, I'm making your tar file now. One moment... | |
[terminal output showing all the files going into your tar file] | |
Done. Your new file is dir1.tar.gz. | |
[email protected] [~]# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <unistd.h> | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
std::string directory_check(); | |
bool user_binary_check(std::string phrase); | |
bool make_tar_file(std::string target_name); | |
std::vector<std::string> u_files; | |
int main() | |
{ | |
system("clear"); | |
bool done = false; | |
std::string filename; | |
while (!done) { | |
std::string path_final = directory_check(); | |
cout << "What DIR(s) would you like to tar? (type \"quit\" to exit)" << endl; | |
std::getline(cin, filename); | |
if (filename.compare("Quit") == 0 || filename.compare("quit") == 0) { | |
done = true; | |
break; | |
} | |
cout << "You chose [" << filename << "]."; | |
bool move_on = user_binary_check("Good"); | |
if (move_on) { | |
u_files.push_back(filename); | |
move_on = user_binary_check("Done"); | |
if (move_on) { | |
done = true; | |
}}} | |
if (filename.compare("Quit") == 0 || filename.compare("quit") == 0) { | |
return 0; | |
} | |
done = false; | |
while (!done) { | |
std::string target_name; | |
bool default_name = user_binary_check("Use default name"); | |
if (default_name) { | |
target_name = u_files[0]; | |
int pos = target_name.find("."); | |
if (pos != std::string::npos) { | |
target_name.erase(pos); | |
} | |
done = make_tar_file(target_name); | |
cout << endl << "Done. Your new file is " << target_name << ".tar.gz" << endl; | |
} | |
else { | |
cout << "What do you want to call the tar file? (don't enter file extension. I'll handle that)." << endl; | |
std::getline(cin, target_name); | |
cout << "You want to call the file [" << target_name << "]." << endl; | |
bool move_on = user_binary_check("Correct"); | |
if (move_on) { | |
done = make_tar_file(target_name); | |
cout << endl << "Done. Your new file is " << target_name << ".tar.gz" << endl; | |
} | |
} | |
} | |
return 0; | |
} | |
std::string directory_check() { | |
char the_path[256]; | |
getcwd(the_path, 255); | |
std::string path_final(the_path); | |
cout << "Displaying contents of current directory:" << endl; | |
cout << path_final << ":" << endl; | |
cout << system("ls") << endl; | |
return path_final; | |
} | |
bool user_binary_check(std::string phrase) { | |
cout << phrase << "? y/n" << endl; | |
std::string quit; | |
std::getline(cin, quit); | |
if (quit[0] == 'y' || quit[0] == 'Y') { | |
return true; | |
} | |
else return false; | |
} | |
bool make_tar_file(std::string target_name) { | |
cout << "Okay, I'm making your tar file now. One moment..." << endl; | |
std::string tar_command = "tar cvzf " + target_name + ".tar.gz"; | |
for (int i = 0; i < u_files.size(); i++) { | |
tar_command = tar_command + " " + u_files[i]; | |
} | |
cout << endl; | |
system(tar_command.c_str()); // tar command goes here with arguments | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment