[i=s] 本帖最后由 gogo11 于 2011-4-22 18:49 编辑 [/i]
用一行。我想用管道形式的,却不知道怎么传递参数。还有其他方法吗?
gogo11@ubuntu:~/temp$ ls
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt haha
gogo11@ubuntu:~/temp$
----------------------------------------------------------------------------------------
gogo11@ubuntu:~/temp$ ls | grep .txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
这个管道能不能把它结果传递给cp命令?
比如ls | grep .txt | cp 后边的不会。
这个问题如果用shell脚本怎么实现?
cp *.txt *.txt.bak怎么搞???
gogo11 于 2011-04-24 12:17:12发表:
都是高手!!!!!!!
yanyongkg 于 2011-04-23 23:58:34发表:
[i=s] 本帖最后由 yanyongkg 于 2011-4-24 00:02 编辑 [/i]
楼主2楼的问题:如何把其他文件拷贝的haha目录?
cp `ls -1 -F |grep -v [/$]` haha #-1是数字1不是字母l
楼主1楼的问题:
9楼的方法很简洁实用!每个分号写一行就是脚本,跟age贴出的脚本一样的道理,for ... in ... ... ...
gogo11 于 2011-04-23 21:11:00发表:
经测试,那个cp命令有问题,删掉重新自己输入一个cp就好了~~~~
huangyandong 于 2011-04-23 17:42:45发表:
for i in $(ls *.txt);do cp $i ${i}.bak;done
相思爱文 于 2011-04-23 12:58:50发表:
用shell脚本很容易,搜索linux批量重命名
可用一行命令把脚本中的内容全包括,用分号间隔
sloepx 于 2011-04-23 08:47:10发表:
过来听课
age 于 2011-04-22 21:38:08发表:
是不是bash路径不对?
另外可以在脚本第二行加上 set -x
运行时可以显示出调试信息, 便于分析问题
gogo11 于 2011-04-22 20:33:34发表:
[i=s] 本帖最后由 gogo11 于 2011-4-22 20:55 编辑 [/i]
1 #!/bin/bash
2 DIR=/home/gogo11/temp
3 LST=$(ls $DIR | grep txt)
4 cd $DIR
5 for file in $LST
6 do
7 cp $file $file.bk
8 done
~
执行效果:
gogo11@ubuntu:~/temp$ ./cp_test
./cp_test: 行 7: : 未找到命令
./cp_test: 行 7: : 未找到命令
./cp_test: 行 7: : 未找到命令
./cp_test: 行 7: : 未找到命令
./cp_test: 行 7: : 未找到命令
./cp_test: 行 7: : 未找到命令
./cp_test: 行 7: : 未找到命令
./cp_test: 行 7: : 未找到命令
./cp_test: 行 7: : 未找到命令
gogo11@ubuntu:~/temp$ sh cp_test
cp_test: 8: : not found
cp_test: 8: : not found
cp_test: 8: : not found
cp_test: 8: : not found
cp_test: 8: : not found
cp_test: 8: : not found
cp_test: 8: : not found
cp_test: 8: : not found
cp_test: 8: : not found
gogo11@ubuntu:~/temp$
原来带sh和不带sh有差别的~~~~~~~~~~~
为什么没有找到命令!?
age 于 2011-04-22 19:19:47发表:
[i=s] 本帖最后由 age 于 2011-4-22 19:26 编辑 [/i]
[code]#!/bin/bashDIR=/home/allblue/test
LST=$(ls $DIR | grep txt)
cd $DIR
for file in $LST
do
cp $file $file.bk
done[/code]
lykginy 于 2011-04-22 19:13:04发表:
这个不太懂 但是用 find 可以
gogo11 于 2011-04-22 18:50:05发表:
[i=s] 本帖最后由 gogo11 于 2011-4-22 18:52 编辑 [/i]
o(∩∩)o...哈哈,linux里边管道用的多,dos下边很少用这个东西,不是很懂。。。。
还有一个,比如说:
gogo11@ubuntu:~/temp$ ls
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt.bak 7.txt 8.txt 9 haha
其中haha是目录,其他是任意文件。
如何把其他文件拷贝的haha目录?