2013-02-15 51 views
0

我需要这个bash脚本:如何用bash重命名文档?

#!bin/bash 
echo "Checking if root…." 
if [[ $(/usr/bin/id -u) -ne 0 ]]; then 
    echo "Not running as root" 
    exit 
fi 
echo -ne "Downloading NEW hosts blocking file: " 
wget -qO /tmp/hosts.txt http://deathsrepo.webege.com/hosts.txt 
echo "DONE!" 
mv /tmp/hosts.text -b etc/ 
wget -qO /tmp/hosts.bak http://deathsrepo.webege.com/hosts.bak 
mv /tmp/hosts.bak -b etc/ 
rename .txt . *.text 

为了能够hosts.text重命名为主机。同样当这个软件包被删除时,我需要hosts.bak命名为主机。

+0

你指的是什么“包装”? – vezult 2013-02-15 03:13:06

回答

0

要重命名文件,只需将其移动:

mv hosts.text /etc/hosts 

如果你的目的地是一个目录,你的文件被移动到它,名称不变:

mv hosts.text /etc/ # file is moved to /etc/hosts.text 

如果目的地是文件,并且目标文件存在,则将源移动到指定的目标文件,并且该目标文件被覆盖:

mv hosts.text /etc/hosts # file is moved and renamed to /etc/hosts 

如果目标不存在,并没有结尾的斜线,您的文件将会被重命名为目标:

mv hosts.text foobar.txt # file is renamed to foobar.txt 
+0

我不能这样做,这是在iPhones,iPod和iPad上完成的我需要将hosts.text重命名为主机,并且我将在重命名主机之前将脚本移除到原始主机文本文档。文本 – user1996894 2013-02-15 03:16:02

+0

@ user1996894:你需要在你的问题中概述你的环境的限制。正如你的问题所表达的那样,我的回答完全有效。 – vezult 2013-06-09 18:21:04

0

1 - 删除原主机:

rm /etc/hosts 

2 - 重命名hosts.text:

mv /tmp/hosts.text /tmp/hosts 

3 - 移动hosts.text的/ etc:

mv /tmp/hosts.text /etc 

您可以移动同时重命名MV。我还会建议制作备份而不删除原有的主机:

mv /etc/hosts /etc/hosts.bak # Make a backup 
mv /tmp/hosts.text /etc/hosts # Move and rename 

我并没有真正得到你想要做什么,但我觉得你的完整剧本,将成为这样的事情:

#!bin/bash 
echo "Checking if root…." 
if [[ $(/usr/bin/id -u) -ne 0 ]]; then 
    echo "Not running as root" 
    exit 
fi 
echo -ne "Downloading NEW hosts blocking file: " 
wget -qO /tmp/hosts.txt http://deathsrepo.webege.com/hosts.txt 
echo "DONE!" 
mv /tmp/hosts.txt /etc/hosts # replace original hosts with downloaded hosts.txt 
wget -qO /tmp/hosts.bak http://deathsrepo.webege.com/hosts.bak 
mv /tmp/hosts.bak /etc  # move downloaded hosts.bak to /etc