Rename multiple files on Android

I am trying to write a script using the Android shell to rename all files of a given extension, to add the .bak extension, and the other is to delete it.

This works to add the .bak extension to all PDF files in the directory

ext=.bak for f in *.pdf do mv $f $f$ext done 

But bash string slicing using ${varname:index:length} does not work in the Android shell, so I don’t understand how to remove the extension. Does anyone have any ideas?

EDIT: to clarify, I'm trying to find a way to remove the last four characters of a string in an android shell. Other solutions that I have not considered that will solve my specific problem are also welcome.

UPDATE: Based on this answer, the following code will remove the file extension from any file that has this extension in the current directory in the unmodified Android shell (where .bak can be replaced with the extension of your choice)

 for f in *.bak do mv $f ${f%.*} done 
+4
source share
2 answers

You tried to remove a substring

 fname="test.abc.bak" mv $fname ${fname%.*} 

thanks

+1
source

I have code, but it uses a temporary file, just try it and check for your usefulness

 ls -l |awk '{print $9}' >/list cat /list |while read line do echo $line >/tempfile first=`cut -f1 -d'.' /tempfile` echo $first >>/output.txt mv $line $first done 
0
source

All Articles