U13: Time to tidy up
use the rmdir
command, this will only remove empty directories
![remove_nonempty_directories](https://img-blog.csdnimg.cn/20190115172451180.png)
U14: The art of typing less to do more
you can tab
complete the names of files and programs on most Unix
systems. pressing tab
twice will show you all possible completions.
Task U14.1
Unix stores a list of all the commands that you have typed in each login session. You can access this list by using the history command or more simply by using the up and down arrows to access anything from your history.
U15: U can touch this
copy files to/from places, move files, rename files, remove files, and most importantly, look at files. Unix command touch
will let us create a new, empty file.
U16: Moving text2 and text3
move these files to a new directory (‘Temp’). using the Unix mv
(move) command:
![touch_and_move_command](https://img-blog.csdnimg.cn/20190115183800277.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg3Mjc3MQ==,size_16,color_FFFFFF,t_70)
For the mv
command, we always have to specify a source file (or directory) that we want to move, and then specify a target location. If we had wanted to we could have moved both files in one go by typing any of the following
commands:
![wild_card_character_1](https://img-blog.csdnimg.cn/20190115184535849.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg3Mjc3MQ==,size_16,color_FFFFFF,t_70)
![wild_card_character_2](https://img-blog.csdnimg.cn/20190115184821613.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg3Mjc3MQ==,size_16,color_FFFFFF,t_70)
The asterisk *
acts as a wild-card character, essentially meaning ‘match anything’. The second example works because there are no other files or directories in the directory that end with the letters ‘t’ (if there was, then they would be copied too). Likewise, the third example works because only those two files contain the letters ‘ea’ in their names, except that the directory of Temp, and error was reported here.
mv: cannot move 'Temp' to a subdirectory of itself, 'Temp/Temp'
Task U16.1
Use touch
to create three files called ‘fat’, ‘fit’, and ‘feet’ inside the Temp directory:
The ?
character is also a wild-card but with a slightly different meaning.
![wild_card_character_?_match_only_one](https://img-blog.csdnimg.cn/20190115190002179.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg3Mjc3MQ==,size_16,color_FFFFFF,t_70)
U17: Renaming files
make a new file and move it whilst renaming it at the same time:
![move_and_rename_the_file](https://img-blog.csdnimg.cn/20190116161450737.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg3Mjc3MQ==,size_16,color_FFFFFF,t_70)
So mv
can rename a file as well as move it. The logical extension of this is using mv
to rename a file without moving it (you have to use mv
to do this as Unix does not have a separate ‘rename’ command):
![rename](https://img-blog.csdnimg.cn/20190116162205765.png)
U18: Stay on target
specified a ‘source’ and a ‘target’ location when you are moving a file, then it doesn’t matter what your current directory is.Moving directories is just like moving files.
Task U18.1
Create another Temp directory (Temp3) and then change directory to your home directory. Without changing directory, move the Temp3 directory to inside the /Temp directory.
![mv_regardless_of_the_current_directory](https://img-blog.csdnimg.cn/2019011616332962.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg3Mjc3MQ==,size_16,color_FFFFFF,t_70)
U19: Here, there, and everywhere
The philosophy of ‘not having to be in a directory to do something in that directory’, extends to just about any operation that you might want to do in Unix.
![not_having_to_be_in_the_directory](https://img-blog.csdnimg.cn/20190116163944341.png)
U20: To slash or not to slash?
Task U20.1
![slash](https://img-blog.csdnimg.cn/20190116165527199.png)
In both cases we have a directory named ‘Documentation’ and it is
optional as to whether you include the trailing slash. When you tab complete any Unix directory name, you will find that a trailing slash character is automatically added for you. This becomes useful when that directory contains subdirectories which you also want to tab complete.
U21: The most dangerous Unix command you will ever learn!
remove a directory with the rmdir
command, but rmdir
won’t remove directories if they contain any files. rm
is a very dangerous command; if you delete something with rm
, you will not get it back! It does not go into the trash or recycle can, it is permanently removed. It is possible to delete everything in your home directory (all directories and subdirectories) with rm
. use it with the -i
command-line option which will ask for confirmation before deleting anything:
root@kali:~/Documents/test1/Temp# pwd
/root/Documents/test1/Temp
root@kali:~/Documents/test1/Temp# ls
fat fit Temp3 text1 text11.txt text3.txt text5.txt text7.txt text9.text
feet Temp1 text text10.txt text2.txt text4.txt text6.txt text8.text
root@kali:~/Documents/test1/Temp# rm -i text11.txt
rm: remove regular empty file 'text11.txt'? y
root@kali:~/Documents/test1/Temp# rm -i text4.txt
rm: remove regular empty file 'text4.txt'? y
root@kali:~/Documents/test1/Temp# ls
fat feet fit Temp1 Temp3 text text1 text10.txt text2.txt text3.txt text5.txt text6.txt text7.txt text8.text text9.text
root@kali:~/Documents/test1/Temp# rm -i ?e??
rm: remove regular empty file 'feet'? n
rm: remove regular empty file 'text'? n
root@kali:~/Documents/test1/Temp# rm -i ?e*
rm: remove regular empty file 'feet'? n
rm: cannot remove 'Temp1': Is a directory
rm: cannot remove 'Temp3': Is a directory
rm: remove regular empty file 'text'? n
rm: remove regular empty file 'text1'? n
rm: remove regular empty file 'text10.txt'? n
rm: remove regular empty file 'text2.txt'? n
rm: remove regular empty file 'text3.txt'? n
rm: remove regular empty file 'text5.txt'? n
rm: remove regular empty file 'text6.txt'? n
rm: remove regular empty file 'text7.txt'? n
rm: remove regular empty file 'text8.text'? n
rm: remove regular empty file 'text9.text'? n
root@kali:~/Documents/test1/Temp# rm -i *.txt
rm: remove regular empty file 'text10.txt'? n
rm: remove regular empty file 'text2.txt'? n
rm: remove regular empty file 'text3.txt'? n
rm: remove regular empty file 'text5.txt'? n
rm: remove regular empty file 'text6.txt'? n
rm: remove regular empty file 'text7.txt'? n
root@kali:~/Documents/test1/Temp# rm -i *ex*.*ex*
rm: remove regular empty file 'text8.text'? n
rm: remove regular empty file 'text9.text'? n
Task U21.1
Remove the file and then remove the empty directories
![rmdir_rm](https://img-blog.csdnimg.cn/20190116172750267.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg3Mjc3MQ==,size_16,color_FFFFFF,t_70)
U22: Go forth and multiply
Copying files with the cp
(copy) command is very similar to moving them. Remember to always specify a source and a target location. In Unix, the current directory can be represented by a ‘.’ (dot) character.The default behavior of copy is to overwrite (without warning) files that have the same name,
![cp](https://img-blog.csdnimg.cn/20190116174524826.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg3Mjc3MQ==,size_16,color_FFFFFF,t_70)
U23: Going deeper and deeper
The cp
command also allows us (with the use of a command-line option) to copy entire directories. The -R option means ‘copy recursively’, many other Unix commands also have a similar option.
![cp_directory_ls_multiple_directories](https://img-blog.csdnimg.cn/20190116175642357.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg3Mjc3MQ==,size_16,color_FFFFFF,t_70)
U24: When things go wrong
In general, if a command fails, check your current directory ( pwd ) and check that all the files or directories that you mention actually exist (and are in the right place). Many errors occur because people are not in the right directory!