Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I have an objective of copying a file to a new filename having appended text more simply than completely retyping the original name. For instance to copy a file ...
  1. #1
    Just Joined! iPenguin's Avatar
    Join Date
    Jul 2008
    Posts
    6

    Lightbulb Bash Script - copy with appended text

    Hello,

    I have an objective of copying a file to a new filename having appended text more simply than completely retyping the original name.

    For instance to copy a file named: reallyLongFileNameAndHow

    To a new file name: reallyLongFileNameAndHow.1

    This would take a command like: cp really* reallyLongFileNameAndHow.1
    Assuming to select only the file beginning with 'really'.

    I can not seem to find a command line only method for accomplishing this, but I did write a script which will produce the result.

    The script would be run as such: ca really* .1
    The result is a copy of the first file name copied to a second file having the text '.1' appended to the name.

    Can anyone think of another method, or improvements to the script, thanks in advance.

    Code:
    #!/bin/sh
    
    # file: ca
    
    # copy with append to simplify adding text to an existing filename
    # accept 1 or more file names with exactly one text string to append to each name
    # wildcards are okay for the filename 
    # for example:  { ca file* .txt } to copy all filenames beginning with file to file*.txt
    
    
    # perform 2 tests: 
    # The 1st to confirm a filename was received 
    # The 2nd to confirm that an appendable text string was received
    #
    for a in "$@"
    do
    
      # test to confirm at least one filename was received
      if [[ -f "$a" && -z "$hasfile" ]]
        then
          hasfile="true"
      fi
    
    
      # test to find the arg that is the appendable text string
      if ! [ -f "$a" ]
        then
          txt=$a
      fi
    
    done
    
    
    
    # test to confirm both filename and appendable text exists, else exit and print usage
    if [[ -z "$hasfile" || -z "$txt" ]]
      then
        echo "The ca command will copy-append text to an existing file name. Wildcards can be used."
        echo "Usage:  { cpa file1 [ file2 file3... ] appended-text }."
        exit;
    fi
    
    
    
    # now process each filename by appending the text to each filename
    # the variable named { a } represents each argument received by the script
    for a in "$@"
    do
    
      # process only a file, the appendable text is already in the { txt } variable
      if [ -f "$a" ]
        then
    
          # create the new filename having appended text
          newFilename="$a$txt"
    
          # if the new file does not exist then create the new file
          if ! [ -f "$newFilename" ]
            then
    
              # copy the original file to the new filename having the appended text
              cp $a $newFilename
    
          fi
    
      fi
    
    done
    
    # - - - EOF - - -

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Try "rename".

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    for a ready script, you can use the File renamer script in my sig.
    eg usage
    Code:
    # touch reallyLongFileNameAndHow
    # filerenamer.py -p "How" -e "How.1" -l "really*" #use -l to list only
    ==>>>>  [ /home/reallyLongFileNameAndHow ]==>[ /home/reallyLongFileNameAndHow.1 ]
    # filerenamer.py -p "How" -e "How.1"  "really*" #remove -l to commit
    /home/reallyLongFileNameAndHow  is renamed to  /home/reallyLongFileNameAndHow.1
    # ls really*
    reallyLongFileNameAndHow.1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...