Using BASH command Find to locate files – Part 1

Use command to list files in the lammps-7Aug19 directory

[u1@node1 lammps-7Aug19]$ find

Count the number of files by piping the list to wc to count the files.

[u1@node1 lammps-7Aug19]$ find | wc -l
14717

Count the the directories listing by piping the list to wc to count the directories

[u1@node1 lammps-7Aug19]$ find -type d | wc -l
672

Count the list of files that matched the wildcard pattern *.mov that are larger than 1M by piping to wc to count

[u1@node1 lammps-7Aug19]$ find -type f -name "*.jpg" -size +1M |wc -l

Find and match files or directories whose contents or attributes were last modified more recently than those of “mybasefile”

[u1@node1 lammps-7Aug19]$ find -type f -name "*.jpg" -cnewer mybasefile

Find empty files in the lammps-7Aug19 directory

[u1@node1 lammps-7Aug19]$ find -type f -empty

Find files or directories that belongs to username

[u1@node1 Downloads]$ find -type f -user root |wc -l
1572

References:

  1. The Linux Command Line

Checking and Modifying Timestamp of whole Directory recursively

Step 1: Show the complete date, time and year for a specific file

$ ls -l --full-time
-rwxrwxr-x  1 root root  1109 2018-07-20 12:52:52.587945000 +0800 Allwmake
drwxrwxr-x  5 root root  4096 2018-07-20 12:52:52.602945000 +0800 applications
drwxrwxr-x  3 root root  8192 2018-07-20 12:53:19.536973000 +0800 bin
-rw-rw-r--  1 root root 35646 2018-07-20 12:52:52.592945000 +0800 COPYING
drwxrwxr-x  5 root root  4096 2018-07-20 12:53:19.936974000 +0800 doc
drwxrwxr-x  8 root root  4096 2018-07-20 12:53:20.039974000 +0800 etc
drwxr-xr-x  4 root root  4096 2018-07-20 12:55:17.230101000 +0800 platforms
-rw-rw-r--  1 root root  1620 2018-07-20 12:52:52.597945000 +0800 README.org
drwxrwxr-x 38 root root  4096 2018-07-20 12:53:22.032976000 +0800 src
drwxrwxr-x 17 root root  4096 2018-07-20 12:54:45.114064000 +0800 tutorials
drwxrwxr-x  7 root root  4096 2018-07-20 12:55:15.939099000 +0800 wmake

Step 2: If you wish to modify the time-stamp for the entire directory, you can use the command,

% for file in `find .`; do touch $file; done

References:

  1. touch – change file timestamps(Unix Tutorial)

Quick and Dirty way to ensure only one instance of a shell script is running at a time

This script is taken from Quick and Dirty way to ensure only one instance of a shell script is running at a time

For example, this can be used to ensure SAGE Mirroring rsync run once.

# rsyncs from sage.math.washington.edu using its rsync daemon
# for automated use, remove the "vv" and "progress" switches

# locking mechanism from
# http://stackoverflow.com/questions/185451/quick-and-dirty-way-to-ensure-only-one-instance-of-a-shell-script-is-running-at-a

LOCKFILE=./rsync_sagemath.lock

if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
echo "rsync_sagemath already running ... exit"
exit
fi

# make sure the lockfile is removed when we exit and then claim it
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $ > ${LOCKFILE}

rsync -av --delete-after rsync.sagemath.org::sage /var/www/html/sage

rm -f ${LOCKFILE}

Simple BASH script to setup shared SSH keys on Cluster

Here is my simple script to setup shared SSH keys on Cluster. You can put this script called ssh-shared-keys.sh into /etc/skel/.bash_profile so that the new users have their keys shared between all the compute nodes.

#!/bin/bash

# Exit script on Error
set -e

# Check for SSH Directory
if [ ! -d ~/.ssh ]; then
   mkdir -p ~/.ssh/
fi


# Check for existence of passphrase
if [ ! -f ~/.ssh/id_rsa.pub ]; then
        ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
        echo "Execute ssh-keygen --[done]"
fi

# Check for existence of authorized_keys and append the shared ssh keys
if [ ! -f ~/.ssh/authorized_keys ]; then
        touch ~/.ssh/authorized_keys
        echo "Create ~/.ssh/authorized_keys --[done]"
        chmod 700 ~/.ssh/authorized_keys
        cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
        echo "Append the public keys id_rsa into authorized keys --[done]"
        chmod 400 ~/.ssh/authorized_keys
        chmod 700 ~/.ssh/
fi

# Create user's ssh config it not exist
if [ ! -f ~/.ssh/config ]; then
        touch ~/.ssh/config
        echo "StrictHostKeyChecking no" > ~/.ssh/config
        echo "StrictHostKeyChecking no --[done]"
        chmod 644 ~/.ssh/config
fi
# Unset error on exit or it will affect after bash command :)
set +e

References:

  1. Helping users to SSH without password into the Compute Nodes manually

Using the command “ls” options and arguments

ls has some interesting format which is very useful. Here are some arguments which you can use. For example, I like

$ ls - ltF
 -a   –all  All files include those with .
 -l  Display results in long format
 -r   –reverse  Reverse order while sorting
 -S  Sort results by file size
 -t  Sort by modification time.
 -F   –classify  Append an indicator character to the end of each listed name

References:

  1. The Linux Command Line (by No Starch Press)