Linux 101 hacks command

Source: Internet
Author: User

Original article link

Use the CD command:
1: Use cdpath to define the root directory of your CD command
For example, if it is no longer in/etc
Export cdpath =/etc
CD mail
Will enter the/etc/mail directory
In this way, we can add export cdpath = path to the. bash_profile file in our home directory, so that we can set a common directory as the starting directory of our CD.

2: For a very deep directory, if you want to return to the upper or upper two levels or multi-level, it will be very annoying to input a lot of http://www.cnblogs.com/........we can add in .bash_profile
Alias... = "CD .."
Alias... 2 = "CD ../.."
Alias .. 3 = "CD http://www.cnblogs.com /.."
Alias .. 4 = "CD http://www.cnblogs.com .."
Alias .. 5 = "CD http://www.cnblogs.com/http://www.cnblogs.com .."
In this way, we can use .. 5 to put it back to the directory of Level 5.
Of course, you can define aliases in any form, such as using CD2 = "CD...", or CD... = "CD.
In this way, we can use our custom "shortcut keys.

3: a command to create a directory & enter this directory
$ VI. bash_profile
Function mkdircd () {mkdir-P "$ @" & eval cd "/"/$ #/"";}
We can use this self-made mkdircd command to achieve our goal.

4: CD-we can return the directory we visited last time, so that we will not be annoyed by jumping out of a deep directory for incorrect input. Instead of using the up/down key or history to find the last command.

5: popd, pushd, dirs:
Pushd: import the current directory to the stack
Popd: export the current directory to the stack and enter the Directory
Dirs: list the directories in the stack.

6: Use "shopt-s cdspell" to correct your input errors
Now, run sh to enter the above command. If you want to enter the/tmp directory, the wrong command is CD/temp, then, the system will automatically correct your mistakes (if you do not have the/TEMP directory). You can also try other spelling mistakes.

 

Date operation:
1: set the date
Date {mmddhhmiyyyy. SS}
Mm-month
DD-Date
Hh-24 hour format
Mi-minutes
Yyyy-year
SS-seconds
For example: # date 013122192009.53
You can also use the following format:
# Date ++ % Y % m % d-s "20090131"
# Date-s "01/31/2009 22:19:53"
# Date-s "31 Jan 2009 22:19:53"
# Date set = "31 Jan 2009 22:19:53"
Set only the time:
# Date + % t-s "22:19:53"
# Date + % T % P-s "10:19:53"

2: View hardware time
Use hwclock
modify hardware time:
# hwclock-systohc
# hwclock -- systohc-UTC
View System Settings
# Cat/etc/sysconfig/clock

3: set the date format
O % d Date (mm/DD/yy)
O % d day of month (01 .. 31)
O % m month (01 .. 12)
O % Y last two digits of year (00 .. 99)
O % a locale's abbreviated weekday name (Sun .. SAT)
O % a locale's full weekday name, variable length (Sunday... Saturday)
O % B locale's abbreviated month name (Jan .. dec)
O % B locale's full month name, variable length (January... December)
O % H hour (00 .. 23)
O % I hour (01 .. 12)
O % Y year (1970 ...)
Example:
$ Date
Thu Jan 1 08:19:23 PST 2009
$ Date -- date = "now"
Thu Jan 1 08:20:05 PST 2009
$ Date -- date = "today"
Thu Jan 1 08:20:12 PST 2009
$ Date -- date = '2017-01-01 00:00:01 UTC + 5 hours '+ % s
18001
$ Date '+ current date: % m/% d/% Y % ncurrent time: % H: % m: % s'
Current date: 01/01/09

4: Get the past and future time
Past Time:
$ Date -- date = '3 seconds ago'
Thu Jan 1 08:27:00 PST 2009
$ Date -- date = "1 day ago"
Wed Dec 31 08:27:13 PST 2008
$ Date -- date = "1 days ago"
Wed Dec 31 08:27:18 PST 2008
$ Date -- date = "1 month ago"
Mon Dec 1 08:27:23 PST 2008
$ Date -- date = "1 year ago"
Tue Jan 1 08:27:28 PST 2008
$ Date -- date = "yesterday"
Wed Dec 31 08:27:34 PST 2008
$ Date -- date = "10 months 2 day ago"
Thu Feb 28 8 08:27:41 PST 2008
Future Time:
$ Date
Thu Jan 1 08:30:07 PST 2009
$ Date -- date = '3 seconds'
Thu Jan 1 08:30:12 PST 2009
$ Date -- date = '4 Hours'
Thu Jan 1 12:30:17 PST 2009
$ Date -- date = 'tomorrow'
Fri Jan 2 08:30:25 PST 2009
$ Date -- date = "1 day"
Fri Jan 2 08:30:31 PST 2009
$ Date -- date = "1 days"
Fri Jan 2 08:30:38 PST 2009
$ Date -- date = "2 days"
Sat Jan 3 08:30:43 PST 2009
$ Date -- date = '1 month'
Sun Feb 1 08:30:48 PST 2009
$ Date -- date = '1 week'
Thu Jan 8 08:30:53 PST 2009
$ Date -- date = "2 months"
Sun Mar 1 08:30:58 PST 2009
$ Date -- date = "2 years"
Sat Jan 1 08:31:03 PST 2011
$ Date -- date = "next day"
Fri Jan 2 08:31:10 PST 2009
$ Date -- date = "-1 days ago"
Fri Jan 2 08:31:15 PST 2009
$ Date -- date = "This Wednesday"
Wed Jan 7 00:00:00 PST 2009

 

1: view the SSH Version
Ssh-V
Openssh_3.9p1, OpenSSL 0.9.7a Feb 19 2003 this is OpenSSH
SSH: SSH Secure Shell 3.2.9.1 (non-encrypted cial version )...

2: Use username to log on to the hostname
Ssh-l username hostname

3: Check the client debugging information:
Ssh-V-l username hostname
(Add the lower-case-V to view it)

4: Use ~ And Control-Z switch SSH Login
After logging on to the remote server using SSH, enter ~ in Sh ~ And CTR + z (when you enter ~ The entered symbol cannot be seen immediately)
In this way, you can switch the remote login session to the background for execution. You can view the jobs command and switch it back with FG % 1.

 

1: grep command
# Grep search_word filename
-V does not display matching rows
-C appears
-R: find it in the folder.
-I case-insensitive
-L display qualified file names

2: Find command
Find path-name filename search by file name
-Type
-Size (+ 100 M: greater than m)
-Mtime (+ 60) unchanged within 60 days-changed within 2 days
-Execute the command after exec is found.
# Find/-type F-name * .tar.gz-size + 100 m-exec LS-l {}/;
# Find/-type F-name * .tar.gz-size + 100 m-exec Rm-f {}/;

3: suppress standard output
>/Dev/null
Suppress error output
2>/dev/null

4: Join two files
5: Change the case of file content tr a-Z A-Z <FILENAME (lowercase arrive write)

6: xargs command
Xargs regards the output of the previous command as the input of the next command.
# Find/etc-name "*. conf" | xargs LS-L is listed in... ... File
Cat url-list.txt | xargs wget-c a file contains your url list, which you can use to download
# Find/-name *. jpg-type F-print | xargs tar-cvzf images.tar.gz find out all JPG files for packaging

7: Sort command
Sort by text
Sort filename
-R Reverse Order
$ Sort-T:-K 2 names.txt-T (followed by the separator)-K (number of fields)
-U to exclude repeated items

8: uniq command
It is often used together with sort to exclude repeated
Sort namesd.txt | uniq
Uniq-C shows the number of times
Uniq-d only displays duplicate

9: Cut command
Show only one column of the file
$ Cut-D:-F 1, 3 names.txt the first and third fields are displayed for files separated:
$ Cut-C 1-8 names.txt displays the first 8 characters in each line of the file
Cut-D:-F1/etc/passwd displays the Unix login names for all the users in the system.

10: STAT command
Display Properties of a file or folder
STAT/etc/My. CNF
-F attributes of a file system

11: Diff command
Diff [Options] file1 file2

12: AC command
AC collects statistics on the user's connection time
-P total time for each person
-D Username: Time of a user

 

1: Zip command
Syntax: Zip example .zip file-name} {file-names}
-R Zip a directory
-{1 .. 9} 1: Fast 9: best default: 6
-P pwd password (the password is displayed)
-E pwd (the password is not displayed)
-T verify a zip file

2: unzip
-V: view detailed decompression process
-L it is difficult to check its content.

3: tar command
Syntax:
Syntax: Tar [Options] [tar-archive-name] [Other-file-names]
Options:
C. Create a tar file.
V verbose mode,
F. Specify the compressed file name.
T: view the content of the TAR file.
X: Extract

Z: in the case of a .gz File
Gzip is faster than bzip, while bzip is more compressed than gzip.
J: Use the. bz2 file.

 

1: PS1-default interaction prompt
Export PS1 = "/u @/h/W>"/u user name,/h host name/W current path, as shown in ~
/T time, // @ 12 hour format time $ kernel_version kernel version number,
2: PS2-continuation interactive prompt
The command is too long. If you want to enter another line, you need to use this/and write it at the end of the line. Then, after entering, the default prompt is> (this is PS2)
Export PS2 = "continue->" can be changed to continue->

3: PS3-Prompt used by "select" Inside shell script

4: PS4-used by "set-X" to prefix tracing output
Prompt in Shell debug mode (set-X to debug)
O $0-indicates the name of script
O $ lineno-displays the current line number within the script

5: prompt_command
After the shell displays ps1, it will execute prompt_command.

6: change the color of the PS1 prompt
$ Export PS1 = "/E [0; 34 m/u @/h/W>/E [M"
/E [color prompt start
X; ym color value
/E [M color prompt ended
The color table is as follows:
Black 0; 30
Blue 0; 34
Green 0; 32
Cyan 0; 36
Red 0; 31
Purple 0; 35
Brown 0; 33

7. Change the background color of the prompt.
/E [{code} m/E [M code: 40-47

 

1: histtimeformat command:
If histtimeformat is defined in the command line, the time when the history command is used is displayed.
Export histtimeformat = '% F % t' % F year month day % t hour minute second

History n displays the last n commands

In the command line, press Ctrl + R to display the reverse-I-Search Prompt. You can find related commands that have been used, and edit the command later.

Run the following command on the command line to run the last command:
!!
! -1
Up Arrow and enter
Press Ctrl + P and enter

! N run the n-record command in history
! Prex: Execute the last command prefixed with prex.

Histsize = N
Histfilesize = n limit the size of History

~ /. Bash_history history save in this file

Histfile =/root/. commandline_warrior save history in the commandline_warrior File

Export histcontrol = ignoredups removes repeated commands in history
Export histcontrol = erasedups remove all repeated commands in history

Export histcontrol = ignorespace the user enters a space before the command, and the command cannot be seen in history.

History-C: clear historical records

Export histsize = 0 restrict the use of the History command

Histignore ignores some commands (the parameters are strictly consistent)
Example: Export histignore = "PWD: ls-LTR:" ignore PWD, ls, LS-LTR

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.