[SOLVED] Changing file names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] Changing file names
# 1  
Old 09-27-2011
[SOLVED] Changing file names

I have written a csh script that changes the name of file from src to dst.

I am getting the error below:

Code:
TESTAmvfiles
DONE TESTAmvfiles
set: Variable name must begin with a letter.

The csh script is:

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

##########################################################################

---------- Post updated at 12:11 PM ---------- Previous update was at 11:16 AM ----------

Problem solved. Needed grep --

Last edited by radoulov; 09-27-2011 at 04:45 PM.. Reason: Marked as solved.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing file names

sac_pzs_iv_epoz_hhe__2013.074.14.40.46.0000_2599.365.23.59.59.99999 sac_pzs_iv_epoz_hhn__2013.074.14.40.46.0000_2599.365.23.59.59.99999 sac_pzs_iv_epoz_hhz__2013.074.14.40.46.0000_2599.365.23.59.59.99999 sac_pzs_iv_haga_hhe__2006.111.00.00.00.0000_2599.365.23.59.59.99999... (3 Replies)
Discussion started by: kristinu
3 Replies

2. Debian

Changing file extension names

Hi I have a list of files :root@L28mustang:/var/log/exim4/2017/Jul2017_Blast_BC07# ls -lrt | grep mainlog -rw-r----- 1 Debian-exim adm 3636932 Jul 8 06:25 mainlog.3.gz -rw-r----- 1 Debian-exim adm 919512 Jul 9 06:27 mainlog.2.gz -rw-r----- 1 Debian-exim adm 7655054 Jul 10 06:25 mainlog.1... (1 Reply)
Discussion started by: anaigini45
1 Replies

3. UNIX for Dummies Questions & Answers

Bulk changing of file names using Terminal in OS X

I want to change the name of several files within a folders (directory) and subdirectories in OS X. If I only wanted to change file names within the directory I guess I would use: rm photo*.jpg picture*.jpg I have lots of subdirectories, is there a way of getting the file changes for all... (5 Replies)
Discussion started by: ademanuele
5 Replies

4. Shell Programming and Scripting

Changing file names

I have a series of files as follows file-1.pdf file-2.pdf file-3.pdf file-4.pdf file-5.pdf file-6.pdf file-7.pdf I want to have the file names with odd numbers starting from an initial number, for example 2000. The result would be the following: file-2001.pdf file-2003.pdf... (9 Replies)
Discussion started by: kristinu
9 Replies

5. Shell Programming and Scripting

Changing file names

I have file names as shown and want to change the name to have only the first four numbers. /home/chrisd/Desktop/nips/nips_2013/5212-learning-feature-selection-dependencies-in-multi-task-learning.pdf /home/chrisd/Desktop/nips/nips_2013/5213-parametric-task-learning.pdf... (3 Replies)
Discussion started by: kristinu
3 Replies

6. UNIX for Dummies Questions & Answers

[solved]removing characters from a mass of file names

I found a closed thread that helped quite a bit. I tried adding the URL, but I can't because I don't have enough points... ? Modifying the syntax to remove ! ~ find . -type f -name '*~\!]*' | while IFS= read -r; do mv -- "$REPLY" "${REPLY//~\!]}"; done These messages are... (2 Replies)
Discussion started by: rabidphilbrick
2 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Writing a loop to changing the names of files in a directory

Hi, I would like to write a loop to change the names of files in a directory. The files are called data1.txt through data1000.txt. I'd like to change their names to a1.txt through a1000.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

8. Shell Programming and Scripting

Changing file names

I have lot of files whose names are something like the following. I want to change the name of all the files from 'npt02' to 'n02'. npt02-z30-sr65-rgdt0p50-dc0p01-16x12drw.tpf npt02-z30-sr65-rgdt0p50-dc0p01-8x6drw.back npt02-z30-sr65-rgdt0p50-dc0p01-8x6drw-bst-mis.xy... (5 Replies)
Discussion started by: kristinu
5 Replies

9. Shell Programming and Scripting

Changing file names with AWK

Dear All, I have some thousands of files in a folder and i need to change those file names without opening the file (no need to change anything in the file content, need to change the file name only). The filenames are as follows: Myfile_name.1_parameter Myfile_name.2_parameter... (6 Replies)
Discussion started by: Fredrick
6 Replies

10. Shell Programming and Scripting

changing file names to lowercase

hey guys having some trouble figuring this out. my program is supposed to take a name of a directory as a command line argument and change the filenames inside that directory to lowercase. what i dont get is how you access that directory and go thru all the files and change the filenames... (1 Reply)
Discussion started by: 30177005
1 Replies
Login or Register to Ask a Question