0

I have a folder called /input/temp. Inside the folder I have lot of files. I need to find the file of pattern Article_????_test_?????????.txt and replace by format below.

Article_????_?????????.txt

Below is the code I tried and which doesn't work:

echo "Please provide the file name Corresponding to DC..."
read file 
ls $HOME/*.txt | grep $file
if [ $? -eq 0 ]
find . $file '*_Test_*' -exec bash -c 'mv $0 ${0/_Test/ }' {} \;

    if [ $? -eq 0 ]
find . $file -name "*.txt" -exec bash -c "mv {} \`echo {} | sed -e 's/[_TEST_]/_/g'\`" \;
then

I Got below error:

find: 0652-083 Cannot execute bash:: A file or directory in the path name does not exist.
find: 0652-083 Cannot execute bash:: A file or directory in the path name does not exist.

bash can't be executed on my platform.

1
  • What shell are you using then? What system is it? Commented Jan 8, 2014 at 8:47

4 Answers 4

1
  • Unless the file name is a regular expression you can use if [ -e "$file" ] instead of ls + grep + test.
  • When you do find . $file '*_Test_*' the last three parameters are actually taken as files or directories to search underneath! It will return
    • all files in the current directory,
    • all files in the directory $file or the path $file if it's not a directory, and
    • all files in any directories matching *_Test_* or their paths if they are not directories.
  • There's no need for bash - you can run mv directly in -exec. This is just extra complexity for no gain.
  • Use $(command) instead of command for much easier quote handling. Each $() has a separate quoting context, so you can do for example echo "$(command "$(c2 "argument with spaces")")".
Sign up to request clarification or add additional context in comments.

Comments

0

According to the link here: This should work

ls -1 Article_????test?????????.txt|awk '{old=$0;gsub(/test/,"_",$0);system("mv \""old"\" "$0)}'

2 Comments

The page linked above explains it.
0

Also try the 'rename' command.

rename 's/_test_/_/' *.txt

You can fine tune the regular expression...

Comments

0

Update from your code:

cd $HOME
find . -name '*_Test_*' |while read line
do
  echo mv ${line) ${line/_Test/}
done

If you need search the pattern Article_????test?????????.txt, try this

cd $HOME
find . -name 'Article_????_test_?????????.txt' |while read line
do
  echo mv ${line) ${line/_Test/}
done

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.