• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

file

Cleanup Stale CVS Snapshots

August 30, 2022

Here’s the script that looks mounts with stale file handles and removes them.

#!/bin/bash
file='/tmp/stale.txt'
df -h | grep Stale > $file
sed -i -e 's/df: ‘/'""'/g' $file
sed -i -e 's/’: Stale file handle/'""'/g' $file
while IFS= read -r line; do
  echo 'unmounting '$line
  umount $line
done < $file

#!/bin/bash file='/tmp/stale.txt' df -h | grep Stale > $file sed -i -e 's/df: ‘/'""'/g' $file sed -i -e 's/’: Stale file handle/'""'/g' $file while IFS= read -r line; do echo 'unmounting '$line umount $line done < $file

Filed Under: Linux Tagged With: bash, cvs, file, handles, snapshots, stale

Check Mounted File Systems

July 28, 2022

Check mounted file systems using df. Displaying different options.

df 
df -h
df -Th

df df -h df -Th

To get rid tmpfs, run this.

df -Th| grep -Ev '(udev|tmpfs)'

df -Th| grep -Ev '(udev|tmpfs)'

Filed Under: Linux Tagged With: check, df, file, mounts, systems, tmpfs

Vi Search and Replace

April 3, 2022

How to do search and replace in Vim.

Search for “foo” and replace it with “bar” in the current line. Use :s

:s/foo/bar/g

:s/foo/bar/g

Search for “foo” and replace it with “bar” in the entire document. Use :%s

:%s/foo/bar/g

:%s/foo/bar/g

You can also pipe instead of forward slash. Useful if your search contains a /.

:%s|foo/|bar|g

:%s|foo/|bar|g

Filed Under: Linux Tagged With: entire, file, replace, search, vim

GCP Fix Hosts

December 7, 2021

This is a script that fixes /etc/hosts in GCP.

#!/bin/bash
 
# VARIABLES
FILE='/etc/hosts'
 
#BACKUP /etc/hosts before changes
cp -ipHR $FILE $FILE_$(date +%Y-%m-%d_%H-%M-%S)
 
# GET IP ADDRESS
IP=$(/sbin/ifconfig | grep 'inet' | grep -v 'inet6' | grep -v '127.0' | cut -d: -f2 | awk '{print $2}' | head -1)
 
# GET HOSTNAME
HN=$(/bin/hostname)
 
# GET PROJECT ID
echo "169.254.169.254 metadata.google.internal  # Added by Google" >> $FILE
PR=$(curl -f -s http://metadata.google.internal/computeMetadata/v1/project/project-id -H "Metadata-Flavor: Google")
 
# ADD HOSTNAME ENTRY TO /etc/hosts
STRING="$IP $HN.mydomain.com"
 
if grep -q "$STRING" "$FILE"
then
  echo "Valid host entry."
else
  echo "Adding host entry."
  echo $STRING $HN >> $FILE
fi
 
# DELETE GOOGLE ENTRIES.
sed -i '/# Added by Google/d' $FILE
 
# ADD BACK GOOGLE ENTRIES.
echo $IP $HN".c."$PR".internal" $HN"  # Added by Google" >> $FILE
echo "169.254.169.254 metadata.google.internal  # Added by Google" >> $FILE
 
# SET PERMISSIONS.
chmod 644 $FILE
 
# DISPLAY /etc/hosts
cat $FILE

#!/bin/bash # VARIABLES FILE='/etc/hosts' #BACKUP /etc/hosts before changes cp -ipHR $FILE $FILE_$(date +%Y-%m-%d_%H-%M-%S) # GET IP ADDRESS IP=$(/sbin/ifconfig | grep 'inet' | grep -v 'inet6' | grep -v '127.0' | cut -d: -f2 | awk '{print $2}' | head -1) # GET HOSTNAME HN=$(/bin/hostname) # GET PROJECT ID echo "169.254.169.254 metadata.google.internal # Added by Google" >> $FILE PR=$(curl -f -s http://metadata.google.internal/computeMetadata/v1/project/project-id -H "Metadata-Flavor: Google") # ADD HOSTNAME ENTRY TO /etc/hosts STRING="$IP $HN.mydomain.com" if grep -q "$STRING" "$FILE" then echo "Valid host entry." else echo "Adding host entry." echo $STRING $HN >> $FILE fi # DELETE GOOGLE ENTRIES. sed -i '/# Added by Google/d' $FILE # ADD BACK GOOGLE ENTRIES. echo $IP $HN".c."$PR".internal" $HN" # Added by Google" >> $FILE echo "169.254.169.254 metadata.google.internal # Added by Google" >> $FILE # SET PERMISSIONS. chmod 644 $FILE # DISPLAY /etc/hosts cat $FILE

Result

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters
 
10.128.0.30 test.mydomain.com test
10.128.0.30 test.c.project-id.internal test  # Added by Google
169.254.169.254 metadata.google.internal  # Added by Google

127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 10.128.0.30 test.mydomain.com test 10.128.0.30 test.c.project-id.internal test # Added by Google 169.254.169.254 metadata.google.internal # Added by Google

It adds a proper host entry before the Google entries. /etc/hosts has top to bottom precedence.

Filed Under: Cloud Tagged With: file, fix, gcp, hosts

Bash Read File

December 1, 2021

How to read a file line by line in Bash.

#!/bin/bash
file=test.txt
while IFS= read -r line; do
  echo $line
done < $file

#!/bin/bash file=test.txt while IFS= read -r line; do echo $line done < $file

Contents of test.txt.

one
two
three

one two three

Results when running test.sh.

$ bash test.sh
one
two
three

$ bash test.sh one two three

To read multiple strings line by line.

#!/bin/bash
file=test.txt
while read -r a b; do
  echo $b
done < $file

#!/bin/bash file=test.txt while read -r a b; do echo $b done < $file

Modified contents of test.txt.

one 1
two 2
three 3

one 1 two 2 three 3

New results.

$ bash test.sh
1
2
3

$ bash test.sh 1 2 3

Filed Under: Linux Tagged With: bash, file, loop, output, read

Create A Swap File

February 16, 2021

How to create a swap file.

A 2GB swap file.

dd if=/dev/zero of=/swapfile bs=1k count=2048k

dd if=/dev/zero of=/swapfile bs=1k count=2048k

Activate.

mkswap /swapfile
chmod 0600 /swapfile
systemctl daemon-reload
swapon /swapfile

mkswap /swapfile chmod 0600 /swapfile systemctl daemon-reload swapon /swapfile

To make swap permanent, add to /etc/fstab.

/swapfile  swap   swap    defaults   0 0

/swapfile swap swap defaults 0 0

Check if swap is working.

cat /proc/swaps
free -h

cat /proc/swaps free -h

Filed Under: Linux Tagged With: activate, create, file, swap

CloudFormation Userdata

January 15, 2021

Here’s another way to add startup scripts to your instance during creation.

      UserData:
        Fn::Base64: !Sub |
          #cloud-config
          repo_upgrade: none
 
          runcmd:
            # Cloud init startup script
            - "bash /root/setup.sh"
 
          write_files:
            # Cloud init startup script
            - owner: root:root
              permissions: '0644'
              path: /root/setup.sh
              content: |
                #! /bin/bash -x
          write_files:
            # Cloud init startup script
            - owner: root:root
              permissions: '0644'
              path: /root/setup.sh
              content: |
                #! /bin/bash -x
                # run your bash commands here
                date > log.txt
                uptime >>  log.txt

UserData: Fn::Base64: !Sub | #cloud-config repo_upgrade: none runcmd: # Cloud init startup script - "bash /root/setup.sh" write_files: # Cloud init startup script - owner: root:root permissions: '0644' path: /root/setup.sh content: | #! /bin/bash -x write_files: # Cloud init startup script - owner: root:root permissions: '0644' path: /root/setup.sh content: | #! /bin/bash -x # run your bash commands here date > log.txt uptime >> log.txt

The above script creates a file setup.sh and executes it during instance creation. The output is dumped to log.txt file.

Filed Under: Cloud Tagged With: cloudformation, execute, file, run, startup, userdata

Audacity Copy

August 8, 2020

I was getting an error when launching Audacity.

The error was:

“The system has detected that another copy of Audacity is running.”

However, Audacity wasn’t running at all. Not one process. It turned out to be just a locked file located in /var/tmp/audacity-[your-username].

All you have to do is delete the entire directory and rerun Audacity.

rm -rf /var/tmp/audacity=[your-username]/

rm -rf /var/tmp/audacity=[your-username]/

Once deleted, Audacity starts right up with no issues.

Filed Under: Linux Tagged With: another, audacity, copy, file, locked, running

  • Go to page 1
  • Go to page 2
  • Go to Next Page »
  • Home
  • About
  • Archives

Copyright © 2023