3

I have some files named like this:

file1.c.keep.apple

file2.c.keep.apple

I am trying to write a shell script so that I pass in the suffix as an argument (in this case, apple) and it will rename all of the files removing the .keep.apple.

Example execution:

script.sh apple

results in the files above being renamed to

file1.c

file2.c

So far, I have

 #! /bin/sh
 find . -type f -name \'*.keep.$1\' -print0 | xargs -0 rename 's/\(.keep.*)$//'

and the files do not get renamed. I know the find portion is correct. I am thinking the regex on my rename is wrong. How can I get the script working the way I want?

4
  • Does it need to be done recursively through several sub directories? Jan 4, 2013 at 19:52
  • yes, sorry. I want it to work when I execute this script from a directory, and it searches the current dir and all subdirs. Jan 4, 2013 at 19:58
  • Check my edited answer, it seems to do what you want. Jan 4, 2013 at 20:12
  • Updated my answer again based on your needs and comments made by Uwe Kleine-König. You should review the answers and comments again! Jan 4, 2013 at 20:32

6 Answers 6

4

I know the find portion is correct

Except it isn't.

find . -type f -name "*.keep.$1" -print0 | ...
6
  • I had it as \'*.keep.$1\' so that the $1 was recognized as an argument. With your solution isn't it going to look for the string literal $1 in the filename? Jan 4, 2013 at 19:58
  • 2
    No, it isn't. And as an added bonus it won't let spaces in $1 destroy your system. Jan 4, 2013 at 19:59
  • Oh, interesting. Thanks. Regardless, the xargs portion must be wrong because the script still doesn't work with your change. Jan 4, 2013 at 20:02
  • 1
    Your parens are unbalanced. Jan 4, 2013 at 20:03
  • With \'*.keep.$1\' you search for files who's name start with '. Jan 4, 2013 at 20:04
3

Updated, try this perhaps:

#!/bin/bash

SUFFIX=$1;

find . -type f -name "*keep.${SUFFIX}" | while read -r file;
do 
    nfile=`echo $file | sed "s/\.keep\.${SUFFIX}//g"`; 
    mv "$file" "$nfile" 2>/dev/null; 
done

here it is running:

jgalley@jgalley-debian:/test5$ cat replace.sh 
#!/bin/bash

SUFFIX=$1;

find . -type f -name "*keep.${SUFFIX}" | while read -r file;
do 
    nfile=`echo $file | sed "s/\.keep\.${SUFFIX}//g"`; 
    mv "$file" "$nfile" 2>/dev/null; 
done
jgalley@jgalley-debian:/test5$ find .
.
./-filewithadash.keep.apple
./dir1
./dir1/file
./dir1/file2.keep.orange
./dir2
./dir2/file2
./file with spaces
./file.keep.orange
./file.keep.somethingelse.apple
./file.orange
./replace.sh
jgalley@jgalley-debian:/test5$ ./replace.sh apple
jgalley@jgalley-debian:/test5$ find .
.
./-filewithadash
./dir1
./dir1/file
./dir1/file2.keep.orange
./dir2
./dir2/file2
./file with spaces
./file.keep.orange
./file.keep.somethingelse.apple
./file.orange
./replace.sh
jgalley@jgalley-debian:/test5$ 
7
  • 1
    This doesn't address the ".keep" part but uses "file" instead, though. Also note that this doesn't work if the filenames contain spaces (which might or might not be important) Jan 4, 2013 at 19:59
  • I didn't test, but I'd say you need sed "s/.keep.${SUFFIX}//". And this is still broken as . is special. Jan 4, 2013 at 20:15
  • I changed the find command to be "*.keep.$1" and the sed to be "s/.keep.${SUFFIX}//g" and its working for me!! Jan 4, 2013 at 20:19
  • 1
    @unexpected62: you want to quote the dots and add a $ at the end for the sed expression, yielding: s/\.keep\.${SUFFIX}$//. Otherwise you will have funny results on filenames like file.keep.applepie.keep.apple. Jan 4, 2013 at 20:24
  • I found another thing that could be bad :-) If you have a filename starting with - the echo and mv commands might not do what they should. Jan 4, 2013 at 20:35
1

I'd say you need:

find . -type f -name "*.keep.$1" -print0 | xargs -0 rename "s/\.keep\.$1$//"

Note the following limitations:

  • rename might not be available everywhere.
  • find -print0 and xargs -0 are GNU extensions which might not be available on all Unixes.
  • if your first parameter contains characters that are special for regexes the result might not what you want. (e.g. yourscript "a*e")
2
  • @unexpected62: hmm, works for me (even before I made the command more robust with some edits after your comment). I did: mkdir adir; touch adir/file2.c.keep.apple; touch file1.c.keep.apple; set apple; find . -type f -name "*.keep.$1" -print0 | xargs -0 rename "s/\.keep\.$1$//" Jan 4, 2013 at 20:27
  • what does $1 stand for in your scripts Mar 9, 2013 at 5:56
1

How about this?

[spatel@us tmp]$ x=aa.bb.cc
[spatel@us tmp]$ y=${x%.cc}
[spatel@us tmp]$ echo $y
aa.bb


[spatel@us tmp]$ x=aa.bb.cc
[spatel@us tmp]$ y=${x%.bb.cc}
[spatel@us tmp]$ echo $y
aa
1
  • This is brilliant! I have used Linux for 26 years and yet did not know this. More details here.
    – Jylpah
    Oct 8, 2023 at 13:07
1

If you can assume bash, and a version of bash greater then 4 (with globstar support), here is a clean bash-only solution:

#!/usr/bin/env bash

(($#)) || exit 1

shopt -s globstar nullglob
for f in **/*.keep."$1"; do
    mv -- "$f" "${f%.keep.$1}"
done

Alternatively, here is a solution using find and a while read loop (assumes GNU or BSD find):

find . -type f -name "*.keep.$1" -print0 | while IFS= read -r -d '' f; do
    mv -- "$f" "${f%.keep.$1}"
done

See http://mywiki.wooledge.org/BashFAQ/030 for more details on this solution.

Also, you can implement what you are trying to do using find with -exec:

find . -type f -name "*.keep.$1" -exec sh -c 'mv -- "$2" "${2%.keep.$1}"' _ "$1" {} ';'
4
  • In the last find solution: in the exec statement, you probably mean bash -c 'mv -- "$1" "${1%.keep.$0}"' "$1" {}. And it fails in some ways if either $1 is * or if $1 is ; Jan 5, 2013 at 20:58
  • No, I meant what I typed. The only differences between what I wrote and what you had suggested is the use of 'bash', and a shift of the positional parameters. 'bash' is not necessary, since POSIX sh supports parameter expansion; your shift of positional parameters is not necessary, nor relevant. And, in fact, including '*' characters in the suffix works just fine, since the shell does not do glob expansion when arguments are inside double quotes. Jan 5, 2013 at 21:39
  • I came across this solution while searching on net for some thing I am not clear with use of 'mv -- "$2" "${2%.keep.$1}"' what does ${2%.keep.$1} and $2 stand for and use of underscore in your solution "$1" {} and then use of ';' what kind of syntax is this Mar 9, 2013 at 6:00
  • ${2%.keep.$1} is a parameter expansion (see the bash man page); this PE expands to the value of $2 with the suffix .keep.$1 removed ($1 is expanded here before the removal). To better understand how $1 and $2 are set here, it helps to understand how sh -c works. sh -c 'echo "$1"' _ "hello, world!" , for example, will spawn a shell to run the 'echo "$1"' command string, and set the positional arguments to: $0 = _; $1 = "hello, world!" (the underscore is there as a placeholder for $0). The rest is wrapping this all up with find. Hope that helps! Mar 10, 2013 at 15:55
0

If you can simply glob the files, you can do just

rename '.keep.apple' '' *

otherwise you would replace the * with the find + xargs you already have.

Note in this by rename is meant rename from util-linux. On some systems it is installed like rename.ul instead of rename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.