append text to a file name in unix

sunshine737
sunshine737 used Ask the Experts™
on
Hi,
I have a file name /path/main/file.txt

i want to append text "processing_" to the file name

output should look like  
/path/main/processed_file.txt



Thanks for your help.

Regards,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2013
Top Expert 2013

Commented:
FILE="/path/main/file.txt"
NEWFILE=$(dirname $file)"/processed_"$(basename $file)
echo "$NEWFILE"

finally

mv $FILE $NEWFILE

or, all in one

FILE="/path/main/file.txt"
mv $FILE $(dirname $file)"/processed_"$(basename $file)

wmp
Most Valuable Expert 2013
Top Expert 2013

Commented:
Another way, with variable editing:

FILE="/path/main/file.txt"
NEWFILE=${FILE%/*}"/processed_"${FILE##*/}

wmp
Most Valuable Expert 2013
Top Expert 2013

Commented:
Sorry, in my first post I used lowercase accidentally. Corrected:

  FILE="/path/main/file.txt"
NEWFILE=$(dirname  $FILE)"/processed_"$(basename $FILE)
echo "$NEWFILE"

finally

mv  $FILE $NEWFILE

or, all in one

FILE="/path/main/file.txt"
mv  $FILE $(dirname $FILE)"/processed_"$(basename $FILE)

wmp
                                 
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Author

Commented:
can it be done in a single line?

Author

Commented:
for some restrictions,i cannot use the script. i can only pass a command as single line.

Thanks
Single line

mv  "/path/main/file.txt $(dirname $FILE)"/processed_"$(basename $FILE)



Mike
oops typo

Single line

mv  "/path/main/file.txt" $(dirname $FILE)"/processed_"$(basename $FILE)



Mike

Author

Commented:
I am getting filename by using a command(i just need to pick the latest file  and then rename it).
for picking the latest file, i used:     ls -rtd1 /path/main/file*.txt | head -1

so i need to append the output of the above command , as input to the mv command.

sorry for not informing earlier.

Thanks


Most Valuable Expert 2013
Top Expert 2013

Commented:
ls -rtd1 /path/main/file*.txt | head -1 |while read FILE ; do mv $FILE ${FILE%/*}"/processed_"${FILE##*/} ; done

or

ls -rtd1 /path/main/file*.txt | head -1 |while read FILE; do  mv $FILE $(dirname $FILE)"/processed_"$(basename  $FILE) ; done

wmp
Most Valuable Expert 2013
Top Expert 2013
Commented:
Another way to get it done:

FILE=$(ls -rtd1 /path/main/file*.txt | head -1) ; mv $FILE ${FILE%/*}"/processed_"${FILE##*/}

FILE=$(ls -rtd1 /path/main/file*.txt | head -1) ; mv $FILE $(dirname  $FILE)"/processed_"$(basename  $FILE)

 
The below will work as a single command


mv  `ls -t /path/main/file*.txt | head -1` /path/main/processed_`ls -t /etc/* | head -1 | cut -f4 -d'/'`
oops typo
the below will work as a single mv command

mv  `ls -t /path/main/file*.txt | head -1` /path/main/processed_`ls -t /path/main/file*.txt | head -1 | cut -f4 -d'/'`
Mike

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial