1

Trying to figure out how to include several rename commands in one shell script that I will save as a cron job.

What I would like to accomplish is renaming different words and also suppress error messages (hence writing > /dev/null 2>&1). I have tried to save the below two lines to rename.sh which only executes the last one (HELLO->hello).

find (path) -maxdepth 1 -type f -exec rename TEST test {} \; > /dev/null 2>&1 find (path) -maxdepth 1 -type f -exec rename HELLO hello {} \; > /dev/null 2>&1 

How should I rewrite?

Example filenames:

  • file1_HELLO.sas7bdat
  • file2_TEST.sas7bdat
  • file3_TEST_HELLO.sas7bdat
8
  • Please edit your question and give us more context. Are you showing us individual commands? A script? How is that being run? Where is the crontab line? What is your operating system? How is it failing? What is the expected behavior? What do you get instead? Can you show us some example file names so we can test it? Commented Sep 3, 2024 at 12:02
  • @terdon sorry, I thought I was clear. It will be run as a shell script. I have tried saving it as being run every 10 min in crontab - why I know that the above only executes the last command (renaming files including HELLO to hello). What I would like is to be able to have the two commands in the same shell script. Rename from util-linux 2.23.2 Commented Sep 3, 2024 at 12:07
  • I don't see any reason why the first one wouldn't run. Can you show the actual script? Is there a shebang? What happens if you run the script manually? Does it work then? What if you redirect stderr to a file instead of /dev/null when you run with cron, anything useful? Commented Sep 3, 2024 at 12:17
  • are there any error messages? Commented Sep 3, 2024 at 14:28
  • @terdon I tried running the script manually, but the outcome is the same (only renames the "HELLO" in filenames). I aslo omit the /dev/null part altogether but has no impact. The script is identical to what I posted here, but with the intended path. When I run the commands separately they work fine Commented Sep 3, 2024 at 15:02

1 Answer 1

0

With zsh:

autoload -Uz zmv zmv -n '*(#qD.)' '${f//(#m)(HELLO|TEST)/$MATCH:l}' 

(remove the -n (dry-run) if happy)

Would turn to lower case all occurrences of HELLO and TEST in the name of the regular files in the current working directory.

Beware that the rename implementation from util-linux which you seem to be using only replaces the first occurrence of the string.

Better to use the perl variants of rename which are a lot less limited (they are limit-less as they embed a perl interpreter). The most commonly found one these days is the one from https://metacpan.org/release/File-Rename, with which you can do:

find . -maxdepth 1 -type f -print0 | rename -d -0 -n 's/HELLO|TEST/\L$&/g' 

(with again, -n for dry-run).

Note: to do the same for non-ASCII file names encoded in UTF-8, use for instance:

find . -maxdepth 1 -type f -print0 | PERL_UNICODE=ASD rename -d -0 -n 's/STÉPHANE/\L$&/g' 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.