Search code examples
bashmovefile-extension

How can move the file and change the file extension with simple bash?


Select file which contains sqlite in my current directory:

ls | grep sqlite
null and empty string in sqlite.txt
pythoin write None into sqlite.txt

Move them into /tmp directory:

ls |grep sqlite |xargs -i mv {}  /tmp
ls /tmp | grep sqlite
null and empty string in sqlite.txt
pythoin write None into sqlite.txt

How can move them into /tmp and change the file extension as rst at the same time?

ls |grep sqlite | some_simple_bash_command

After executing it:

ls /tmp | grep sqlite
null and empty string in sqlite.rst
pythoin  write None   into sqlite.rst     

@David C. Rankin,grep sqlite * can't work at all!

enter image description here


Solution

  • Try this Shellcheck-clean code:

    for f in *sqlite*; do
        mv -v -- "$f" "/tmp/${f%.*}.rst"
    done