本文是我学习Hadoop搭建过程中的各种笔记,内容来自于各种公开的教程,起点非常低,从Linux基础开始,直至在PC上搭建Hadoop成功,是真正的从零开始。
感谢过程中帮助我的各位认识的和不认识的老师。
10、文件创建 -1:
1、文件操作:
进入(某个目录)操作 cd
进入home目录:
[root@hadoop01 ~]# cd /home/
查看home目录:
[root@hadoop01 home]# ll
total 4
drwx------. 4 aidon aidon 4096 Apr 19 01:43 aidon
2.创建文件夹和目录 (指定目录和不指定目录的区别)
查看当前所在目录 pwd
[root@hadoop01 home]# pwd
/home
[root@hadoop01 home]# cd ~
[root@hadoop01 ~]# pwd
/root
目录创建,单个,多个,递归创建:mkdir
mkdir test 或 mkdir ./ test1
mkdir test mkdir后不加东西,在现在目录下创建新的目录
[root@hadoop01 home]# mkdir test
[root@hadoop01 home]# ll
total 8
drwx------. 4 aidon aidon 4096 Apr 19 01:43 aidon
drwxr-xr-x. 2 root root 4096 Apr 19 04:17 test
mkdir ./test1 指在当前目录创建test1
[root@hadoop01 home]# mkdir ./test1
[root@hadoop01 home]# ll
total 12
drwx------. 4 aidon aidon 4096 Apr 19 01:43 aidon
drwxr-xr-x. 2 root root 4096 Apr 19 04:17 test
drwxr-xr-x. 2 root root 4096 Apr 19 04:32 test1
mkdir –p ./test2/aidon/ test3
同时创建多个,./在当前目录下创建test2,aidon,test3 mkdir -p ./test2/aidon/test3
必须加 –p 递归创建, ,否则报错
[root@hadoop01 home]# mkdir -p ./test2/aidon/test3
[root@hadoop01 home]# ll ./
total 16
drwx------. 4 aidon aidon 4096 Apr 19 01:43 aidon
drwxr-xr-x. 2 root root 4096 Apr 19 04:17 test
drwxr-xr-x. 2 root root 4096 Apr 19 04:32 test1
drwxr-xr-x. 3 root root 4096 Apr 19 04:36 test2
[root@hadoop01 home]# ll ./test2/
total 4
drwxr-xr-x. 3 root root 4096 Apr 19 04:36 aidon
[root@hadoop01 home]# ll ./test2/aidon/
total 4
drwxr-xr-x. 2 root root 4096 Apr 19 04:36 test3
在test2下创建aidon,在aidon下创建test3 ↑
在当前目录下创建 test4 ,test5. (在一个目录下创建多个,区别于递归创建)
[root@hadoop01 home]# mkdir ./test4 ./test5
[root@hadoop01 home]# ll
total 24
drwx------. 4 aidon aidon 4096 Apr 19 01:43 aidon
drwxr-xr-x. 2 root root 4096 Apr 19 04:17 test
drwxr-xr-x. 2 root root 4096 Apr 19 04:32 test1
drwxr-xr-x. 3 root root 4096 Apr 19 04:36 test2
drwxr-xr-x. 2 root root 4096 Apr 19 04:47 test4
drwxr-xr-x. 2 root root 4096 Apr 19 04:47 test5
mkdir /tmp/test01 //创建空文件夹【创建/在tmp下/创建01】
mkdir-p/data1/hadoop/hdfs/name //递归创建文件夹
创建空文件 touch /tmp/test01/file.txt
[root@hadoop01 home]# cd ./aidon/
[root@hadoop01 aidon]# ll
total 0
[root@hadoop01 aidon]# touch ./abc
[root@hadoop01 aidon]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 19 05:00 abc
创建文件:vi ./def
按i键进入编辑模式,输入78900000,按(冒号):wq ,保存退出
[root@hadoop01 aidon]# vi ./def
[root@hadoop01 aidon]# ll
total 4
-rw-r--r--. 1 root root 0 Apr 19 05:00 abc
-rw-r--r--. 1 root root 9 Apr 19 05:02 def root用户下,def里面有9个字节
vi file.log //创建空文件夹并进如编辑模式
创建file.txt并输人空到改文件中 echo>file.txt
[root@hadoop01 aidon]# echo '1234567890' >> ./gh
[root@hadoop01 aidon]# ll
total 8
-rw-r--r--. 1 root root 0 Apr 19 05:00 abc
-rw-r--r--. 1 root root 9 Apr 19 05:02 def
-rw-r--r--. 1 root root 11 Apr 19 05:06 gh
[root@hadoop01 aidon]# cat ./gh
1234567890
打印字符串”1234567890”,加到gh里面 :echo’1234567890’ >> ./gh
看当前文件的内容: cat ./gh 出来的就是:1234567890
3.列出文件和目录列表:
ls – a – h –l 【-a表示列出所有的】
ll – h(ls –l)
[root@hadoop01 aidon]# ll ./
total 8
-rw-r--r--. 1 root root 0 Apr 19 05:00 abc
-rw-r--r--. 1 root root 9 Apr 19 05:02 def
-rw-r--r--. 1 root root 11 Apr 19 05:06 gh
[root@hadoop01 aidon]# ll /home/
total 24
drwx------. 4 aidon aidon 4096 Apr 19 05:06 aidon
drwxr-xr-x. 2 root root 4096 Apr 19 04:17 test
drwxr-xr-x. 2 root root 4096 Apr 19 04:32 test1
drwxr-xr-x. 3 root root 4096 Apr 19 04:36 test2
drwxr-xr-x. 2 root root 4096 Apr 19 04:47 test4
drwxr-xr-x. 2 root root 4096 Apr 19 04:47 test5
[root@hadoop01 aidon]# ll -h /home/
total 24K
drwx------. 4 aidon aidon 4.0K Apr 19 05:06 aidon
drwxr-xr-x. 2 root root 4.0K Apr 19 04:17 test
drwxr-xr-x. 2 root root 4.0K Apr 19 04:32 test1
drwxr-xr-x. 3 root root 4.0K Apr 19 04:36 test2
drwxr-xr-x. 2 root root 4.0K Apr 19 04:47 test4
drwxr-xr-x. 2 root root 4.0K Apr 19 04:47 test5
[root@hadoop01 aidon]# ls /home/
aidon test test1 test2 test4 test5
[root@hadoop01 aidon]# ls -a /home/
. .. aidon test test1 test2 test4 test5
[root@hadoop01 aidon]# ls -l -a /home/
total 32
drwxr-xr-x. 8 root root 4096 Apr 19 04:47 .
dr-xr-xr-x. 26 root root 4096 Apr 18 21:55 ..
drwx------. 4 aidon aidon 4096 Apr 19 05:06 aidon
drwxr-xr-x. 2 root root 4096 Apr 19 04:17 test
drwxr-xr-x. 2 root root 4096 Apr 19 04:32 test1
drwxr-xr-x. 3 root root 4096 Apr 19 04:36 test2
drwxr-xr-x. 2 root root 4096 Apr 19 04:47 test4
drwxr-xr-x. 2 root root 4096 Apr 19 04:47 test5
1.3文件编辑器Vi
Vim/vi/nana file
:q //退出
:w //保存
:qw //保存退出
:q! //强制退出
创建123这个文件 vim/123
[root@hadoop01 aidon]# vi ./123
[root@hadoop01 aidon]# ll
total 12
-rw-r--r--. 1 root root 24 Apr 19 05:21 123
-rw-r--r--. 1 root root 0 Apr 19 05:00 abc
-rw-r--r--. 1 root root 9 Apr 19 05:02 def
-rw-r--r--. 1 root root 11 Apr 19 05:06 gh
创建12这个文件 vim/12
[root@hadoop01 aidon]# vim ./12
[root@hadoop01 aidon]# ll
total 12
-rw-r--r--. 1 root root 24 Apr 19 05:21 123
-rw-r--r--. 1 root root 0 Apr 19 05:00 abc
-rw-r--r--. 1 root root 9 Apr 19 05:02 def
-rw-r--r--. 1 root root 11 Apr 19 05:06 gh
创建后,强制退出,文件没能保留,所以没有12这个文件
1.4文件内容查看:
cat – n //查看内容时显示行号
cat/etc/redhat – release //不显示行号
cat – n/etc/passwd //查看内容时显示行号
查看123文件里面有什么内容:cat ./123
[root@hadoop01 aidon]# cat ./123
232423
rlersa
w
tw
r
所有的信息一次性读出来:cat /var/log/boot.log
[root@hadoop01 aidon]# cat /var/log/boot.log
Welcome to CentOS
Starting udev: [ OK ]
Mounting local filesystems: [ OK ]
…………
Starting certmonger: [ OK ]
打印出来信息的行号:-n
[root@hadoop01 aidon]# cat -n /var/log/boot.log
1Welcome to CentOS
2Starting udev: [ OK ]
3Setting hostname hadoop01: [ OK ]
…………
41Starting certmonger: [ OK ]
11、Linux中的文件操作 02-1:
1.查看文件
默认查看文件后10行 tail
[root@hadoop01 aidon]# tail /var/log/boot.log
Retrigger failed udev events [ OK ]
Loading autofs4: [ OK ]
Starting automount: [ OK ]
Enabling Bluetooth devices:
Starting sshd: [ OK ]
Starting postfix: [ OK ]
Starting abrt daemon: [ OK ]
Starting crond: [ OK ]
Starting atd: [ OK ]
Starting certmonger: [ OK ]
查看文件后几(2)行:tail-n tail -2
[root@hadoop01 aidon]# tail -2 /var/log/boot.log
Starting atd: [ OK ]
Starting certmonger: [ OK ]
tail多用来监控某个文件 tail -f ./123
[root@hadoop01 aidon]# tail -f ./123
232423
rlersa
w
tw
r
hello world
hello world88888
等着向里面输入东西,按Ctrl + C,退出来
再克隆一个hadoop01,在克隆的里面输入,同时hadoop01中就会呈现出来,即可以实现动态监控:
echo ‘hello world’ >>/home/aidon (输入‘hello world’到home的aidon里面)
echo ‘hello world88888’ >>/home/aidon
默认查看文件前10行 head
[root@hadoop01 aidon]# head /var/log/boot.log
Welcome to CentOS
Starting udev: [ OK ]
Setting hostname hadoop01: [ OK ]
Setting up Logical Volume Management: No volume groups found
[ OK ]
Checking filesystems
/dev/sda3: clean, 98756/3129344 files, 1080699/12505856 blocks
/dev/sda1: clean, 38/76912 files, 44307/307200 blocks
[ OK ]
Remounting root filesystem in read-write mode: [ OK ]
查看指定头几行 -n 数字 -n表示看多少行
[root@hadoop01 aidon]# head -2 /var/log/boot.log
Welcome to CentOS
Starting udev: [ OK ]
cat
cat – n
tail –n
tail –f 动态监控文件数据
more 按百分比查看,不能回滚看
[root@hadoop01 aidon]# more /var/log/boot.log
Welcome to CentOS
Starting udev: [ OK ]
Setting hostname hadoop01: [ OK ]
…………
Bringing up loopback interface: [ OK ]
按Enter键,向下翻看,按q退出来
less 能往回滚看
[root@hadoop01 ~]# less /var/log/boot.log
按方向键的上下键,上、下翻看,按q退出来
vi
2.复制目录 cp – r/tmp/test01/opt
cp –r ./aidon ./test:把当前目录aidon复制到test目录;复制多个时加多个空格即可。
复制目录:
[root@hadoop01 ~]# cd /home/ 切换到home目录
[root@hadoop01 aidon]# cd ../ 两点退回到上层目录
[root@hadoop01 home]# ll 查看目录下的内容
total 24
drwx------. 4 aidon aidon 4096 Apr 19 05:23 aidon
drwxr-xr-x. 2 root root 4096 Apr 19 04:17 test
drwxr-xr-x. 2 root root 4096 Apr 19 04:32 test1
drwxr-xr-x. 3 root root 4096 Apr 19 04:36 test2
drwxr-xr-x. 2 root root 4096 Apr 19 04:47 test4
drwxr-xr-x. 2 root root 4096 Apr 19 04:47 test5
注意:[root@hadoop01 home]# ll ./test/ ll 与 ./test/ 之间必须有空格
[root@hadoop01 home]# ll ./test/
total 0
[root@hadoop01 home]# ll ./test
total 0
./test 与 ./test/ 区别应该不大,二者结果相同
把当前aidon目录复制到当前test目录 不可以不加 –r:
[root@hadoop01 home]# cp ./aidon/ ./test
cp: omitting directory `./aidon/'
当前的目录里面可能有东西,必须要加 –r(目录下面有东西,仍然给你复制):
[root@hadoop01 home]# cp -r ./aidon/ ./test 加–r,
[root@hadoop01 home]# ll ./test
total 4
drwx------. 4 root root 4096 Apr 19 06:45 aidon
aidon下面有东西都复制过来了:
[root@hadoop01 home]# ll ./test/aidon/
total 12
-rw-r--r--. 1 root root 53 Apr 19 06:45 123
-rw-r--r--. 1 root root 0 Apr 19 06:45 abc
-rw-r--r--. 1 root root 9 Apr 19 06:45 def
-rw-r--r--. 1 root root 11 Apr 19 06:45 gh
复制后aidon仍然保留在原地方,只不过多了一份
[root@hadoop01 home]# ll
total 24
drwx------. 4 aidon aidon 4096 Apr 19 05:23 aidon
drwxr-xr-x. 3 root root 4096 Apr 19 06:45 test
drwxr-xr-x. 2 root root 4096 Apr 19 04:32 test1
drwxr-xr-x. 3 root root 4096 Apr 19 04:36 test2
drwxr-xr-x. 2 root root 4096 Apr 19 04:47 test4
drwxr-xr-x. 2 root root 4096 Apr 19 04:47 test5
3.复制文件 cp/tmp/file1.txt/opt
复制单个文件:
把aidon下面的123文件复制到test目录下面来 cp ./aidon/123 ./test
[root@hadoop01 home]# ll ./aidon
total 12
-rw-r--r--. 1 root root 53 Apr 19 05:49 123
-rw-r--r--. 1 root root 0 Apr 19 05:00 abc
-rw-r--r--. 1 root root 9 Apr 19 05:02 def
-rw-r--r--. 1 root root 11 Apr 19 05:06 gh
[root@hadoop01 home]# cp ./aidon/123 ./test
[root@hadoop01 home]# ll ./test
total 8
-rw-r--r--. 1 root root 54 Apr 21 17:49 123
drwx------. 4 root root 4096 Apr 21 17:44 aidon
复制多个文件、目录:
把aidon目录下的abc ,def ,test5 复制到test目录:
cp -r ./aidon/abc ./aidon/def ./test5/ ./test
[root@hadoop01 home]# cp -r ./aidon/abc ./aidon/def ./test5/ ./test
[root@hadoop01 home]# ll ./test
total 16
-rw-r--r--. 1 root root 53 Apr 19 07:17 123
-rw-r--r--. 1 root root 0 Apr 19 07:25 abc
drwx------. 4 root root 4096 Apr 19 06:45 aidon
-rw-r--r--. 1 root root 9 Apr 19 07:25 def
drwxr-xr-x. 2 root root 4096 Apr 19 07:25 test5
复制多个加空格:cp -r ./aidon/abc加空格 ./aidon/def 加空格./test5/加空格 ./test
4.移动(Windows中的剪切)、剪切、重命名文件或目录
mv/opt/test01/tmp
mv/opt/file1.txt/tmp/file2.txt
rename :批量
查看当前目录下的test目录: ll ./test
[root@hadoop01 home]# ll ./test
total 16
-rw-r--r--. 1 root root 54 Apr 21 17:49 123
-rw-r--r--. 1 root root 0 Apr 21 17:52 abc
drwx------. 4 root root 4096 Apr 21 17:44 aidon
-rw-r--r--. 1 root root 9 Apr 21 17:52 def
drwxr-xr-x. 2 root root 4096 Apr 21 17:52 test5
[root@hadoop01 home]# ll ./test1
total 0
把test中的123剪切到test1 mv ./test/123 ./test1
[root@hadoop01 home]# mv ./test/123 ./test1
[root@hadoop01 home]# ll./test1
total 4
-rw-r--r--. 1 root root 53 Apr 19 07:17 123
剪切后test中的123的没了的
[root@hadoop01 home]# ll ./test
total 12
-rw-r--r--. 1 root root 0 Apr 21 17:52 abc
drwx------. 4 root root 4096 Apr 21 17:44 aidon
-rw-r--r--. 1 root root 9 Apr 21 17:52 def
drwxr-xr-x. 2 root root 4096 Apr 21 17:52 test5
剪切目录,不加-r 也可以:
[root@hadoop01 home]# mv ./test/aidon ./test1
[root@hadoop01 home]# ll ./test1
total 8
-rw-r--r--. 1 root root 54 Apr 21 17:49 123
drwx------. 4 root root 4096 Apr 21 17:44 aidon
test1之前只有123,现在多了aidon
aidon下面还有很多东西:
[root@hadoop01 home]# ll ./test1/aidon/
total 12
-rw-r--r--. 1 root root 54 Apr 21 17:44 123
-rw-r--r--. 1 root root 0 Apr 21 17:44 abc
-rw-r--r--. 1 root root 9 Apr 21 17:44 def
-rw-r--r--. 1 root root 11 Apr 21 17:44 gh
test下面少了aidon:
[root@hadoop01 home]# ll ./test
total 8
-rw-r--r--. 1 root root 0 Apr 21 17:52 abc
-rw-r--r--. 1 root root 9 Apr 21 17:52 def
drwxr-xr-x. 2 root root 4096 Apr 21 17:52 test5
剪切多个:
把test中的test和abc都剪切到test1 mv ./test/test5/ ./test/abc ./test1
[root@hadoop01 home]# mv ./test/test5/ ./test/abc ./test1
[root@hadoop01 home]# ll ./test1
total 12
-rw-r--r--. 1 root root 54 Apr 21 17:49 123
-rw-r--r--. 1 root root 0 Apr 21 17:52 abc
drwx------. 4 root root 4096 Apr 21 17:44 aidon
drwxr-xr-x. 2 root root 4096 Apr 21 17:52 test5
剪切并重命名:
把test里的def剪切到test1,并重命名为dddef : mv ./test/def /test1/dddef
[root@hadoop01 home]# mv ./test/def ./test1/dddef
[root@hadoop01 home]# ll ./test1
total 16
-rw-r--r--. 1 root root 53 Apr 19 07:17 123
-rw-r--r--. 1 root root 0 Apr 19 07:25 abc
drwx------. 4 root root 4096 Apr 19 06:45 aidon
-rw-r--r--. 1 root root 9 Apr 19 07:25 dddef
drwxr-xr-x. 2 root root 4096 Apr 19 07:25 test5
test里面少了def(test里面什么东西都没了):
[root@hadoop01 home]# ll ./test
total 0
将test1里的test5,剪切到test并重命名为test0
[root@hadoop01 home]# mv ./test1/test5/ ./test/test0
[root@hadoop01 home]# ll ./test
total 4
drwxr-xr-x. 2 root root 4096 Apr 21 17:52 test0
5.重定向和追加 > >>
> 先清空文件内容,后写入新内容
# ls –l /root > /tmp/file1.txt
>> 追加新的内容,就的内容不会消除
# # ls –l /root >> /tmp/file1.txt
在test里面追加“678”:echo “678”>>(两个>>符号) ./test,出现的结果是在test的最后一行显示678
[root@hadoop01 home]# cat ./test1/123
232423
rlersa
w
tw
r
hello world
hello world88888
[root@hadoop01 home]# echo "678" >> ./test1/123
[root@hadoop01 home]# cat ./test1/123
232423
rlersa
w
tw
r
hello world
hello world88888
678
在test里面覆盖“678”:echo “678”>(一个>符号) ./test,cat ./test 出现的结果是只有678
[root@hadoop01 home]# echo "678" > ./test1/123
[root@hadoop01 home]# cat ./test1/123
678
6.屏幕打印 echo
echo “hello world” //打印到屏幕
echo”hello world”/data0/my.cnf //打印到文件
echo”hello world”>>/data0/my.cnf //打印追加到文件
[root@hadoop01 home]# echo "hello world"
hello world
[root@hadoop01 home]# echo "aaaaa"
aaaaa
7.删除
rm //删除文件
[root@hadoop01 home]# ll ./test1
total 12
-rw-r--r--. 1 root root 4 Apr 21 18:16 123
-rw-r--r--. 1 root root 0 Apr 21 17:52 abc
drwx------. 4 root root 4096 Apr 21 17:44 aidon
-rw-r--r--. 1 root root 9 Apr 21 17:52 dddef
删除test1下面的123文件 rm ./test1/123;在问的后面输入yes
[root@hadoop01 home]# rm ./test1/123
rm: remove regular file `./test1/123'? yes 输入yes 被删除, no 不被删除
[root@hadoop01 home]# ll ./test1
total 8
-rw-r--r--. 1 root root 0 Apr 21 17:52 abc
drwx------. 4 root root 4096 Apr 21 17:44 aidon
-rw-r--r--. 1 root root 9 Apr 21 17:52 dddef
[root@hadoop01 home]# rm ./test1/abc
rm: remove regular empty file `./test1/abc'? no 输入no 没有被删除
[root@hadoop01 home]# ll ./test1
total 8
-rw-r--r--. 1 root root 0 Apr 19 07:25 abc
drwx------. 4 root root 4096 Apr 19 06:45 aidon
-rw-r--r--. 1 root root 9 Apr 19 07:25 dddef
rm (麻烦,要回答yes或no)
–rf //删除目录 【目录要加-r,要递归】
rm –f //强制删除
直接删除test1下面的aidon ,不再回答yes或no:rm -rf ./test1/aidon
[root@hadoop01 home]# rm -rf ./test1/aidon/ (r递归,f强制)
[root@hadoop01 home]# ll ./test
total 4
drwxr-xr-x. 2 root root 4096 Apr 21 17:52 test0
rm –rf folder //常用命令 不可恢复