【linux練習】基礎作業一

1. 創建一個目錄/data。 
[root@StudyServer[1] oldboystudy]# mkdir data
[root@StudyServer[1] oldboystudy]# ll
total 4
drwxr-xr-x. 2 root root 4096 Apr 11 09:49 data
2. 在/data下面建立一個文件oldboy.txt。 
[root@StudyServer[1] oldboystudy]# cd data/
[root@StudyServer[1] data]# touch oldboy.txt
[root@StudyServer[1] data]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 11 09:53 oldboy.txt
[root@StudyServer[1] data]#
3. 爲oldboy.txt增加內容爲“I am studying linux.”。 
[root@StudyServer[1] data]# vim oldboy.txt 
[root@StudyServer[1] data]# cat oldboy.txt 
i am studying linux.
[root@StudyServer[1] data]#
4. 把oldboy.txt文件拷貝到/tmp下。 
[root@StudyServer[1] data]# cp oldboy.txt /tmp 
[root@StudyServer[1] data]# ll /tmp/
total 8
-rw-r--r--. 1 root root   21 Apr 11 09:55 oldboy.txt
drwx------. 2 root root 4096 Jan 13 16:31 pulse-GmmRjFD7HG5z
[root@StudyServer[1] data]#
5. 把/data目錄移動到/root下。
[root@StudyServer[1] oldboystudy]# ll
total 4
drwxr-xr-x. 2 root root 4096 Apr 11 10:01 data
[root@StudyServer[1] oldboystudy]# mv  data/  /root
[root@StudyServer[1] oldboystudy]# ll
total 0
[root@StudyServer[1] oldboystudy]# cd
[root@StudyServer[1] ~]# ll
total 108
-rw-r--r--. 1 root root   143 Mar 16 14:39 !
-rw-------. 1 root root  1554 Dec 22 12:21 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Apr 11 10:01 data

[root@StudyServer[1] ~]# ll data/
total 4
-rw-r--r--. 1 root root 21 Apr 11 09:55 oldboy.txt
[root@StudyServer[1] ~]#  
6. 進入/root目錄下的data目錄,刪除oldboy.txt文件。 
[root@StudyServer[1] ~]# ll data/
total 4
-rw-r--r--. 1 root root 21 Apr 11 09:55 oldboy.txt
[root@StudyServer[1] ~]# 
[root@StudyServer[1] ~]# 
[root@StudyServer[1] ~]# 
[root@StudyServer[1] ~]# cd data/
[root@StudyServer[1] data]# rm -f oldboy.txt    #-f表示強制刪除文件
[root@StudyServer[1] data]# ll
total 0
[root@StudyServer[1] data]#
7. 接第6題,退出到上一級目錄,刪除data目錄。 
[root@StudyServer[1] data]# cd
[root@StudyServer[1] ~]# rm -f data/
rm: cannot remove `data/': Is a directory   #必須加上-r參數進行遞歸刪除
[root@StudyServer[1] ~]# rm -rf data/     #遞歸強制刪除目錄
[root@StudyServer[1] ~]#
8. 已知文件test.txt內容爲:
test
liyao	
oldboy 
9. 請給出輸出test.txt文件內容時,不包含oldboy字符串的命令。 
[root@StudyServer[1] oldboystudy]# cat test.txt 
test
liyao
oldboy
[root@StudyServer[1] oldboystudy]# tail -n 2 test.txt 
liyao
oldboy
[root@StudyServer[1] oldboystudy]#
10. 請用一條命令完成創建目錄/oldboy/test,即創建/oldboy目錄及/oldboy/test目錄 
[root@StudyServer[1] oldboystudy]# mkdir -p  /oldboy/test
[root@StudyServer[1] oldboystudy]# cd /oldboy/test/
[root@StudyServer[1] test]#
Mkdir常用的參數介紹:
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
#-p參數表示當父目錄不存在是直接創建父目錄,如該題中/oldboy目錄不存在的情況下創建test這個目錄時,需要使用-p參數自動創建test目錄的父目錄。
  -v, --verbose     print a message for each created directory
  -Z, --context=CTX  set the SELinux security context of each created
                      directory to CTX
      --help     display this help and exit
      --version  output version information and exit
11. 已知/tmp下已經存在test.txt文件,如何執行命令才能把/mnt/test.txt拷貝到/tmp下覆蓋掉/tmp/test.txt,而讓系統不提示是否覆蓋(root權限下)。 
[root@StudyServer[1] ~]# cp -f -r  /mnt/test.txt /tmp/test.txt 
cp: overwrite `/tmp/test.txt'? 
[root@StudyServer[1] ~]#
#理論上來說,-r表示遞歸,-f表示強制。增加了-r和-f參數不會再提示是否覆蓋的情況,但實際情況仍然會提示是否覆蓋,什麼原因呢?是別名在作怪,請看下面:
[root@StudyServer[1] ~]# 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'
這就意味着,執行cp命令實際上執行的是cp –i命令。那麼我們想要通過cp命令在不提示覆蓋的前提下實現覆蓋,就需要修改alias。
[root@StudyServer[1] ~]# cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
#alias cp='cp -i'   #將cp相關的alias註釋掉
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
[root@StudyServer[1] ~]#
#註釋掉後重新登錄即可。
附上cp命令相關參數:
-R, -r, --recursive          copy directories recursively
      --reflink[=WHEN]         control clone/CoW copies. See below.
      --remove-destination     remove each existing destination file before
                                 attempting to open it (contrast with --force)
      --sparse=WHEN            control creation of sparse files. See below.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 Argument
-f, --force                  if an existing destination file cannot be
                                 opened, remove it and try again (redundant if
                                 the -n option is used)
12. 只查看/etc/passwd文件(共100行)內第20到第30行的內容 
head -n 30 /etc/passwd | tail -n +20


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章