Share 20 unix/linux command Tips _linux Shell

Source: Internet
Author: User
Tags chmod diff mkdir gopher readable tmp folder password protection

Let's start a new year with these unix/linux command techniques to improve productivity at the end. I've been looking for it for a long time and I'm going to share it with you now.

>/path/to/file.log
# or use the following format
: >/path/to/file.log
 
# then delete it 
Rm/path/to/file.log

How do I record terminal output?

Try using the script command line tool to create output records for your terminal output.

Script My.terminal.sessio

Enter command:

LS
date
sudo service foo stop

To exit (end a script session), enter exit or logout or press control-d.

Exit

To browse for input:

More my.terminal.session
less my.terminal.session
cat my.terminal.session

Restore the deleted/tmp folder

I am in the article Linux and Unix shell, I made some mistakes. I accidentally deleted the/tmp folder. To restore it, I need to do this:

Mkdir/tmp
chmod 1777/tmp
chown root:root/tmp
ls-ld/tmp

Lock a folder

For my data privacy, I want to lock the/downloads folder under my file server. So I ran it:

chmod 0000/downloads

Root users still have access, while the LS and CD commands do not work. To restore it, use:

chmod 0755/downloads

Password-protected files in vim

Afraid of the root user or anyone else spying on your personal files? Try the password protection in vim, enter:

Vim +x filename

Or, use the X command to encrypt your file before exiting Vim, and Vim prompts you to enter a password.

Clear garbled on the screen

Just enter:

Reset

Easy to read format

Pass the- H or- H(and other options) options to the GNU or BSD tool to get the output of commands such as LS, DF, Du, in an easy to read format:

LS-LH
# in readable format (for example: 1K 234M 2G)
df-h
df-k
# in bytes, KB, MB, or GB output: 
free-b
free-k
free-m Free-g
# Output in readable format (e.g. 1K 234M 2G)
du-h
# Displays file system permissions in readable format
stat-c%a/boot
# More readable numbers
sort-h -A file
# Displays CPU information in readable form on Linux
lscpu
lscpu-e
lscpu-e=cpu,node
# Displays the size
of each file in readable form Tree-h
tree-h/boot

Displaying known user information in a Linux system

Just enter:

# # Linux Version # #
lslogins
 
# BSD version # #
Logins

Sample output:

UID USER pwd-lock pwd-deny last-login GECOS
 0 root  0 0 22:37:59 Root
 1 bin  0 1  bin
 2 daemon  0 1  Daemon
 3 adm  0 1  ADM
 4 LP  0 1  LP
 5 sync  0 1  sync
 6 shutdown  0 1 2014-dec17 shutdown
 7 halt  0 1  halt
 8 mail  0 1  Mail
 ten uucp  0  1 UUCP
 operator  0 1  operator
 Games  0 1  Games
 Gopher  0 1  Gopher
 FTP  0 1  ftp User
 mysql  0 1  mysql Server
 ntp  0 1 MB
 Apache  0 1  Apache
 haldaemon 0 1  HAL daemon
 vcsa  0 1  Virtual Console memory owner
 72 tcpdump  0 1
 sshd  0 1  privilege-separated SSH
 bayi dbus  0 1  System message bus  0 1 postfix
 nobody  0 1  Nobody
173 abrt  0 1
497 vnstat  0 1  Vnstat user
498 nginx  0 1  nginx user
499 saslauth  0 1  "SASLAUTHD user"

How do I delete files that were accidentally unzipped under the current folder?

I accidentally unzipped a tarball in/var/www/html/instead of/home/projects/www/current. It messes up the files under/var/www/html, and you don't even know which ones are being misunderstood. The easiest way to fix this problem is to:

cd/var/www/html/
/bin/rm-f "$ (tar ztf/path/to/file.tar.gz)"

Wondering about the output of the top command?

Seriously, you should try using Htop instead of top:

sudo htop

Want to run the same command again

You only need to enter!!。 Like what:

/myhome/dir/script/name arg1 arg2
 
# to run the same command again 
!!
 
# # to run the last command run by root user
sudo!!

!! Will run the most recently used command. To run the most recently run with "foo" command:

!foo
# to run the last command that starts with "service" in the root user
sudo!service

!$ is used to run a command with the last parameter on it:

# edit nginx.conf
sudo vi/etc/nginx/nginx.conf
 
# test nginx.conf
/sbin/nginx-t-c/etc/nginx/nginx.conf< c19/># Test Finished "/sbin/nginx-t-c/etc/nginx/nginx.conf" You can use VI to edit this document again
sudo vi!$

Remind you at the terminal that you have to go.

If you need a reminder to leave your terminal, enter the following command:

Leave +HHMM

Over here:

    • HHMM -time is in the form of HHMM, HH represents the hour (12 o'clock or 24 hours), MM represents minutes. All the time is converted into the 12 o'clock system and assumed to occur over the next 12 hours.

Sweet Home.

Want to get into the place you just entered? Run:

CD-

Need to get back to your home directory quickly? Input:

Cd

The variable cdpath defines the search path for the directory:

Export CDPATH=/VAR/WWW:/NAS10

Now, without typing the CD */var/www/html/so long, I can enter the following command directly into/var/www/html:

CD HTML

Edit a file while less browsing

To edit a file that you are browsing with less, you can press V. You can edit it using the editor specified by the variable $editor:

Less *.c
less foo.html
# Press the V button to edit the file # # #
# # Exit editor, you can continue to use less to browse # #

List all the files and directories in your system

To see all the directories in your system, run:

Find/-type D | Less
 
# list $home all directories find
$HOME-type d-ls | less

To see all the files, run:

Find/-type F | Less
 
# list all the files in $HOME find
$HOME-type f-ls | less

To construct a tree with a single command

You can create one tree at a time with the mkdir plus-P option:

Mkdir-p/jail/{dev,bin,sbin,etc,usr,lib,lib64}
ls-l/jail/

Copy files to multiple directories

Do not have to run:

Cp/path/to/file/usr/dir1
Cp/path/to/file/var/dir2
cp/path/to/file/nas/dir3

Run the following command to copy the files to multiple directories:

Echo/usr/dir1/var/dir2/nas/dir3 | Xargs-n 1 cp-v/path/to/file

Leave to create a shell function as an exercise for the reader.

Quickly find the difference between two directories

The diff command compares the files by row. But it can also compare two directories:

Ls-l/tmp/r
ls-l/tmp/s
# Use diff to compare two folders
diff/tmp/r//tmp/s/

Picture: Find out the differences between directories

Text formatting

You can reformat each paragraph with the FMT command. In this case, I'm going to split the extra long lines and populate the short lines:

FMT file.txt

You can also split long rows, but do not repopulate, that is, split long lines, but do not fill short lines:

Fmt-s file.txt

You can see the output and write it to a file

Use the tee command to see the output on the screen and write to the log file My.Log as follows:

Mycoolapp arg1 arg2 Input.file | Tee My.Log

Tee ensures that you see the Mycoolapp output on the screen at the same time and write to the file My.Log.

via:http://www.cyberciti.biz/open-source/command-line-hacks/20-unix-command-line-tricks-part-i/

Author: nixcraft Translator: GEEKPI proofreading: Wxy

Related Article

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.