Shell script to rename given file names to from uppercase to lowercase OR lowercase to uppercase

in Categories File-management last updated April 9, 2008
#!/bin/bash
# 
# Shell script to rename given file names to from uppercase to 
# lowercase OR lowercase to uppercase
# 
# Copyright (C) 2005 nixCraft project.
#
# This script licensed under GNU GPL version 2.0 or above
#
# Support/FeedBack/comment :  http://cyberciti.biz/fb/
# -------------------------------------------------------------------
# To rename file uppercase to lowercase create sym link:
# ln -s /path/2upper /path/2lower
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
FILES="$1"
ME="$(basename $0)"
 
# function to display message and exit with given exit code
function die(){
  echo -e "$1"
  exit $2
}
 
# exit if no command line argument given
[ "$FILES" == "" ] && die "Syntax: $ME {file-name}\nExamples:\n $ME xyz\n $ME \"*.jpg\"" 1 || :
# scan for all input file
for i in $FILES
do
   # see if upper to lower OR lower to upper by command name
   [ "$ME" == "2upper" ] && N="$(echo "$i" | tr [a-z] [A-Z])" ||  N="$(echo "$i" | tr [A-Z] [a-z])"
   # if source and dest file not the same then rename it  
   [ "$i" != "$N" ] &&  mv "$i" "$N" || :
done

Shell Script that outputs the number of users with entries in the /etc/passwd file

in Categories File-management last updated April 9, 2008
#!/bin/bash
# Shell Script that outputs the number of users with entries in the
# Unix or Linux /etc/passwd file
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
PASSWD=/etc/passwd
 
# easy method with wc
OUT=$(wc -l $PASSWD)
 
# More accurate method to find out
# If user account suspended using # character then second method 
# is more accurate and recommend
OUT1=$(grep -Ev "^#" $PASSWD | wc -l)
 
echo "$OUT [$OUT1]"
# You can filter result with awk
echo $OUT | awk '{ print $1 }'

Shell Script to find out whether file has read, write and execute permission

in Categories File-management last updated April 5, 2008

There are three specific permissions on Unix-like (including Linux / Mac OS x) systems that apply to each user or class:
=> The read permission (r), which grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory
=> The write permission (w), which grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files.
=> The execute permission (x), which grants the ability to execute a file. This permission must be set for executable binaries in order to allow the operating system to run them. When set for a directory, this permission grants the ability to traverse its tree in order to access files or subdirectories, but not see files inside the directory (unless read is set).

SHELL CONDITIONAL EXPRESSIONS

You can use conditional expressions to find out file permissions. These are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons.

Conditional ExpressionMeaning
-a file True if file exists.
-b file True if file exists and is a block special file.
-c file True if file exists and is a character special file.
-d file True if file exists and is a directory.
-e file True if file exists.
-f file True if file exists and is a regular file.
-g file True if file exists and is set-group-id.
-h file True if file exists and is a symbolic link.
-k file True if file exists and its “sticky” bit is set.
-p file True if file exists and is a named pipe (FIFO).
-r file True if file exists and is readable.
-s file True if file exists and has a size greater than zero.
-t fd True if file descriptor fd is open and refers to a terminal.
-u file True if file exists and its set-user-id bit is set.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-O file True if file exists and is owned by the effective user id.
-G file True if file exists and is owned by the effective group id.
-L file True if file exists and is a symbolic link.
-S file True if file exists and is a socket.
-N file True if file exists and has been modified since it was last read.

Sample Shell Script

#!/bin/bash
# Shell script to find out whether file has read, write and execute 
# permission
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter file name : "
read file
 
# find out if file has write permission or not
[ -w $file ] && W="Write = yes" || W="Write = No"
 
# find out if file has excute permission or not
[ -x $file ] && X="Execute = yes" || X="Execute = No"
 
# find out if file has read permission or not
[ -r $file ] && R="Read = yes" || R="Read = No"
 
echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"