Common Linux commands

Source: Internet
Author: User

Common Linux commands
Basic Linux commands


----------------- Directory section -------------------


[Pwd] displays the current absolute directory


[Cd] switch directory
Cd-display the previous working directory
Cd ~ Displays the Home Directory of the current user.
Cd. display the current directory

Cd .. display the upper-level directory of the current directory

 

[root@centos ~]# cd /usr/[root@centos usr]# lsbin  etc  games  include  lib  libexec  local  my  sbin  share  src  tmp[root@centos usr]# cd -/root[root@centos ~]# cd .[root@centos ~]# cd ..[root@centos /]# cd ~[root@centos ~]# pwd/root

[Ls] displays the list of current directory files


Ls-a displays all files (including hidden files)
The ls-l command is the same as the ll command to display all files in the form of a list
Ls-t display files in chronological order
Ls-m horizontal output file name separated ','
Ls-R recursively displays the current directory and Its subdirectories
Ls-S displays files in sorting by file size

 

 

root@centos ~]# ls -R.:anaconda-ks.cfg  install.log.syslog  lnmp-install.loginstall.log      lnmp                vhost.sh[root@centos ~]# ls -a.                .bash_logout   .gconf              .lesshst          vhost.sh..               .bash_profile  .gconfd             lnmp              .viminfoanaconda-ks.cfg  .bashrc        install.log         lnmp-install.log.bash_history    .cshrc         install.log.syslog  .tcshrc[root@centos ~]# ls -tlnmp-install.log  lnmp             install.logvhost.sh          anaconda-ks.cfg  install.log.syslog[root@centos ~]# ls -l-rw-------. 1 root root    1383 11 7 05:25 anaconda-ks.cfg-rw-r--r--. 1 root root   43838 11 7 05:25 install.log-rw-r--r--. 1 root root   10033 11 7 05:23 install.log.syslog-rwxr-xr-x. 1 root root    1930 11 7 08:00 lnmp-rw-r--r--. 1 root root 1810073 11 7 08:01 lnmp-install.log-rwxr-xr-x. 1 root root    5307 11 7 08:01 vhost.sh[root@centos ~]# ls -manaconda-ks.cfg, install.log, install.log.syslog, lnmp, lnmp-install.log,vhost.sh[root@centos ~]# ls -Sl1848-rw-r--r--. 1 root root 1810073 11 7 08:01 lnmp-install.log-rw-r--r--. 1 root root   43838 11 7 05:25 install.log-rw-r--r--. 1 root root   10033 11 7 05:23 install.log.syslog-rwxr-xr-x. 1 root root    5307 11 7 08:01 vhost.sh-rwxr-xr-x. 1 root root    1930 11 7 08:00 lnmp-rw-------. 1 root root    1383 11 7 05:25 anaconda-ks.cfg

[Tree] print the directory structure using the directory tree


[Mkdir] create a directory

Mkdir./aaa create an aaa directory under the current directory (only one directory can be created)
Mkdir-p./aaa/bbb/ccc create multiple directories (Multi-Level directories can be created)

 

 

[Root @ centos admin] # mkdir/aaa [root @ centos admin] # rm/aaarm: cannot delete "/aaa ": is a directory [root @ centos admin] # rm-f/aaarm: cannot delete "/aaa ": is a directory [root @ centos admin] # rm-rf/aaa [root @ centos admin] # mkdir. /aaa [root @ centos admin] # lsaaa [root @ centos admin] # mkdir. /aaa/bbb/cccmkdir: the directory cannot be created ". /aaa/bbb/ccc ": the file or directory [root @ centos admin] # mkdir-p. /aaa/bbb/ccc [root @ centos admin] # lsaaa [root @ centos admin] # tree. ├ ── aaa │ └ ── bbb │ └ ── ccc

Rmdir: delete a directory.

Rmdir can only delete one empty directory

 

 

[Root @ centos admin] # rmdir. /aaa rmdir: Delete ". /aaa "failed: the directory is not empty [root @ centos admin] # rmdir. /aaa/bbb/ccc [root @ centos admin] # tree. ├ ── aaa │ └ ── bbb rm can also delete directories (you can forcibly Delete non-empty directories and use them with caution) [root @ centos admin] # rm/aaa rm: unable to delete "/aaa": A directory [root @ centos admin] # rm-f/aaa rm: Unable to delete "/aaa ": is a directory [root @ centos admin] # rm-rf/aaa [root @ centos admin] #

------------------ FILE Section -------------------------


[Touch] File Creation command
Touch file name
Touch-d yyyymmdd file name modification File Creation Time

 

 

[Root @ centos aaa] # lsbbb [root @ centos aaa] # touch a.txt [root @ centos aaa] # ll total usage 4-rw-r -- r -- 1 root 0 December 25 21:32 a.txt drwxr- xr-x 2 root 4096 December 25 21:21 bbb [root @ centos aaa] # touch-d 20150102 a.txt [root @ centos aaa] # ll total usage 4-rw-r -- r -- 1 root 0 january 2 2015 a.txt drwxr-xr-x 2 root 4096 December 25 21:21 bbb

[Cp] file copy command
Cp source file target file
Cp-r source file target file // The entire source file will be copied

 

 

[Root @ centos aaa] # cp-r. // var/c [root @ centos c] # cd/var/c [root @ centos c] # ll total usage 8-rw-r -- r -- 1 root 0 December 25 21:42 a.txt drwxr- xr-x 2 root 4096 December 25 21:42 bbb

Of course, we can also use./* to represent all files in the current directory.
You can copy it again.


[Mv] Move or rename a file
Target mv source file


[Cat] viewing files starts from the first line.
[Tac] Viewing from the last row


[File] display file type
File Name

 

 

[root@centos aaa]# file a.txt a.txt: ASCII text

[More] view the file content on one page
More file name
/String
? String
Can be used to search for a specified string.

B or ctrl + B flip up
Enter a space or press enter to flip down the page


Q exit


[Less] Same as more
N flip down
N page flip
Q exit


[Head] displays the first few lines of the file content.
Head-n numeric file name
[Tail] displays the last few lines of the file content.
Tail-n numeric file name


[Man] manual query command

 

------------------- System part ----------------------
[Init] system commands
Parameters:
Init 0 Shutdown
Init 6 restart


[Shutdown] shutdown/warning
Shutdown-t seconds before automatic shutdown
Shutdown-r restart
Shutdown-h shut down immediately


------------------- Command alias ----------------------
[Alias] configuration file to be modified
First, switch to the user's home directory cat ~
Second, edit the file. bash vi. bash_profile.
Again: add the command you want to modify and Its alias to the file.
Alias rm = '/bin/rm-F'
Save and exit: esc +! Wq
Source: source. bash_profile


Principle: The. bash_profile file is automatically loaded during user login initialization.
Source is actually re-loading and executing the file
Which of the following statements is different from the shell script directly executed using./XX. sh?
It is valid globally, and shell is only valid in the associated directory.


------------------- Basic query command ---------------------
[Find]
Search by Time
-Atime: the access time for changing the file during reading or execution is equivalent to lu-l.
-When mtime is used to write a file, change the file time. make time is equivalent to ls-l.
-Ctime: write files, change permissions, owner, and file link are equivalent to lc-l.
Or set the lnode timestamp.


Common commands:
Find./-mtime n search for files modified within n days by Time
-Mtime + n days ago, files modified on the nth day are not included
-Mtime-n contains the nth day after n days


For example, if-mtime + 3 is 0103 today, it is to find all the modified files in 1231 and earlier versions.
-Mtime-3: 0103 today. Then, you can find the modified files in 0101-0103.


Find./-newer file1 is new to file1.


Find./-name filename file name contains filename File


[Special commands]
Find./-name filename-exec ls-l {}\;

 

[Root @ centos ~] # Find./-name lnmp-exec ls-l "{}" \;-rwxr-xr-x. 1 root 1930 November 7 08:00./lnmp

For example, we want to delete all the. conf files.
Find/-name "*. conf"-exec rm-rf {}\;


Another example is the file we want to copy.
Find/-name "*. conf"-exec cp {}/home/my /\;


Rename the suffix of the. txt file to. txt d.
Find./-name "*. txt"-exec mv "{}" "{} d "\;


Explanation:


-The exec parameter is followed by the command, and its termination is,
Therefore, the semicolon following this command is indispensable,
Given that semicolons in various systems have different meanings, the front is followed by a backslash.


{} Curly brackets indicate the names of the files found by the previous find.


[History] view commands executed in history
History
History-c Clear history Execute Command


Echo print
Echo $ PATH print the environment variable directory to: Split


Grep command

-C: only counts matching rows.
-I is case insensitive (only applicable to single characters)
-N: displays the matched rows and row numbers.
-S does not display error messages when they do not exist
-V: Display All rows that do not contain matched text
-L complete matching row Information

 

 

[root@centos ~]# cat a.txt | grep h 5,6,h7,8,9,h10,h[root@centos ~]# cat a.txt | grep -v h1,n,23,4,n[root@centos ~]# cat a.txt | grep -n h3:5,6,h4:7,8,9,h5:10,h[root@centos ~]# 

The following commands are generally not used independently but are used after the MPs queue.
Sort-r reverse sorting
Cut-d 'delimiter'-f multiple fields separated by commas
Uniq-c COUNT // generally used after sort; otherwise, the statistics may be invalid.
Wc-l statistics on the number of rows

 

 

[Root @ centos ~] # Ll | cut-d ''-f5-13 | sort-s | uniq-c 1 1 10033 November 7 05:23 install. log. syslog 1 1383 November 7 05:25 anaconda-ks.cfg 1 1810073 November 7 08:01 lnmp-install.log 1 1930 November 7 08:00 lnmp 1 43838 November 7 05:25 install. log 1 5307 08:01 vhost. sh [root @ centos ~] # Ll | cut-d ''-f5-13 | sort-s | wc-l7


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.