Four useful tips for Linux mkdir, tar, and kill commands (1)
We have been doing a task in a regular way until we know there are better solutions. As a follow-up of the Linux tips and tricks series, I will introduce four tips that can help you in various aspects. Start!
Four useful Linux tips
1. Suppose you want to create a very long/complex directory tree similar to the following. What is the most effective way to achieve this?
Similar to the directory tree to be implemented below.
$ cd /home/$USER/Desktop
$ mkdir tecmint
$ mkdir tecmint/etc
$ mkdir tecmint/lib
$ mkdir tecmint/usr
$ mkdir tecmint/bin
$ mkdir tecmint/tmp
$ mkdir tecmint/opt
$ mkdir tecmint/var
$ mkdir tecmint/etc/x1
$ mkdir tecmint/usr/x2
$ mkdir tecmint/usr/x3
$ mkdir tecmint/tmp/Y1
$ mkdir tecmint/tmp/Y2
$ mkdir tecmint/tmp/Y3
$ mkdir tecmint/tmp/Y3/z
In the above case, you can simply run the following command line.
$ mkdir -p /home/$USER/Desktop/tecmint/{etc/x1,lib,usr/{x2,x3},bin,tmp/{Y1,Y2,Y3/z},opt,var}
You can use the tree Command for verification. If not, you can use apt or yum to install the 'tree' software package.
$ tree tecmint
Check the directory structure
We can use the above method to create any complex directory tree structure. Note that this is just a common command, but you can use '{}' to create a hierarchical directory. If necessary, it is very useful in shell scripts.
2. Create a file such as test on the Desktop/home/$ USER/Desktop) and enter the following content.
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
Y
Z
In this case, what would a common user do?
A. He will first create a file. It is best to use the touch command, for example:
$ touch /home/$USER/Desktop/test
B. He will open the file in a text editor, which may be nano, vim or other editors.
$ nano /home/$USER/Desktop/test
C. Then he will input the above content to the file, save and exit.
Ignore the time used by him/her. It takes at least three steps for him/her to execute the above situation.
What can an experienced Linux User do? He will input the following text in the terminal to complete all the tasks. He does not need to perform each step separately.
cat << EOF > /home/$USER/Desktop/test
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
Y
Z
EOF