20 Useful Linux command-line tricks

Source: Internet
Author: User
Tags create directory diff gopher readable tmp folder password protection

Unix Command Line Tricks–part I

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

Deleting a HUGE file

I had a huge log file 200GB I need to delete on a production Web server. My rm and LS command was crashed and I were afraid the system to a crawl with huge disk I/O load. To remove a HUGE file, enter:

>/path/to/file.log# or use the following syntax: >/path/to/file.log # finally delete it Rm/path/to/file.log
Want to cache console output?

Try the script command line utility to create a typescript of everything printed on your terminal.

Script My.terminal.sessio

Type commands:

Lsdatesudo Service Foo Stop

To exit (to end script session) type exit or logout or presscontrol-D

Exit

To view type:

More my.terminal.sessionless My.terminal.sessioncat my.terminal.session
Restoring deleted/tmp Folder

As my journey continues with Linux and Unix shell, I made a few mistakes. I accidentally deleted/tmp folder. To restore it all you have to do is:

Mkdir/tmpchmod 1777/tmpchown root:root/tmpls-ld/tmp
Locking a Directory

For privacy the My data I wanted to lock down/downloads on my file server. So I ran:

chmod 0000/downloads

The root user can still has access and LS and CD commands won't work. To go back:

chmod 0755/downloads
Password protecting file in vim text editor

Afraid that root user or someone could snoop into your personal text files? Try password protection to a file in Vim, type:

Vim +x filename

Or, before quitting in vim with vim :X command to encrypt your file and Vim would prompt for a password.

Clear gibberish all over the screen

Just Type:

Reset
Becoming human

Pass the -h or other -H options command line option to GNU or BSD utilities to get output of command commands Like LS, DF, Du, in human-understandable formats:

ls-lh# print sizes in human readable format (e.g., 1K 234M 2G) df-hdf-k# show output in bytes, KB, MB, or Gbfree-bfree -kfree-mfree-g# print sizes in human readable format (e.g., 1K 234M 2G) du-h# get file system perms in human readable fo Rmatstat-c%a/boot# Compare Human readable numberssort-h-a file# display the CPU information in human readable format On a linuxlscpulscpu-elscpu-e=cpu,node# Show the size of each file but in  a more human readable waytree-htree-h/b Oot
Show information about known users in the Linux based system

Just Type:

# # Linux version # #lslogins # # BSD version # #logins

Sample outputs:

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-d             EC17 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 + Apache 0 1 Apache Haldaemon 0 1 HAL daemon VCSA 0 1 Virtual Console memory own ER tcpdump 0        1. sshd 0 1 privilege-separated SSH dbus 0 1 Syst        EM message bus postfix 0 1 Nobody 0 1 Nobody173 abrt 0          1497 Vnstat 0 1 Vnstat user498 nginx 0 1 nginx user499 saslauth 0 1 "SASLAUTHD user"
How does I fix mess created by accidentally untarred files on the current dir?

So I accidentally untar a tarball in/var/www/html/directory instead of/home/projects/www/current. It created mess in/var/www/html/. The easiest-to-fix this mess:

Cd/var/www/html//bin/rm-f "$ (tar ztf/path/to/file.tar.gz)"
Confused on a top command output?

Seriously, you need-try out htop instead of top:

sudo htop
Want to run the same command again?

Just type !! . For example:

/myhome/dir/script/name arg1 Arg2 # to run the same command again!! # to run, the last command again as root Usersudo!!

The !! repeats the most recent command. To run the most recent command beginning with "foo":

!foo# Run The most recent command beginning with "service" as Rootsudo!service

The use to run command with the last argument of the most !$ recent command:

# Edit Nginx.confsudo vi/etc/nginx/nginx.conf # Test nginx.conf for errors/sbin/nginx-t-c/etc/nginx/nginx.conf # after Testing a file with "/sbin/nginx-t-c/etc/nginx/nginx.conf", you# can edit file again with Visudo VI!$
Get a reminder if you had to leave

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

Leave +HHMM

Where,

    • hhmm-The time of day was in the form hhmm where HH was a time in hours (on a or hour clock), and mm are minutes. All times is converted to a hour clock, and assumed to is in the next hours.
Home Sweet Home

Want to go the directory is were just in? Run:
cd -
Need to quickly return to your home directory? Enter:
cd
The variable CDPATH defines the search path for the directory containing directories:

Export CDPATH=/VAR/WWW:/NAS10

Now, instead of typing cd /var/www/html/ I can simply type the following to CD into/var/www/html path:

CD HTML
Editing a file being viewed with less pager

To-edit a file being viewed with less pager, press v . You'll have the file for edit under $EDITOR:

Less *.cless foo.html## Press V to edit file # # # Quit from editor and you would return to the less pager again # #
List all files or directories on your system

To see all of the directories on your system, run:

Find/-type D | Less # List all directories in your $HOMEfind $HOME-type d-ls | Less

To see all of the files, run:

Find/-type F | Less # List All files in your $HOMEfind $HOME-type f-ls | Less
Build directory trees in a single command

You can create directory trees one at a time using mkdir command by passing the -p option:

Mkdir-p/jail/{dev,bin,sbin,etc,usr,lib,lib64}ls-l/jail/
Copy file into multiple directories

Instead of running:

Cp/path/to/file/usr/dir1cp/path/to/file/var/dir2cp/path/to/file/nas/dir3

Run the following command to copy file into multiple dirs:

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

Creating a Shell function is left as an exercise for the reader

Quickly find differences between, directories

The diff command compare files on line. It can also compare-directories:

Ls-l/tmp/rls-l/tmp/s# Compare-Folders using diff # #diff/tmp/r//tmp/s/

Fig.: Finding differences between folders

Text formatting

You can reformat each paragraph with fmt command. In this example, I ' m going to reformat file by wrapping overlong lines and filling short lines:

FMT file.txt

You can also split long lines, but does not refill i.e. wrap overlong lines, but does not fill short lines:

Fmt-s file.txt
See the output and write it to a file

Use the tee command as follows to see the output of the also write to a log file named My.Log:

Mycoolapp arg1 arg2 Input.file | Tee My.Log

The tee command ensures that you'll see Mycoolapp output on the screen and to a file same time.

20 Useful Linux command-line tricks

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.