1.1 创建一个目录 /data
解答:[[email protected]~]# mkdir /data
[[email protected] ~]# cd /
[[email protected] /]# ls
bin data etc lib lost+found mnt proc sbin srv tmp var
boot dev home lib64 media opt root selinux sys usr
1.2 在/data下面创建文件oldboy.txt
解答[[email protected]/]# touch /data/oldboy.txt
[[email protected] /]# ls -l /data/oldboy.txt
-rw-r--r--. 1 root root 0 Mar 10 04:13/data/oldboy.txt
1.3 为oldboy.txt增加内容"I am studying linux."(至少三种方法)
1.3.1方法一
vi /vim
[[email protected] /]# vi /data/oldboy.txt
I am studying linux.
[[email protected] /]# cat /data/oldboy.txt
I am studying linux.
1.3.2方法二
echo
[[email protected] data]# echo "I am studyinglinux" >> /data/oldboy.txt
[[email protected] data]# cat /data/oldboy.txt
I am studying linux .
解释:
#把echo的结果输出到 /data/oldboy.txt内
#输出到/data/oldboy.txt的最后一行
echo "Iam studying " >>/data/oldboy.txt
cat /data/oldboy.txt
>>表示把文件内容放到最后一行
>表示先清空文件内容,然后把你想要的内容追加到最后一行
1.3.3方法三
cat
[[email protected] /]# cat >> /data/oldboy.txt << EOF
> I am studying linux
> EOF
[[email protected] /]# cat /data/oldboy.txt
I am studying linux
I am studying linux
1.4 把 oldboy.txt 文件拷贝到/tmp 下。
解答:[[email protected]]# cp /root/data/oldboy.txt /tmp/
cp: overwrite `/tmp/oldboy.txt‘? y
[[email protected] data]# ls /tmp/
oldboy.txt yum.log
1.5 把/data 目录移动到/root 下。
解答:[[email protected]]# mv /tmp/data /root
[[email protected] data]# ls /root
anaconda-ks.cfg history.txt lrzsz-0.12.21-21.mga6.x86_64.rpm oldboy
data hudson nginx oldboy.txt
file01.txt hudson.txt nginx.conf test.txt
file.txt logon npp_7.3.2_Installer.exe
1.6 进入/root 目录下的 data 目录,删除 oldboy.txt 文件。
1.6.1方法一:rm -f
[[email protected] data]# cd /data
[[email protected] data]# ls
shahe.txt test.txt wangyao.txt
[[email protected] data]# touch oldboy.txt
[[email protected] data]# ls
oldboy.txt shahe.txt test.txt wangyao.txt
[[email protected] data]# rm -f oldboy.txt
[[email protected] data]# ls
shahe.txt test.txt wangyao.txt
1.6.2方法二:find ./ type f –name “oldboy.txt” |xargs rm -f
[[email protected] data]# cd /data
[[email protected] data]# ls oldboy.txt
shahe.txt test.txt wangyao.txt
[[email protected] data]# find ./ -type f-name "oldboy.txt" |xargs rm –f
[[email protected] data]# ls
shahe.txt test.txt wangyao.txt
1.6.3方法三:find ./ -type f -name "oldboy.txt" -exec rm -f {} \;
[[email protected] data]# touch oldboy.txt
[[email protected] data]# ls
oldboy.txt shahe.txt test.txt wangyao.txt
[[email protected] data]# find ./ -type f-name "oldboy.txt" -exec rm -f {} \;
[[email protected] data]# ls
shahe.txt test.txt wangyao.txt
1.7 接第 6 题,退出到上一级目录,删除 data 目录。
解答:cd .. rmdirdata【rmdir 代表删除空目录,只能使用删除空目录】
[[email protected] data1]# cd ..
[[email protected] /]# rmdir data1
1.8 已知文件 test.txt 内容为:
test
liyao
oldboy
请给出输出 test.txt文件内容时,不包含oldboy 字符串的命令。
1.8.1方法一:
[[email protected] /]# cat test.txt
test liyao
oldboy
[[email protected] /]# grep -v"oldboy" test.txt
test liyao
1.8.2方法二:
[[email protected] /]# cat test.txt
test liyao
oldboy
[[email protected] /]# sed ‘/oldboy/d‘test.txt
test liyao
1.9 请用一条命令完成创建目录/oldboy/test,即创建/oldboy 目录及/oldboy/test 目录。
解答:
[[email protected] ~]# mkdir -p /data/test
1.10 已知/tmp 下已经存在 test.txt 文件,如何执行命令才能把/mnt/test.txt 拷贝到/tmp 下覆盖掉/tmp/test.txt,而让系统不提示是否覆盖
解答:[[email protected]/]# mkdir -p /oldboy/test
[[email protected] /]# ls
app data home lost+found oldboy proc selinux test usr
bin dev lib media oldgirl root srv test.txt var
boot etc lib64 mnt opt sbin sys tmp
[[email protected] /]# ls -ld oldboy/
drwxr-xr-x. 3 root root 4096 Mar 14 10:16oldboy/
[[email protected] /]#
\cp /mnt/test.txt /tmp/test.txt
/bin/cp /mnt/test.txt /tmp/test.txt
1.11 给rm命令创建一个别名,在执行rm命令的时候,提示" do not use rm command"
解答:[[email protected]~]# alias
alias cp=‘cp -i‘
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias mv=‘mv -i‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which--tty-only --read-alias --show-dot --show-tilde‘
[[email protected] ~]# alias rm=‘echo do notuse rm command‘
[[email protected] ~]# rm /data
do not use rm command /data
1.12 只查看 ett.txt 文件(共 100 行)内第 20 到第 30(至少两种方法)。
解答:
1.13 查找/data目录下名叫oldboy,以.txt结尾的所有文件
解答:find/oldboy/ -type f “txt”
1.14 如何查看/etc/passwds文件的行数?
解答:cat/etc/passwd |wc –l