This article mainly records the entry-level basic operation methods in Linux, and arranges the basic Linux operation statements
File view
pwd lists the path of the current directory and views the current directory
[root@localhost ~]# pwd /root
~Represents the user's home directory
ls lists all files in the current directory
[root@localhost ~]# ls anaconda-ks.cfg
ll (ls -l abbreviation) lists the files in the current directory (with file information)
[root@localhost ~]# ll total 4 -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg [root@localhost ~]# ls -l total 4 -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
ll-a lists all files (including hidden files) in the current directory
[root@localhost ~]# ll -a total 28 dr-xr-x---. 2 root root 135 Sep 8 17:38 . dr-xr-xr-x. 17 root root 224 Sep 7 18:05 .. -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-------. 1 root root 8 Sep 8 17:38 .bash_history -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile -rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc [root@localhost ~]# ^C [root@localhost ~]# ^C [root@localhost ~]# ^C [root@localhost ~]# ^C [root@localhost ~]#
ll --help view ls usage, - help is a help instruction
[root@localhost ~]# ll --help Usage: ls [OPTION]... [FILE]... List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. --author with -l, print the author of each file ............
Create, rename files \ folders
touch filename creates an empty folder
Create an empty header file hello.txt
[root@localhost ~]# touch hello.txt [root@localhost ~]# ll total 4 -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt
mkdir create directory
Create directory abc
[root@localhost ~]# mkdir abc [root@localhost ~]# ll total 4 drwxr-xr-x. 2 root root 6 Sep 18 21:53 abc -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt
MKDIR - P (no error will be reported even if the target file exists)
[root@localhost ~]# mkdir -p abc [root@localhost ~]# ll total 4 drwxr-xr-x. 2 root root 6 Sep 18 21:53 abc -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt [root@localhost ~]#
When we create a new file and do not know whether it exists, we can use the - p parameter to ensure that no error will be reported
mv rename files \ folders
Change abc name to abc
[root@localhost ~]# ll total 4 drwxr-xr-x. 2 root root 6 Sep 18 21:53 abc -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt [root@localhost ~]# mv abc abx [root@localhost ~]# ll total 4 drwxr-xr-x. 2 root root 6 Sep 18 21:53 abx -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt
Link file
Hard link (operation on original file):
[root@localhost ~]# ll total 4 drwxr-xr-x. 2 root root 6 Sep 18 21:53 abx -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 18 21:51 hello.txt [root@localhost ~]# In hello.txt hlink [root@localhost ~]# ln hello.txt hlink [root@localhost ~]# ll total 4 drwxr-xr-x. 2 root root 6 Sep 18 21:53 abx -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 2 root root 0 Sep 18 21:51 hello.txt -rw-r--r--. 2 root root 0 Sep 18 21:51 hlink
Soft link (equivalent to shortcut, cannot delete original file) add - s after ln
[root@localhost ~]# ln -s hello.txt hlink ln: failed to create symbolic link 'hlink': File exists [root@localhost ~]# ll total 4 drwxr-xr-x. 2 root root 6 Sep 18 21:53 abx -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 2 root root 0 Sep 18 21:51 hello.t
Switch directory
cd. Current directory
A. Indicates
[root@localhost ~]# pwd /root [root@localhost ~]# cd . [root@localhost ~]# pwd /root
**cd.. * * go to the upper level directory
Two.. indicate the upper level directory
[root@localhost ~]# pwd /root [root@localhost ~]# cd .. [root@localhost /]# pwd /
After you specify a directory after cd, you can switch to the specified directory
[root@localhost /]# cd / [root@localhost /]# pwd / [root@localhost /]# cd /bin/ [root@localhost bin]# pwd /bin [root@localhost bin]#
**cd ~ * * go to the current household directory
[root@localhost test]# cd ~ [root@localhost ~]# pwd /root
cd xxx/xxx jumps directly to a directory
[root@localhost ~]# cd abx/test/ [root@localhost test]# pwd /root/abx/test
Delete files \ folders (directories)
The rm command can delete a file or directory, or delete all files and their subdirectories below it
For linked files, only the entire linked file is deleted, while the original file remains unchanged
Common usage:
rm delete the file (there will be a prompt dialogue, and entering y means to confirm the deletion)
[root@localhost test]# ll total 0 -rw-r--r--. 1 root root 0 Sep 19 15:28 abc.txt -rw-r--r--. 1 root root 0 Sep 19 15:27 sbc.txt [root@localhost test]# rm abc.txt rm: remove regular empty file 'abc.txt'? y [root@localhost test]# ll total 0 -rw-r--r--. 1 root root 0 Sep 19 15:27 sbc.txt
rm -r delete directory (confirmation required)
To delete a directory, you need to specify the parameter r, otherwise you will be prompted that it cannot be deleted
[root@localhost abx]# rm -r test rm: remove directory 'test'? y [root@localhost abx]# ll total 0
rm -f force delete (no query statement)
[root@localhost abx]# touch a.txt [root@localhost abx]# ll total 0 -rw-r--r--. 1 root root 0 Sep 19 15:40 a.txt [root@localhost abx]# rm -f a.txt [root@localhost abx]# ll total 0
Forced deletion has certain risks. Because there is no recycle bin in Linux, the deleted files cannot be retrieved
rm -rf recursively delete directories and files
The most dangerous operation in Linux is the most destructive
[root@localhost ~]# ll total 4 drwxr-xr-x. 2 root root 6 Sep 19 15:40 abx -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg [root@localhost ~]# mkdir -p abx/test/aaa [root@localhost ~]# cd abx/test/aaa/ [root@localhost aaa]# touch a.txt [root@localhost aaa]# cd ~ [root@localhost ~]# rm -rf abx [root@localhost ~]# ll total 4 -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg
Because rf parameters can recursively delete any data and have strong destructive power, they need to be used with caution!!!
Copy \ paste \ cut
Common methods:
cp copy & paste files
Copy the hello.txt file. The copied file name is hello~bak.txt
[root@localhost ~]# ll total 4 -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg [root@localhost ~]# cd hello.txt -bash: cd: hello.txt: No such file or directory [root@localhost ~]# touch hello.txt [root@localhost ~]# ll total 4 -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 19 15:51 hello.txt [root@localhost ~]# cp hello.txt hello-bak.txt [root@localhost ~]# ll total 4 -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 19 15:52 hello-bak.txt -rw-r--r--. 1 root root 0 Sep 19 15:51 hello.txt [root@localhost ~]#
cp -r copy & paste files or directories
To copy a directory, you need to specify the parameter r
[root@localhost ~]# mkdir abc [root@localhost ~]# ll total 4 drwxr-xr-x. 2 root root 6 Sep 19 23:32 abc -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 19 15:52 hello-bak.txt -rw-r--r--. 1 root root 0 Sep 19 15:51 hello.txt [root@localhost ~]# cp -r abc xyz [root@localhost ~]# ll total 4 drwxr-xr-x. 2 root root 6 Sep 19 23:32 abc -rw-------. 1 root root 1246 Sep 7 18:06 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Sep 19 15:52 hello-bak.txt -rw-r--r--. 1 root root 0 Sep 19 15:51 hello.txt drwxr-xr-x. 2 root root 6 Sep 19 23:32 xyz
mv move (cut) files or directories
Move the directory xyz to abc
[root@localhost ~]# ll abc/ total 0 [root@localhost ~]# mv xyz abc [root@localhost ~]# ll abc/ total 0 drwxr-xr-x. 2 root root 6 Sep 19 23:32 xyz
Content view
cat display text content
[root@localhost ~]# cat anaconda-ks.cfg #version=DEVEL # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom # Use graphical install graphical ......
cat -b display line output
[root@localhost ~]# cat -b anaconda-ks.cfg 1 #version=DEVEL 2 # System authorization information 3 auth --enableshadow --passalgo=sha512 4 # Use CDROM installation media 5 cdrom .......
Split screen display more
Display some content according to screen size
[root@localhost ~]# more anaconda-ks.cfg #version=DEVEL # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom # Use graphical install graphical ........
Output and display
echo is similar to System.out.println() in Java
echo: do not parse escape characters
echo -e: parse escape character
echo $PATH: output environment variable
[root@localhost ~]# echo "Hello\t\t world" Hello\t\t world [root@localhost ~]# echo -e "hello\t\t world" hello world [root@localhost ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Disk usage
df view disk usage
[root@localhost ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/centos-root 8374272 1027072 7347200 13% / devtmpfs 485828 0 485828 0% /dev tmpfs 497944 0 497944 0% /dev/shm tmpfs 497944 7748 490196 2% /run tmpfs 497944 0 497944 0% /sys/fs/cgroup /dev/sda1 1038336 135276 903060 14% /boot tmpfs 99592 0 99592 0% /run/user/0
Screen clearing command
Clear to clear the screen
After use:
[root@localhost ~]#
View memory usage
free
free -m: the display unit is MB
free -h: displays easily recognizable units according to the size of the value
[root@localhost ~]# ^C [root@localhost ~]# free -m total used free shared buff/cache available Mem: 972 121 712 7 138 690 Swap: 1023 0 1023 [root@localhost ~]# free -h total used free shared buff/cache available Mem: 972M 121M 712M 7.6M 138M 690M
Shutdown restart shortcut command
Shutdown - h now shutdown
reboot -h now reboot
Exit exit the current login status