Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, if i have these files in this structure: /folder1/folder2/afile.20070501 /folder1/folder3/afile.20070501 /folder1/folder4/afile.20070501 /folder2/folder5/afile.20070501 /folder2/folder6/afile.20070501 I don't know *all* the location of all the files - there may be more or ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    3

    Rename files while preserving directories

    Hi, if i have these files in this structure:

    /folder1/folder2/afile.20070501
    /folder1/folder3/afile.20070501
    /folder1/folder4/afile.20070501
    /folder2/folder5/afile.20070501
    /folder2/folder6/afile.20070501

    I don't know *all* the location of all the files - there may be more or less.

    I would like to remove the '.20070501' extension from all the files while keeping the files in the same directory.

    Thanks

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    here's one way:
    Code:
    find /path -type f -name "*.20070501" > file
    while read -r line; 
    do 
       mv "$line"  "${line%%.*}"
    done < file

  3. #3
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by ghostdog74 View Post
    here's one way:
    Code:
    find /path -type f -name "*.20070501" > file
    while read -r line; 
    do 
       mv "$line"  "${line%%.*}"
    done < file
    The temporary file is not necessary, IFS should be empty for the read, and to remove only the final extension, use '%', not '%%':

    Code:
    find /path -type f -name "*.20070501" |
    while IFS= read -r line
    do 
       mv "$line"  "${line%.*}"
    done

Posting Permissions

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