Bash: How to replace a string in a directory recursively
Combine find, xargs and sed.
find . -name "*.css" | xargs sed -i 's/BEFORE/AFTER/g'
Combine find, xargs and sed.
find . -name "*.css" | xargs sed -i 's/BEFORE/AFTER/g'
Define a function echo_count and count the function name to be called in the script. You can refer to a variable ${TOTAL} .For example,
#!/bin/bash
export TOTAL=$(( $(grep '^echo_count' $0 | wc -l) ))
export COUNT=1
function echo_count () {
echo -n "($(( COUNT++ ))/${TOTAL}) $1"
}
echo
echo_count 'Processing one - Stopping Apache2... '
sudo service apache2 stop
echo 'Done'
echo_count 'Processing two - '
echo 'Apache2 is not working.'
#!/bin/bash
export TARGET_DIR='./' # Current directory
export ORIGINAL_NAME='xxxxx'
export TARGET_NAME='yyyyy'
find "${TARGET_DIR}" -name '*${ORIGINAL_NAME}*' -execdir rename "s/${ORIGINAL_NAME}/${TARGET_NAME}/g" '{}' \;
AWS announced to provide their own internal NTP inside VPC at AWS re:Invent 2017. Here is how to do it:
CentOS
sudo yum erase ntp*; sudo yum -y install chrony; sudo service chronyd start
Debian/Ubuntu
sudo apt-get remove -y ntp*; sudo apt-get -y install chrony; sudo service chronyd start; apt-get autoremove
Add an option -o StrictHostKeyChecking=no into a ssh command:
ssh -o StrictHostKeyChecking=no your_username@example.com
OR
export TARGET_HOST='example.com'
if [ -z `ssh-keygen -F ${TARGET_HOST}` ]; then
ssh-keyscan -H ${TARGET_HOST} >> ~/.ssh/known_hosts
fi
ssh-keygen -R ${TARGET_HOST}
ssh-keyscan -f ~/.ssh/known_hosts ${TARGET_HOST} >>~/.ssh/known_hosts 2>/dev/null
#!/bin/sh
export DATE_CURRENT=`date +%Y-%m-%d`
export TIME_CURRENT=`date +%Y%m%d%H%M%S`
export PURGE_AFTER_DAYS=30
export PURGE_AFTER=`date -d +${PURGE_AFTER_DAYS}days -u +%Y-%m-%d`
export BACKUP_TAG='backup'
# 1-1 Get the list of instances that you want to get backup
INSTANCES=`aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=key,Values=${BACKUP_TAG}" | awk '{print $3}'`
for INSTANCE in ${INSTANCES}; do
BACKUP=`aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=resource-id,Values=${INSTANCE}" "Name=key,Values=${BACKUP_TAG}" | awk '{print $5}'`
# 1-2 Check if the instance is marked as backup or not
if [ "${BACKUP}" == "true" ]; then
# 1-3 Create a backup
AMI_ID=`aws ec2 create-image --instance-id ${INSTANCE} --name "${INSTANCE}_${TIME_CURRENT}" --no-reboot`
# 1-4 Create a tag for search for later use
aws ec2 create-tags --resources ${AMI_ID} --tags Key=PurgeAllow,Value=true Key=PurgeAfter,Value=$PURGE_AFTER
fi
done
# 2-1 Search backups by tag
AMI_PURGE_ALLOWED=`aws ec2 describe-tags --filters "Name=resource-type,Values=image" "Name=key,Values=PurgeAllow" | awk '{print $3}'`
for AMI_ID in ${AMI_PURGE_ALLOWED}; do
PURGE_AFTER_DATE=`aws ec2 describe-tags --filters "Name=resource-type,Values=image" "Name=resource-id,Values=${AMI_ID}" "Name=key,Values=PurgeAfter" | awk '{print $5}'`
if [ -n ${PURGE_AFTER_DATE} ]; then
DATE_CURRENT_EPOCH=`date -d ${DATE_CURRENT} +%s`
PURGE_AFTER_DATE_EPOCH=`date -d ${PURGE_AFTER_DATE} +%s`
if [[ ${PURGE_AFTER_DATE_EPOCH} < ${DATE_CURRENT_EPOCH} ]]; then
# 2-2 Delete backup if it is marked as backup
aws ec2 deregister-image --image-id ${AMI_ID}
SNAPSHOT_ID=`aws ec2 describe-images --image-ids ${AMI_ID} | grep EBS | awk '{print $4}'`
aws ec2 delete-snapshot --snapshot-id ${SNAPSHOT_ID}
fi
fi
done
Use System Environment Variable${PIPESTATUS[X]}.
$ exit 1 | exit 2 | exit 3 | exit 0
$ echo ${PIPESTATUS[@]}
1 2 3 0
Also, the exit code of the normal end is 0, and the one of the abnormal end is Greater than 0 in Bash.
The following bash command deletes backup*.tar.gz files in a /var/www/html/backups directory by xargs rm -fr.
find /var/www/html/backups -mtime +2 -name "backup*.tar.gz" | xargs rm -fr
Consider to insert one white space before and after English word in Japanese. Run the following script by changing a string 'YOUR_FILENAME'. The script copy the original file as YOUR_FILENAME.org and overwrite and replace the original file by inserting one white space before and after an English word. It is useful multi-byte language such as CJK (Chinese, Japanese and Korean).
export FILENAME='index.rst'; \
cp ${FILENAME} ${FILENAME}.org; \
cat ${FILENAME}.org | sed y/0123456789/0123456789/ | \
There are a couple of options to add a user to group(s) in Linux. The commands are gpasswd, usermod and adduser.
I recommend to use gpasswd to add a user to group, which is no harm.
gpasswd -a <USERNAME> <GROUPNAME>
You must use usermod command as follows:
usermod -aG <GROUPNAME> <USERNAME>