Code:
#!/bin/csh
#
###########################################################################
#
# --Module----------------------------------------------------------------
#
# manipulate-files.csh
#
# Manipulates files (including file names and contents).
#
# --Operations------------------------------------------------------------
#
# manipulate-files.csh [options]
#
# Options:
#
# --rmfiles=str
# Remove files matching string in file name.
#
# --rmlines=str
# Remove lines matching string in a file.
#
# --mvfiles=src/dst
# Replace source with dest in the file names.
#
# --mvlines=src/dst
# Replace source with dest in a file.
#
# -c, --commit
# Commit the change.
#
# -r, --replace
# Commit the change.
#
# -v, --verbose
# Print information on what is happening.
#
# -u, --usage
# Print options.
#
# -e, --examples
# Print examples.
#
# -h, --help
# Print full description.
#
# chmod -R 777 dir_name/
# Tells the system how much access it should permit to all files and
# directories respectively.
#
# chmod Options:
#
# u Sets permissions for the owner of the file,
# e.g.: "u+w" allows the owner to write to the file
# g Sets permissions for the group (to which owner belongs),
# e.g. "g-x" suppresses the execution of the file by the group
# o Sets permissions for other users (that are not in group),
# e.g.: "o=r" allows others only to read the file
# a Sets permissions for all (owner, group and others),
# e.g.: "a-w" disables write access to the file for everyone
# = Assigns the permissions,
# e.g. "a=rw", sets read and write permissions and disables execution
# for all
# - Removes certain thing[s] from the permissions, keeping all other
# (not involved) permissions.
# e.g. "a-x" disables execution of the file for everyone, this example doesn't touch read and write permissions.
# + Adds certain thing[s] to the permissions, keeping all other
# (not involved) permissions.
# e.g. "a+x" allows execution of the file for everyone, this example
# doesn't touch read and write permissions.
# r Sets read permissions
# w Sets write permissions
# x Sets execute permissions
# -R, --recursive
# Change files and directories recursively
#
# --Examples--------------------------------------------------------------
#
# /pth/manipulate-files.csh
#
# --Note------------------------------------------------------------------
#
# Matching patterns:
#
# Instead of: set match = `echo $f | grep $fsrc`
# one can use: set match = `echo $f | awk -v src=$fsrc '$0 ~ src'`
#
# find path -name "*.fext" -exec rm -f '{}' ';'
# find path [expression]
#
# -exec command
# Execute command; true if 0 status is returned. All following
# arguments to find are taken to be arguments to the command until
# an argument consisting of ";" is encountered. The string "{}"
# is replaced by the current file name being processed everywhere
# it occurs in the arguments to the command, not just in arguments
# where it is alone, as in some versions of find. Both of these
# constructions might need to be escaped (with a "\") or quoted to
# protect them from expansion by the shell. See the EXAMPLES sec-
# tion for examples of the use of the "-exec" option. The speci-
# fied command is run once for each matched file. The command is
# executed in the starting directory. There are unavoidable
# security problems surrounding use of the -exec option; you
# should use the -execdir option instead.
#
# --History---------------------------------------------------------------
#
# V01 - JAN 2011 - Christopher Dimech
# Initial release.
#
###########################################################################
# ------------------------------------------------------------------------
# 1. INITIALIZATION
# ------------------------------------------------------------------------
# Set echo to recognize both the -n flag and backslashed escape sequences
set echo_style = "both"
# Set bc for numeric calculations
alias MATH 'set \!:1 = `echo "\!:3-$" | bc -l`'
set iarg = 0
set opt_rmfiles = 0
set opt_rmlines = 0
set opt_mvfiles = 0
set opt_mvlines = 0
set opt_commit = 0
set opt_replace = 0
set opt_ftag = 0
set opt_verbose = 0
set opt_usage = 0
set opt_examples = 0
set opt_help = 0
set Aftag = ""
set Armfiles = ""
set Armlines = ""
# ----------------------------------------------------------------------
# 2. READ THE COMMAND LINE ARGUMENTS
# ----------------------------------------------------------------------
set AfullnamesLst = ""
set fullnamesLst = ""
set narg = $#argv
while ($iarg < $narg)
MATH iarg = $iarg + 1
set arg = $argv[$iarg]
set opt = `echo $arg | awk 'BEGIN {FS="="} {print $1}'`
set par = `echo $arg | awk 'BEGIN {FS="="} {print $2}'`
switch ($opt)
case "--rmf":
case "--rmfiles":
echo "TESTArmfiles"
set Armfiles = $par
set opt_rmfiles = 1
breaksw
case "--rml":
case "--rmlines":
echo "TESTArmlines"
set Armlines = $par
set opt_rmlines = 1
breaksw
case "--mvf":
case "--mvfiles":
echo "TESTAmvfiles"
set Amvfiles = $par
set opt_mvfiles = 1
echo "DONE TESTAmvfiles"
breaksw
case "--mvl":
case "--mvlines":
echo "TESTAmvlines"
set Amvlines = $par
set opt_mvlines = 1
breaksw
case "-c":
case "--commit":
echo "TESTcommit"
set opt_commit = 1
breaksw
case "-r":
case "--replace":
echo "TESTreplace"
set opt_replace = 1
breaksw
case "-t":
case "--tag":
echo "TESTtag"
set opt_ftag = 1
breaksw
case "-v":
case "--verbose":
echo "TESTverbose"
set opt_verbose = 1
breaksw
case "-u":
case "--usage":
echo "TESTusage"
set opt_usage = 1
breaksw
case "-e":
case "--examples":
echo "TESTexamples"
set opt_example = 1
breaksw
case "-h":
case "--help":
echo "TESThelp"
set opt_help = 1
breaksw
default:
echo "TESTAfullnamesLst"
echo "$arg"
set AfullnamesLst = "$AfullnamesLst $arg"
breaksw
endsw
end # while
echo "HELLLO"
# -------------------------------------------------------------------
# 3. PRINT USAGE, EXAMPLES, or HELP
# -------------------------------------------------------------------
set Version = "V01"
set fcsh = "/pth/manipulate-files.csh"
set arg1 = "[(-rmf, --rmfiles)=string] [(-rml, --rmlines)=string]"
set arg2 = "[(-mvf, --mvfiles)=source/dest] [(-mvl, -mvlines)=source/dest]"
set arg3 = "[-c, --commit] [-v, --verbose] [-u, --usage] [-e, --examples]"
set arg4 = "[-h, --help]"
set usg1 = "$fcsh $arg1 $arg2 $arg3 $arg4"
set eg1 = "$fcsh --mvfiles=npt02/n02 *.txt -v --commit -v"
set eg2 = "$fcsh --mvlines=Diag/diag *.txt --commit -v"
set eg3 = "$fcsh --rmfiles=xy *.txt --commit -v"
set eg4 = "$fcsh --rmlines=Diag *.txt --commit -v"
if ($opt_usage == 1) then
echo "\n $usg1\n"
exit 0
endif
if ($opt_examples == 1) then
echo "\n $eg1\n $eg2\n $eg3\n $eg4\n"
exit 0
endif
if (($opt_help == 1) || ($narg == 0)) then
echo ""
echo "##################################################################"
echo "* *"
echo "* /tommy/csh/manipulate-files.csh *"
echo "* *"
echo "* Manipulates files (moving, deleting). *"
echo "* *"
echo "##################################################################"
echo ""
echo " OPTIONS:"
echo ""
echo " --mvfiles=src/dst"
echo " Replace file names matching src with dst."
echo ""
echo " --mvlines=src/dst"
echo " Replace lines matching src with dst."
echo ""
echo " --rmfiles=str"
echo " Remove files matching str in file name."
echo ""
echo " --rmlines=str"
echo " Remove lines matching str in the file."
echo ""
echo " -c, --commit"
echo " Commit the change."
echo ""
echo " -r, --replace"
echo " Replace (overwrite) the file."
echo ""
echo " -v, --verbose"
echo " Print information on what is happening."
echo ""
echo " -u, --usage"
echo " Print usage."
echo ""
echo " -e, --examples"
echo " Print examples."
echo ""
echo " -h, --help"
echo " Print full description."
echo ""
echo " --Examples------------------------------------------------------"
echo ""
echo "\n $eg1\n $eg2\n $eg3\n $eg4\n"
exit 0
endif
# -------------------------------------------------------------------------
# TRAP ERRORS
# -------------------------------------------------------------------------
# Check for invalid filenames. cerr1 and cerr2 are non-empty for invalid
# arguments
set ierr = 0
set AierrLst = ""
foreach f ($AfullnamesLst)
set cerr = `echo $f | awk '/^-|=/'`
if ("$cerr" != "") then # Matches '-' at beginning or '='.
set Aierr_list = "$AierrLst $f"
set ierr = 1
endif
end
# Prints unrecognised arguments
if ($ierr == 1) then
echo ""
foreach f ($AierrLst)
echo "\nERROR. Unrecognised argument: $f"
end
echo ""
exit 1
endif
if ($opt_mvfiles == 1) then
echo "\nAmvfiles: $Amvfiles"
set n = `echo $Amvfiles | awk 'BEGIN {FS="/"} {print NF}'`
if ($n == 1) then
echo "\nERROR. --mvfiles takes two string arguments.\n"
exit 1
endif
endif
if ($opt_mvlines == 1) then
set n = `echo $Amvlines | awk 'BEGIN {FS="/"} {print NF}'`
if ($n == 1) then
echo "\nERROR. --mvlines takes two string arguments.\n"
exit 1
endif
endif
# ---------------------------------------------------------------------
# SET DEFAULT ARGUMENTS AND PARAMETERS
# ---------------------------------------------------------------------
set fullnamesLst = "$AfullnamesLst"
if ($opt_mvfiles == 1) then
set fsrc = `echo $Amvfiles | awk 'BEGIN {FS="/"} {print $1}'`
set fdst = `echo $Amvfiles | awk 'BEGIN {FS="/"} {print $2}'`
endif
if ($opt_mvlines == 1) then
set lsrc = `echo $Amvlines | awk 'BEGIN {FS="/"} {print $1}'`
set ldst = `echo $Amvlines | awk 'BEGIN {FS="/"} {print $2}'`
endif
set rmFPattern = ""
if ($opt_rmfiles == 1) set rmFPattern = $Armfiles
set rmLPattern = ""
if ($opt_rmlines == 1) set rmLPattern = $Armlines
set tag = ""
if ($opt_ftag == 1) set tag = $Aftag
if ($opt_ftag == 0) set tag = "new"
# -----------------------------------------------------------------
# PRINT HEADER AND COMMAND LINE ARGUMENTS
# -----------------------------------------------------------------
if ($opt_verbose == 1) then
echo "##################################################################"
echo ""
echo " manipulate-files.csh"
echo ""
echo " Manipulates files (moving, deleting)"
echo ""
echo "##################################################################"
echo " manipulate-files.csh [options]"
if ($opt_mvfiles == 1) then
echo "Replace file names matching '$fsrc' with '$fdst'.\n"
echo "Commands to run:\n"
foreach f ($fullnamesLst)
set match = `echo $f | grep $fsrc`
echo "match = .$match. $f $fsrc"
if ("$match" != "") then # filename matches fsrc.
set fname = `echo $f | awk 'BEGIN {FS="."} {print $1}'`
set fext = `echo $f | awk 'BEGIN {FS="."} {print $2}'`
set fout = `echo $f | awk -v src=$fsrc -v dst=$fdst \
'{sub(src,dst); print}'`
echo " mv $f $fout"
else # filename does not match fsrc.
echo "WARNING: '$f' does not match '$fsrc'"
endif
end
echo ""
endif
if ($opt_mvlines == 1) then
echo "Replace lines matching '$lsrc' with '$ldst'.\n"
echo "Commands to run:\n"
foreach f ($fullnamesLst)
set match = `grep $lsrc $f`
echo "$match"
set fname = `echo $f | awk 'BEGIN {FS="."} {print $1}'`
set fext = `echo $f | awk 'BEGIN {FS="."} {print $2}'`
set fout = "${fname}-${tag}.${fext}"
echo " awk -v src=$lsrc -v dst=$ldst '{gsub(src,dst); print}' $f > $fout"
if ($opt_replace == 1) echo " mv $fout $f\n"
end
if ($opt_replace == 0) echo ""
endif
if ($opt_rmfiles == 1) then
echo "Delete files matching $rmFPattern.\n"
echo "Commands to run:\n"
echo " find . -name "\"\*"$rmFPattern"\*\"" -exec rm -f {} \;\n"
endif
if ($opt_rmlines == 1) then
echo "Remove lines matching $rmLPattern.\n"
echo "Commands to run:\n"
foreach f ($fullnamesLst)
set match = `grep $lsrc $f`
echo "$match"
set fname = `echo $f | awk 'BEGIN {FS="."} {print $1}'`
set fext = `echo $f | awk 'BEGIN {FS="."} {print $2}'`
set fout = "${fname}-${tag}${fext}"
echo " grep -v "$rmLPattern" $f > $fout"
if ($opt_replace == 1) echo " mv $fout > $f\n"
end
if ($opt_replace == 0) echo ""
endif
endif
# ------------------------------------------------------------------------
# IMPLEMENTATION
# ------------------------------------------------------------------------
#
if ($opt_commit == 1) then
# Replace file names matching '$fsrc' with '$fdst'.
if ($opt_mvfiles == 1) then
foreach f ($fullnamesLst)
set match = `echo $f | grep $fsrc`
if ("$match" != "") then # filename matches fsrc.
set fname = `echo $f | awk 'BEGIN {FS="."} {print $1}'`
set fext = `echo $f | awk 'BEGIN {FS="."} {print $2}'`
set fout = "${fname}-${tag}${fext}"
set fnew = `echo $f | awk -v src=$fsrc -v dst=$fdst \
'{sub(src,dst); print}'`
if ($opt_replace == 1) mv $f $fout
else # filename does not match fsrc.
echo "WARNING: '$f' does not match '$fsrc'"
endif
end # foreach
endif
# Replace lines matching '$lsrc' with '$ldst'.
if ($opt_mvlines == 1) then
foreach f ($fullnamesLst)
set fname = `echo $f | awk 'BEGIN {FS="."} {print $1}'`
set fext = `echo $f | awk 'BEGIN {FS="."} {print $2}'`
set fout = "${fname}-${tag}.${fext}"
awk -v src=$lsrc -v dest=$ldst '{ gsub(src,dst); print }' $f > $fout
if ($opt_replace == 1) mv $fout $f
end
endif
# Delete files matching $rmFPattern.
if ($opt_rmfiles == 1) find . -name "*$rmFPattern*" -exec rm -f {} \;
# Remove lines matching $rmLPattern.
if ($opt_rmlines == 1) then
foreach f ($fullnamesLst)
set fname = `echo $f | awk 'BEGIN {FS="."} {print $1}'`
set fext = `echo $f | awk 'BEGIN {FS="."} {print $2}'`
set fout = "${fname}-${tag}${fext}"
grep -v "$rmLPattern" $f > $fout
if ($opt_replace == 1) mv $fout $f
end
endif
endif # ($opt_commit == 1)
##########################################################################
#foreach f ($fullnames_list)
# set cerr = `echo $f | awk '/^-|=/'`
# if ("$cerr" != "") set ierr = 1
# if ($ierr == 1) then
# echo "\n ERROR: Unrecognised argument: $f\n"
# exit 1
# endif
#end
# File extensions that have permission to be deleted.
#set fext_list = "xz xy ps"
#set rmlines = `echo $Armlines | awk 'BEGIN {FS="."} {print $1}'`
#set rmfiles = `echo $Armfiles | awk 'BEGIN {FS="."} {print $1}'`
#set fullname_list = ""
#set fname_list = ""
#set fext_list = ""
#foreach f (Afullnames_list)
# set fname = `echo $f | awk 'BEGIN {FS="."} {print $1}'`
# set fext = `echo $f | awk 'BEGIN {FS="."} {print $2}'`
# set fullname_list = "$fullname_list $f"
# set fname_list = "$fname_list $fname"
# set fext_list = "$fext_list $fext"
#end
#if (($opt_commit == 0) && ($opt_rmlines == 1)) then
# foreach f ($fnames_list)
# set fout = "$f-cln"
# echo "grep -v \"$rmlines\" $f.log > $fout.log"
# end
#endif
#if (($opt_commit == 0) && ($opt_rmfiles == 1)) then
# foreach f (fext_list)
# if ($ == $f) then
# echo "find . -name \"*.$rmfiles\" -exec rm -f {} \;"
# else
# echo "Script does not allow the deletion of $f files"
# endif
# end
#endif
#set fext_list = "xz xy ps"
#if ($opt_rmlines == 1) then
# foreach f ($fnames_list)
# grep -v "$lineptn" $f.log > $f-cln.log
# end
#endif
#set files = `find . -name "*.$fileptn"`
# Removes (deletes) files matching pattern
#if ($opt_rmfiles == 1) then
# foreach f (fext_list)
# if ($ == $f) then
# find . -name "*.$fileptn" -exec rm -f {} \;
# else
# echo "Script does not allow the deletion of $f files"
# endif
# end
#endif
##########################################################################