We help IT Professionals succeed at work.

Check out this week's podcast, "Dairy Farms to Databases: Community's Hand in Technology"Listen Now

x

append text to a file name in unix

852 Views
Last Modified: 2013-12-26
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

CERTIFIED EXPERT
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
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
Another way, with variable editing:

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

wmp
CERTIFIED EXPERT
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
                                 

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


CERTIFIED EXPERT
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
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
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'/'`
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.