Find command + replace the extension (.xxx) by *

Hello,

I'm on HP Unix and in a Job, I tried to extract all files from a folder, and replace the extension (.xxxx) by '*' , remove duplicates and move the result in a file..

Example :
Folder has : ABC, CCC.txt, CCC.sf, CCC.sfd, DDD

I need to generate and output file with :

    ABC*
    CCC*
    DDD*

the commands that I did (See below) is working, but it increased the size of the logs because in my folder it has more than 600 000 files.. Is I can used another command to doing it or do an echo off somewhere and echo on after the execution.. thanks

[ -f $MAITUT/temp_file_A ] && rm $MAITUT/temp_file_A
[ -f $MAITUT/BCK_TMP_FILES_TO_DELETE ] && rm $MAITUT/BCK_DATA_FILES_TO_DELETE


find * -type f -mtime -365 | while read FileName
do
echo ${FileName%.*}'*' >> $MAITUT/temp_file_A
done

# Remove duplicates

sort -u $MAITUT/temp_file_A > $MAITUT/BCK_DATA_FILES_TO_DELETE

Example of the Info that i have in the logs :

...

+ read FileName
+ echo ARCAEDF*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCAMF*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCAMF*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCCC*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCCC*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCCD*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCCD*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName

...

Thanks a lot
Alain

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

1 Like

Someone left a set -x in your script by accident, causing it print every line of execution to stderr. Remove the set -x and it will stop doing that.

... or the script is run with "sh -x"; the -x option also turns on the debug mode.

The following will run faster, and log less in debug mode

find * -type f -mtime -365 |
# replace a trailing .xxx with *
  sed 's/[.][^.]*$//' |
# Remove duplicates
  sort -u > $MAITUT/BCK_DATA_FILES_TO_DELETE

The pipe passes the output to the next command without a temp file.

1 Like

The request seems to be to add a .* to every single file name, have it an extension or not. Try this small adaption to MadeInGermany's proposal:

find * -type f -mtime -365 | sed 's/[.][^.]*$//; s/$/.*/' | sort -u
1 Like

Many thank to all of you, I used both and run very well & fast..

set +x
 find * -type f -mtime -365 | sed 's/[.][^.]*$//; s/$/*/' | sort -u > $MAITUT/FILES_TO_DELETE

My previous post missed the replacement * in sed 's/[.][^.]*$/*/' .
But still this is different from the original while loop: it does not add the * when there is no .xxxx extension.

BTW your last post has ICODE tags instead of Code tags. The latter work much better for multi-line code blocks.