Last active
June 23, 2018 00:36
-
-
Save Paulmicha/5052787 to your computer and use it in GitHub Desktop.
Command-line / Shell / Bash Scripting - Snippets, Notes & Whatnot
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
#!/bin/bash | |
# -*- coding: UTF8 -*- | |
## | |
# Command-line / Shell / Bash - snippets : Cron-related snippets | |
# Tested on Debian 6 ("Squeeze") | |
# | |
# Sources : | |
# https://drupal.org/node/23714 | |
# http://www.linuxquestions.org/questions/linux-general-1/crontab-every-10-minutes-117651/ | |
# | |
# Edit global config | |
nano /etc/crontab | |
# Per-user alternative | |
crontab -e | |
# Example Contents : | |
# /etc/crontab: system-wide crontab | |
# Unlike any other crontab you don't have to run the `crontab' | |
# command to install the new version when you edit this file | |
# and files in /etc/cron.d. These files also have username fields, | |
# that none of the other crontabs do. | |
SHELL=/bin/sh | |
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
# m h dom mon dow user command | |
17 * * * * root cd / && run-parts --report /etc/cron.hourly | |
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) | |
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) | |
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly ) | |
# | |
# Timing setup notes : | |
# +---------------- minute (0 - 59) | |
# | +------------- hour (0 - 23) | |
# | | +---------- day of month (1 - 31) | |
# | | | +------- month (1 - 12) | |
# | | | | +---- day of week (0 - 6) (Sunday=0) | |
# | | | | | | |
# * * * * * user command_to_be_executed | |
# Every X minutes, or hours, or whatever, is noted : | |
# */X | |
# Example Drupal cron - using Drush, every hour at 00 min | |
0 * * * * root cd /var/www/the-drupal-project-folder/ && drush cron | |
# Example Drupal cron - using Curl, every hour at 30 min | |
30 * * * * root curl -s http://www.the-drupal-project.com/cron.php | |
# Example Drupal cron - using Lynx, every 10 minutes | |
*/10 * * * * root /usr/bin/lynx -source http://www.the-drupal-project.com/cron.php | |
# Setup a job to execute at reboot | |
# (to add to the crontab file) | |
@reboot /path/to/starter.sh | |
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
#!/bin/bash | |
# -*- coding: UTF8 -*- | |
## | |
# Drupal 6 Permissions | |
# | |
# Sources : | |
# https://drupal.org/node/244924 | |
# | |
VERSION="1.0-dev" | |
SCRIPT_NAME="$(basename ${0})" | |
## | |
# Usage info | |
# | |
function usage { | |
echo "Set ownership & permissions for files & folders (for Drupal 6) v${VERSION} | |
Usage : | |
./${SCRIPT_NAME} [ owner ] [ group ] [ path ] | |
" | |
exit 1 | |
} | |
# Param 1 : owner | |
# default : current user | |
P_OWNER=${1} | |
if [ -z "${1}" ]; then | |
P_OWNER="$USER" | |
fi | |
# Param 2 : group | |
# default : www-data | |
P_GROUP=${2} | |
if [ -z "${2}" ]; then | |
P_GROUP="www-data" | |
fi | |
# Param 3 : path | |
# default : . | |
P_PATH=${3} | |
if [ -z "${3}" ]; then | |
P_PATH="." | |
fi | |
# debug ok 2014/06/20 16:06:18 | |
#echo "debug param 1 = ${P_OWNER}" | |
#echo "debug param 2 = ${P_GROUP}" | |
#echo "debug param 3 = ${P_PATH}" | |
# Current dir (or replace '.' below) | |
cd $P_PATH | |
# Owner/group | |
chown $P_OWNER:$P_GROUP . -R | |
# Non-writeable files | |
find . -type f -exec chmod 640 {} + | |
# Non-writeable dirs | |
find . -type d -exec chmod 750 {} + | |
# Writeable files (Drupal6 example dir structure) | |
find . -type f -wholename "*sites/default/files*" -exec chmod 660 {} + | |
# (Boost file cache) | |
find . -type f -wholename "*cache/normal*" -exec chmod 660 {} + | |
find . -type f -wholename "*cache/perm*" -exec chmod 660 {} + | |
# Writeable dirs (Drupal6 example dir structure) | |
find . -type d -wholename "*sites/default/files*" -exec chmod 770 {} + | |
# (Boost file cache) | |
find . -type d -wholename "*cache/normal*" -exec chmod 770 {} + | |
find . -type d -wholename "*cache/perm*" -exec chmod 770 {} + | |
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
#!/bin/bash | |
# -*- coding: UTF8 -*- | |
## | |
# Drupal 7 Permissions | |
# | |
# Sources : | |
# https://drupal.org/node/244924 | |
# | |
VERSION="1.0-dev" | |
SCRIPT_NAME="$(basename ${0})" | |
## | |
# Usage info | |
# | |
function usage { | |
echo "Set ownership & permissions for files & folders for Drupal 7 v${VERSION} | |
Usage : | |
./${SCRIPT_NAME} [ owner ] [ group ] [ path ] | |
" | |
exit 1 | |
} | |
# Param 1 : owner | |
# default : current user | |
P_OWNER=${1} | |
if [ -z "${1}" ]; then | |
P_OWNER="$USER" | |
fi | |
# Param 2 : group | |
# default : www-data | |
P_GROUP=${2} | |
if [ -z "${2}" ]; then | |
P_GROUP="www-data" | |
fi | |
# Param 3 : path | |
# default : . | |
P_PATH=${3} | |
if [ -z "${3}" ]; then | |
P_PATH="." | |
fi | |
# debug ok 2014/06/19 04:10:10 | |
#echo "debug param 1 = ${P_OWNER}" | |
#echo "debug param 2 = ${P_GROUP}" | |
#echo "debug param 3 = ${P_PATH}" | |
# Current dir (or replace '.' below) | |
cd $P_PATH | |
# Owner/group | |
chown $P_OWNER:$P_GROUP . -R | |
# Non-writeable files | |
find . -type f -exec chmod 640 {} + | |
# Non-writeable dirs | |
find . -type d -exec chmod 750 {} + | |
# Writeable files (Drupal7 example dir structure) | |
find . -type f -wholename "*sites/default/files*" -exec chmod 660 {} + | |
find . -type f -wholename "*sites/default/tmp*" -exec chmod 660 {} + | |
find . -type f -wholename "*sites/default/private*" -exec chmod 660 {} + | |
# Writeable dirs (Drupal7 example dir structure) | |
find . -type d -wholename "*sites/default/files*" -exec chmod 770 {} + | |
find . -type d -wholename "*sites/default/tmp*" -exec chmod 770 {} + | |
find . -type d -wholename "*sites/default/private*" -exec chmod 770 {} + | |
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
#!/bin/bash | |
# -*- coding: UTF8 -*- | |
## | |
# Command-line / Shell / Bash - snippets : Manage Apache "htaccess & htpasswd" - based access restriction | |
# Tested 2013/12/04 13:18:49 on Debian 6 ("Squeeze") | |
# | |
#------------------------------------------------------------------------------------------ | |
# .htaccess or Apache settings contents | |
Order allow,deny | |
Allow from all | |
Options None | |
AuthUserFile /path/to/htpasswd-file | |
AuthName "Title of Authentication" | |
AuthType Basic | |
require valid-user | |
#------------------------------------------------------------------------------------------ | |
# .htpasswd creation | |
# Syntax : | |
htpasswd -bc /path/to/htpasswd-file UserName UserPassword | |
#------------------------------------------------------------------------------------------ | |
# Alternative : only apply for an IP | |
# Note : using 127.0.0.1 or "localhost" does NOT work :( | |
# @see http://stackoverflow.com/questions/8854909/apache-authentication-except-localhost | |
# permit by USER || IP | |
Satisfy any | |
# USER | |
AuthUserFile /var/www/munin/.htpasswd | |
AuthGroupFile /dev/null | |
AuthName "Password Protected Area" | |
AuthType Basic | |
require valid-user | |
# IP | |
order deny,allow | |
deny from all | |
allow from 123.123.123.123 | |
#------------------------------------------------------------------------------------------ | |
# Reference | |
# htpasswd command : | |
#htpasswd [-cmdpsD] passwordfile username | |
#htpasswd -b[cmdpsD] passwordfile username password | |
#htpasswd -n[mdps] username | |
#htpasswd -nb[mdps] username password | |
# -c Create a new file. | |
# -n Don't update file; display results on stdout. | |
# -m Force MD5 encryption of the password. | |
# -d Force CRYPT encryption of the password (default). | |
# -p Do not encrypt the password (plaintext). | |
# -s Force SHA encryption of the password. | |
# -b Use the password from the command line rather than prompting for it. | |
# -D Delete the specified user. | |
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
#!/bin/bash | |
# -*- coding: UTF8 -*- | |
## | |
# Command-line / Shell / Bash - snippets : List all normal user and system accounts in the system. | |
# Tested on RHEL / Debian Linux | |
# | |
# Author: Vivek Gite <www.cyberciti.biz>, under GPL v2.0+ | |
# | |
# Sources : | |
# http://www.cyberciti.biz/faq/linux-list-users-command/ | |
# | |
_l="/etc/login.defs" | |
_p="/etc/passwd" | |
## get mini UID limit ## | |
l=$(grep "^UID_MIN" $_l) | |
## get max UID limit ## | |
l1=$(grep "^UID_MAX" $_l) | |
## use awk to print if UID >= $MIN and UID <= $MAX and shell is not /sbin/nologin ## | |
echo "----------[ Normal User Accounts ]---------------" | |
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max && $7 != "/sbin/nologin" ) print $0 }' "$_p" | |
echo "" | |
echo "----------[ System User Accounts ]---------------" | |
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( !($3 >= min && $3 <= max && $7 != "/sbin/nologin")) print $0 }' "$_p" |
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
#!/bin/bash | |
# -*- coding: UTF8 -*- | |
## | |
# Command-line / Shell / Bash - snippets : Miscellaneous | |
# | |
# Tested in : | |
# Debian 7 "wheezy" | |
# Ubuntu 14.04 LTS "trusty" | |
# | |
# Sources : | |
# http://serverfault.com/questions/490825/how-to-set-the-domain-name-on-gnu-linux | |
# http://askubuntu.com/questions/151941/how-can-you-completely-remove-a-package | |
# http://askubuntu.com/questions/254826/how-to-force-a-clock-update-using-ntp | |
# | |
#--------------------------------------------------------------------- | |
# Get current linux version | |
lsb_release -a | |
#--------------------------------------------------------------------- | |
# Change server's hostname | |
# (installed domain name) | |
# (content of this file is the domain) | |
nano /etc/hostname | |
# Activate hostname | |
hostname -F /etc/hostname | |
# Add domain name and address to the server | |
# (in this file, replace old domain with desired one) | |
nano /etc/hosts | |
#--------------------------------------------------------------------- | |
# List all active sockets / connexions (only servers) | |
netstat -l | |
#--------------------------------------------------------------------- | |
# Remove package | |
# Along with with non-manual dependencies | |
# (will not remove non-systemwide configuration files) | |
# @see http://askubuntu.com/questions/151941/how-can-you-completely-remove-a-package | |
apt-get --purge autoremove | |
#--------------------------------------------------------------------- | |
# Force time update | |
ntpdate -sb time.nist.gov | |
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
#!/bin/bash | |
# -*- coding: UTF8 -*- | |
## | |
# Unix Permissions Basics & snippets | |
# (Command-line / Shell / Bash) | |
# | |
# Tested on : | |
# Debian 7 "wheezy" | |
# Unbuntu 14.04 "trusty" | |
# | |
# Sources : | |
# http://www.tldp.org/LDP/abs/html/invoking.html | |
# http://www.yolinux.com/TUTORIALS/LinuxTutorialManagingGroups.html | |
# https://www.digitalocean.com/community/tutorials/linux-permissions-basics-and-how-to-use-umask-on-a-vps | |
# | |
#---------------------------------------------------------------------------------------- | |
# Disambiguation | |
# User name : human-readable name of a Linux user | |
# OR : "machine" name of a Linux user (usually, a lowercase version of the human-readable name), also = user login | |
# User ID : user login | |
# OR : unique numerical reference to the user | |
# (same goes for group id and group name) | |
#---------------------------------------------------------------------------------------- | |
# Explanations | |
# On Linux : | |
# 1. Each file is owned by exactly one user and one group. | |
# 2. Users may belong to several groups, and are automatically assigned a User Private Group (UPG) | |
# which is a unique group of the same name as the user. | |
# 3. Configuring services/programs to operate AS a distinct user allows to control their permissions. | |
# 4. Permission to read a directory also requires execution permission. | |
# Notations : | |
# Description = Abreviation = Octal Code | |
Read = r = 4 | |
Write = w = 2 | |
Execute = x = 1 | |
Read and Execute = rx = 5 | |
Read and Write = rw = 6 | |
Read, Write and Execute = rwx = 7 | |
#---------------------------------------------------------------------------------------- | |
# Utilities | |
# List users | |
cat /etc/passwd | |
# List groups | |
groups | |
# List groups | |
groups user_id | |
# Set ownership | |
chown user_id:group_id [-R] | |
# Set permissions | |
chmod [octal] file/or/directory [-R] | |
# Change specific permissions | |
# @see http://man.yolinux.com/cgi-bin/man2html?cgi_command=chmod | |
chmod [ugoa][+-=][rwxXst] file/or/directory [-R] | |
#---------------------------------------------------------------------------------------- | |
# Snippets | |
# Give everyone read/execute permission | |
chmod 555 my_filename | |
chmod +rx my_filename | |
# Give just the script owner read/execute permission | |
chmod u+rx my_filename | |
# Execute for all files in current dir & subdirs | |
find . -type f -exec chmod 640 {} + | |
# Restrict by path (matching by pattern) | |
find . -type f -wholename "*sites/default/files*" -exec chmod 660 {} + | |
# Execute for all dirs in current dir & subdirs | |
find . -type d -exec chmod 750 {} + | |
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
#!/bin/bash | |
# -*- coding: UTF8 -*- | |
## | |
# Drupal 7 Permissions - PHP-FPM nginx Ajenti setup | |
# | |
# Sources : | |
# https://drupal.org/node/244924 | |
# | |
VERSION="1.0-dev" | |
SCRIPT_NAME="$(basename ${0})" | |
## | |
# Usage info | |
# | |
function usage { | |
echo "Set ownership & permissions for files & folders for Drupal 7 v${VERSION} | |
Usage : | |
./${SCRIPT_NAME} [ owner ] [ group ] [ path ] | |
" | |
exit 1 | |
} | |
# Param 1 : owner | |
# default : current user | |
P_OWNER=${1} | |
if [ -z "${1}" ]; then | |
P_OWNER="$USER" | |
fi | |
# Param 2 : group | |
# default : www-data | |
P_GROUP=${2} | |
if [ -z "${2}" ]; then | |
P_GROUP="www-data" | |
fi | |
# Param 3 : path | |
# default : . | |
P_PATH=${3} | |
if [ -z "${3}" ]; then | |
P_PATH="." | |
fi | |
# debug ok 2014/06/19 04:10:10 | |
#echo "debug param 1 = ${P_OWNER}" | |
#echo "debug param 2 = ${P_GROUP}" | |
#echo "debug param 3 = ${P_PATH}" | |
# Current dir (or replace '.' below) | |
cd $P_PATH | |
# Owner/group | |
chown $P_OWNER:$P_GROUP . -R | |
# Non-writeable files | |
find . -type f -exec chmod 750 {} + | |
# Non-writeable dirs | |
find . -type d -exec chmod 750 {} + | |
# Writeable files (Drupal7 example dir structure) | |
find . -type f -wholename "*sites/default/files*" -exec chmod 660 {} + | |
find . -type f -wholename "*sites/default/tmp*" -exec chmod 660 {} + | |
find . -type f -wholename "*sites/default/private*" -exec chmod 660 {} + | |
# Writeable dirs (Drupal7 example dir structure) | |
find . -type d -wholename "*sites/default/files*" -exec chmod 770 {} + | |
find . -type d -wholename "*sites/default/tmp*" -exec chmod 770 {} + | |
find . -type d -wholename "*sites/default/private*" -exec chmod 770 {} + | |
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
#!/bin/bash | |
# -*- coding: UTF8 -*- | |
## | |
# Command-line / Shell / Bash - snippets : PostgreSQL 8.4.17 | |
# | |
# Tested on Debian 6 ("Squeeze") | |
# | |
# Sources : | |
# http://www.postgresql.org/docs/8.4/static/libpq-pgpass.html | |
# http://stackoverflow.com/questions/1348126/modify-owner-on-all-tables-simultaneously-in-postgresql | |
# http://stackoverflow.com/questions/13358973/how-to-run-a-sql-command-from-a-bash-variable-via-ssh-and-psql | |
# http://stackoverflow.com/questions/12232640/in-postgresql-whats-the-difference-a-database-and-a-relation-error-rela | |
# http://stackoverflow.com/questions/11599533/postgresql-8-4-grant-dml-privileges-on-all-tables-to-a-role | |
# | |
#----------------------------------------------------------------------------------- | |
# Prepare server for using commands like "pg_dump", | |
# without being prompted for a password every time. | |
# @see http://www.postgresql.org/docs/8.4/static/libpq-pgpass.html | |
# format : "hostname:port:database:username:password" | |
# default port is 5432 | |
# -> ex : | |
echo 'localhost:5432:DB_NAME_A:USER_NAME_A:PASSWORD_A | |
localhost:5432:DB_NAME_B:USER_NAME_B:PASSWORD_B | |
localhost:5432:DB_NAME_C:USER_NAME_C:PASSWORD_C | |
' > ~/.pgpass | |
chmod 0600 ~/.pgpass | |
#----------------------------------------------------------------------------------- | |
# Make DB Dump (tgz compressed) | |
# @todo auto datestamp in filename - e.g. MY_FILE_NAME-$(date +%F-%H-%M-%S).sql.tar | |
pg_dump -i -U USER_NAME -h localhost -F t DB_NAME > MY_FILE_NAME.sql.tar | |
tar czf MY_FILE_NAME.sql.tar.gz MY_FILE_NAME.sql.tar | |
rm MY_FILE_NAME.sql.tar | |
#----------------------------------------------------------------------------------- | |
# Restore DB Dump (tgz compressed) | |
tar xzf MY_FILE_NAME.sql.tar.tgz | |
pg_restore --username "USER_NAME" --dbname "DB_NAME" -h localhost --verbose --ignore-version MY_FILE_NAME.sql.tar | |
rm MY_FILE_NAME.sql.tar | |
#----------------------------------------------------------------------------------- | |
# Restore PgSQL Dump (NOT compressed : "plain" restore) | |
# Ex. when logged in as root | |
su - postgres | |
psql DB_NAME -f MY_FILE_NAME.sql | |
# Setting schema owner | |
# (if inside a terminal pgsql, exit first by typing "\q", or just paste the query) | |
# @see http://stackoverflow.com/questions/13358973/how-to-run-a-sql-command-from-a-bash-variable-via-ssh-and-psql | |
psql DB_NAME -c "ALTER SCHEMA my_shema_name OWNER TO my_owner_name" | |
#----------------------------------------------------------------------------------- | |
# Fixing table owner problems | |
# Ex. when logged in as root | |
# @see http://stackoverflow.com/questions/1348126/modify-owner-on-all-tables-simultaneously-in-postgresql | |
su - postgres | |
for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'SCHEMA_NAME';" DB_NAME` ; do psql -c "alter table SCHEMA_NAME.$tbl owner to OWNER_NAME" DB_NAME ; done | |
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
#!/bin/bash | |
# -*- coding: UTF8 -*- | |
## | |
# Command-line / Shell / Bash - snippets : the "screen" command | |
# Tested on Debian 6 ("Squeeze") | |
# | |
# Contents | |
# 1. Concept | |
# 2. Usage | |
# 3. Documentation / Reference (help) | |
# | |
# Sources : | |
# http://www.thegeekstuff.com/2010/07/screen-command-examples/ | |
# | |
#---------------------------------------------------- | |
# 1. Concept | |
# "screen" is to the command-line what tabs are to web browsers. | |
# Feels like using wormholes to "encapsulate" several terminals from the one currently in use. | |
# Essentially used for parallel execution / mutli-tasking inside a single terminal. | |
# Like "nohup", but allowing to navigate from one "screened" command to another (like tabs). | |
#---------------------------------------------------- | |
# 2. Usage | |
# Send a command in a screen | |
# "open new tab" | |
screen unix-command-to-be-executed | |
# same as | |
screen ./unix-shell-script-to-be-executed | |
# List screens running - get their id | |
# "list opened tabs" | |
screen -ls | |
# Go back 'home' | |
# "switch tab" | |
# CTRL+A followed by d | |
# Kill a screened process (detach) | |
# "close tab" | |
screen -d <pid>.<tty>.<host> | |
# Go to a screened process (reattach) | |
# "switch tab" | |
screen -r <pid>.<tty>.<host> | |
#---------------------------------------------------- | |
# 3. Documentation / Reference (help) | |
screen -h | |
#Use: screen [-opts] [cmd [args]] | |
# or: screen -r [host.tty] | |
#Options: | |
#-a Force all capabilities into each window's termcap. | |
#-A -[r|R] Adapt all windows to the new display width & height. | |
#-c file Read configuration file instead of '.screenrc'. | |
#-d (-r) Detach the elsewhere running screen (and reattach here). | |
#-dmS name Start as daemon: Screen session in detached mode. | |
#-D (-r) Detach and logout remote (and reattach here). | |
#-D -RR Do whatever is needed to get a screen session. | |
#-e xy Change command characters. | |
#-f Flow control on, -fn = off, -fa = auto. | |
#-h lines Set the size of the scrollback history buffer. | |
#-i Interrupt output sooner when flow control is on. | |
#-l Login mode on (update /var/run/utmp), -ln = off. | |
#-list or -ls. Do nothing, just list our SockDir. | |
#-L Turn on output logging. | |
#-m ignore $STY variable, do create a new screen session. | |
#-O Choose optimal output rather than exact vt100 emulation. | |
#-p window Preselect the named window if it exists. | |
#-q Quiet startup. Exits with non-zero return code if unsuccessful. | |
#-r Reattach to a detached screen process. | |
#-R Reattach if possible, otherwise start a new session. | |
#-s shell Shell to execute rather than $SHELL. | |
#-S sockname Name this session <pid>.sockname instead of <pid>.<tty>.<host>. | |
#-t title Set title. (window's name). | |
#-T term Use term as $TERM for windows, rather than "screen". | |
#-U Tell screen to use UTF-8 encoding. | |
#-v Print "Screen version 4.00.03jw4 (FAU) 2-May-06". | |
#-wipe Do nothing, just clean up SockDir. | |
#-x Attach to a not detached screen. (Multi display mode). | |
#-X Execute <cmd> as a screen command in the specified session. | |
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
Control characters are not normally useful inside a script. | |
Ctl-A | |
Moves cursor to beginning of line of text (on the command-line). | |
Ctl-B | |
Backspace (nondestructive). | |
Ctl-C | |
Break. Terminate a foreground job. | |
Ctl-D | |
Log out from a shell (similar to exit). | |
EOF (end-of-file). This also terminates input from stdin. | |
When typing text on the console or in an xterm window, Ctl-D erases the character under the cursor. When there are no characters present, Ctl-D logs out of the session, as expected. In an xterm window, this has the effect of closing the window. | |
Ctl-E | |
Moves cursor to end of line of text (on the command-line). | |
Ctl-F | |
Moves cursor forward one character position (on the command-line). | |
Ctl-G | |
BEL. On some old-time teletype terminals, this would actually ring a bell. In an xterm it might beep. | |
Ctl-H | |
Rubout (destructive backspace). Erases characters the cursor backs over while backspacing. | |
#!/bin/bash | |
# Embedding Ctl-H in a string. | |
a="^H^H" # Two Ctl-H's -- backspaces | |
# ctl-V ctl-H, using vi/vim | |
echo "abcdef" # abcdef | |
echo | |
echo -n "abcdef$a " # abcd f | |
# Space at end ^ ^ Backspaces twice. | |
echo | |
echo -n "abcdef$a" # abcdef | |
# No space at end ^ Doesn't backspace (why?). | |
# Results may not be quite as expected. | |
echo; echo | |
# Constantin Hagemeier suggests trying: | |
# a=$'\010\010' | |
# a=$'\b\b' | |
# a=$'\x08\x08' | |
# But, this does not change the results. | |
######################################## | |
# Now, try this. | |
rubout="^H^H^H^H^H" # 5 x Ctl-H. | |
echo -n "12345678" | |
sleep 2 | |
echo -n "$rubout" | |
sleep 2 | |
Ctl-I | |
Horizontal tab. | |
Ctl-J | |
Newline (line feed). In a script, may also be expressed in octal notation -- '\012' or in hexadecimal -- '\x0a'. | |
Ctl-K | |
Vertical tab. | |
When typing text on the console or in an xterm window, Ctl-K erases from the character under the cursor to end of line. Within a script, Ctl-K may behave differently, as in Lee Lee Maschmeyer's example, below. | |
Ctl-L | |
Formfeed (clear the terminal screen). In a terminal, this has the same effect as the clear command. When sent to a printer, a Ctl-L causes an advance to end of the paper sheet. | |
Ctl-M | |
Carriage return. | |
#!/bin/bash | |
# Thank you, Lee Maschmeyer, for this example. | |
read -n 1 -s -p \ | |
$'Control-M leaves cursor at beginning of this line. Press Enter. \x0d' | |
# Of course, '0d' is the hex equivalent of Control-M. | |
echo >&2 # The '-s' makes anything typed silent, | |
#+ so it is necessary to go to new line explicitly. | |
read -n 1 -s -p $'Control-J leaves cursor on next line. \x0a' | |
# '0a' is the hex equivalent of Control-J, linefeed. | |
echo >&2 | |
### | |
read -n 1 -s -p $'And Control-K\x0bgoes straight down.' | |
echo >&2 # Control-K is vertical tab. | |
# A better example of the effect of a vertical tab is: | |
var=$'\x0aThis is the bottom line\x0bThis is the top line\x0a' | |
echo "$var" | |
# This works the same way as the above example. However: | |
echo "$var" | col | |
# This causes the right end of the line to be higher than the left end. | |
# It also explains why we started and ended with a line feed -- | |
#+ to avoid a garbled screen. | |
# As Lee Maschmeyer explains: | |
# -------------------------- | |
# In the [first vertical tab example] . . . the vertical tab | |
#+ makes the printing go straight down without a carriage return. | |
# This is true only on devices, such as the Linux console, | |
#+ that can't go "backward." | |
# The real purpose of VT is to go straight UP, not down. | |
# It can be used to print superscripts on a printer. | |
# The col utility can be used to emulate the proper behavior of VT. | |
exit 0 | |
Ctl-N | |
Erases a line of text recalled from history buffer [8] (on the command-line). | |
Ctl-O | |
Issues a newline (on the command-line). | |
Ctl-P | |
Recalls last command from history buffer (on the command-line). | |
Ctl-Q | |
Resume (XON). | |
This resumes stdin in a terminal. | |
Ctl-R | |
Backwards search for text in history buffer (on the command-line). | |
Ctl-S | |
Suspend (XOFF). | |
This freezes stdin in a terminal. (Use Ctl-Q to restore input.) | |
Ctl-T | |
Reverses the position of the character the cursor is on with the previous character (on the command-line). | |
Ctl-U | |
Erase a line of input, from the cursor backward to beginning of line. In some settings, Ctl-U erases the entire line of input, regardless of cursor position. | |
Ctl-V | |
When inputting text, Ctl-V permits inserting control characters. For example, the following two are equivalent: | |
echo -e '\x0a' | |
echo <Ctl-V><Ctl-J> | |
Ctl-V is primarily useful from within a text editor. | |
Ctl-W | |
When typing text on the console or in an xterm window, Ctl-W erases from the character under the cursor backwards to the first instance of whitespace. In some settings, Ctl-W erases backwards to first non-alphanumeric character. | |
Ctl-X | |
In certain word processing programs, Cuts highlighted text and copies to clipboard. | |
Ctl-Y | |
Pastes back text previously erased (with Ctl-U or Ctl-W). | |
Ctl-Z | |
Pauses a foreground job. | |
Substitute operation in certain word processing applications. | |
EOF (end-of-file) character in the MSDOS filesystem. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment