Almost all are not able to remember all the shortcuts that are available for the vi or vim editor. I will call it vim throughout this article. There are several cheatsheets out there, but the one that works for me is the one provided by rtorr in Github. I use the cheatsheet often. I suggest that you start small. At the very least learn how to navigate in vim. Once you feel comfortable doing that, you can learn other advanced shortcuts such as editing, search and replace, working with multiple files and many much more. I hope you’ll find the cheatsheet as helpful as it has done for me. Again if you missed the link above, I posted it below.
SFTP Command Line
Here are command lines you can within SFTP.
Commands available from the local client.
lls lcd lmkdir lpwd lumask
Commands available on remote server.
cd chmod chown exit get help ln ls mkdir put pwd rename rm rmdir version !
I decided to log my commands for good record keeping. Here’s my script.
#!/bin/bash if [[ $# -eq 0 ]] ; then echo 'No arguments. Format: ./go.sh script.sh'; exit 0; fi log=log.txt echo '-------------------------------' >> $log date >> $log echo '---' >> $log cat $1 >> $log echo '---' >> $log /bin/bash $1 2>&1 | tee -a $log
Line by line explanation.
- Checks for script to run. Exits if there’s no argument.
- Sets the log file.
- Prints dashes.
- Prints the date.
- Print 3 dashes.
- Prints the commands to the log file.
- Print 3 dashes.
- Run the command. Send error and standard out to log file.
Running MySQL commands from the Terminal
You can run MySQL commands from the terminal by using -e switch. Here are a few examples.
mysql -u username -p -e "create database mydb" mysql -u username -p -e "use mydb" mysql -u username -p database -e "select * from mytable"
If you have .mycnf configured, you can omit the username and password.
mysql -e "create database mydb" mysql -e "use mydb" mysql -e "select * from mytable"
To run multiple commands from a single line, separate the commands using a semicolon.
mysql -e "create database somedb; use mydb; select * from mytable;"
Ansible Run Commands
Typically you will run Ansible using playbooks, just like the playbook that I ran from the previous post to patch a bunch of servers all at once. In addition to running playbooks, you can also run single ad hoc commands to a server or to a group of servers. Below are a few examples of that.
First, here’s my /etc/ansible/hosts file.
[servers] server1 server2 [webservers] server3 server4 [appservers] server5 server6 [dbservers] server7 server8
Here are the commands.
ansible servers -m ping ansible webservers -m command -a "df -h" ansible appservers -m command -a "crontab -l" ansible dbservers -m command -a "cat /etc/hosts" ansible all -m command -a "cat /etc/passwd" ansible webservers -m command -a "systemctl status apache2" ansible appservers -m command -a 'uptime'
The first command runs ping on server1 and server2. The second command displays disk information of server3 and server4. The third command displays the crontab of server5 and server6, and so on and on. You get the drift.