Linux Stall
  • Home
  • Android
  • How to
  • Perl
  • Tips
  • Tutorials
No Result
View All Result
Linux Stall
No Result
View All Result
Home Tips

Linux Command Line tips that every Linux user should know

Chankey Pathak by Chankey Pathak
January 12, 2012
in Tips
47 1
21
Linux Command Line tips that every Linux user should know
16
SHARES
807
VIEWS
Share on FacebookShare on Twitter

Below is the collection of Linux command line tips which I’ve found useful for Linux users. To get more information about the command mentioned below just open your terminal and type man <command>.

Things a Linux user must learn

  • Learn bash: No need to refer a lengthy bash guide or something else. Just read the complete man page of bash (man bash).
  • Learn vim: You might be using Emacs or Eclipse for your work all the time but nothing can compete vim.
  • Learn ssh: Learn the basics of passwordless authentication.
  • Learn basics of bash job management: Using &, Ctrl-C, fg, bg, Ctrl-Z, jobs, kill.
  • Learn basic commands for file management: ls and ls -l, less, head, tail and tail -f, ln and ln -s (learn the differences between hard links and soft links), chown, mount, chmod, df, du (du -sk *).
  • Learn basic commands for network management: dig, ifconfig.
  • Learn how to use grep, find and sed.
  • Learn how to use aptitude or yum (depends on the distro) to find and install packages.

For daily use

  • In bash, you may use Ctrl+R to search in command history.
  • In bash, you may use Ctrl+W to delete the last word, and Ctrl+U to delete the complete line.
  • Use cd – command to go back to the previous working directory.
  • Learn how to use xargs.

$ find . -name \*.py | xargs grep some_function

$ cat hosts | xargs -I{} ssh [email protected]{} hostnameX

  • Use pstree -p command to get see the process tree.
  • Learn various signals. For example, to suspend a process, use kill -STOP [pid]. Type man 7 signal in terminal for complete guide.
  • If you want to keep running a background process forever then you can use nohup or disown.
  • Use netstat -lntp command to see what the processes are listening. You should check about lsof also.
  • In your bash script you can use subshells to group commands.

# do something in current dir

(cd /some/other/dir; other-command)

# continue in original dir

  • Trimming of strings: ${var%suffix} and ${var#prefix}. For example if var=foo.pdf, then echo ${var%.pdf}.txt prints “foo.txt”.
  • The output of a command can be treated like a file via <(some command). For example, compare local /etc/hosts with a remote one: diff /etc/hosts <(ssh somehost cat /etc/hosts)
  • Know about “here documents” in bash.
  • Learn how to redirect both standard output and standard error via: some-command >logfile 2>&1.
  • You should know about ASCII table (with hex and decimal values). Type man ascii in terminal.
  • While working remotely via ssh, you should use screen or dtach to save your session.
  • For web deveopers use of curl and curl -I, wget etc is useful.
  • To convert HTML page to text file: lynx -dump -stdin
  • If you must handle XML, xmlstarlet is good.
  • In ssh, learn how to port tunnel with -L or -D (and occasionally -R). Also learn how to access web sites from a remote server.
  • If you were typing a command but then changed your mind, Press Alt+shift+3. It will add # at the beginning and enter it as a comment.

Data processing

  • Learn about sort and uniq.
  • Learn about cut, paste, and join.
  • Learn how to get union, intersection and difference of text files.

cat a b | sort | uniq > c # c is a union b

cat a b | sort | uniq -d > c # c is a intersect b

cat a b b | sort | uniq -u > c # c is set difference a – b

  • Summing all numbers in the second column of a text file, code given below is probably 3X faster and 3X shorter than equivalent Python.

awk ‘{ x += $2 } END { print x }’

  • Learn about strings and grep command.
  • To split files into different parts learn about split (to split by size) and csplit (to split by a pattern).

System debugging

  • To know the status of your disk, cpu or network use iostat, netstat, top (or the better htop), and (especially) dstat.
  • To know your system’s memory status use free and vmstat command.
  • Use mtr which is a network diagnostic tool.
  • To find out which process or socket is using bandwidth, try iftop or nethogs.
  • You may use ab tool which is helpful for quick checking of web server performance.
  • For more serious network debugging take use of wireshark or tshark.
  • Learn how to use strace, and that you can strace a running process (with -p). This is helpful if your program is failing, hanging, or crashing, and you don’t know why.
  • Use the ldd command to check shared libraries.
  • Learn how to connect to a running process with gdb and get its stack traces.
  • Knowledge of /proc is very helpful. Examples: /proc/cpuinfo, /proc/xxx/smaps, /proc/xxx/exe, /proc/xxx/cwd, /proc/xxx/fd/.
  • When debugging why something went wrong in the past? To know about this use the sar command. It collects, reports and saves system activity information.

PS: I think I have missed some tips because they didn’t come in my mind at the moment. If you know some good command line tips then please share them in the comment. Thank you :)

Edit: I found some useful tips from reddit users which they gave after reading this post.

1. ifconfig is deprecated, alternative of it is ip.
2. Use of aliases is also an important thing which I forgot to mention.

Cheat Sheet

Download or bookmark the cheat sheet given below. It is very useful.

linux command line cheat sheet

Noticed “rm -rf / – make computer faster” under file commands in the cheat sheet? Don’t ever do that, that will delete all of your files. Do it on your enemy’s system :P
Tags: Cheat sheetscommand lineTips
Previous Post

How to read a CSV file in Perl?

Next Post

Difference between Unix and Linux

Chankey Pathak

Chankey Pathak

Data Scientist at Morgan Stanley. I've been using Linux since past 12 years. I plan to share what I know about Linux in this blog.

Related Posts

conceptual image, methaphor of busy and fast business, man with airplane desk
Tips

Drastically Speed up your Linux System with Preload

July 23, 2020
Professional Developer programmer working a software website design and coding technology, writing codes and database in company office, Global cyber connection technology.
Tips

Writing A Basic Expect Script For Automating Tasks

July 12, 2020
The three-dimensional network topology infographics with ip addresses 3d illustration
Tips

Useful Remote IPMI Commands For Managing Servers

July 11, 2020
How to run sudo command without password on Linux?
FAQ

How to run sudo command without password on Linux?

June 21, 2015
Add auto-complete to “yum”
Tips

Add auto-complete to “yum”

August 18, 2013
How to display a digital clock in Linux terminal?
Tips

How to display a digital clock in Linux terminal?

September 20, 2012
Next Post
difference between Unix and Linux

Difference between Unix and Linux

Comments 21

  1. BalaC says:
    9 years ago

    The following can be added to bash
    1. Ctrl+U and Ctrl+Y
    2. Alt+.

    Reply
  2. Alex Shinn says:
    9 years ago

    I’ve always used the following for set operations which reduces the size of data to sort:

    comm -1 -2 <(sort a) <(sort b) # a intersect b
    comm -2 -3 <(sort a) <(sort b) # a – b

    Reply
  3. swaminadane says:
    9 years ago

    Hi, can u pls send me the cheat sheet for linux.

    Reply
  4. Seth says:
    9 years ago

    If you were typing a command but then changed your mind, Press Alt+shift+3 works, but Alt-r is a little less awkward on the fingers.

    Reply
    • Chankey Pathak says:
      9 years ago

      That’s a good one. Thanks :)

      Reply
  5. ubungu says:
    9 years ago

    “rm -rf /” – make computer faster
    I like this, lol :D

    Reply
    • Chankey Pathak says:
      9 years ago

      haha :D

      Reply
  6. John Dolmayan says:
    9 years ago

    Great post!

    Reply
  7. Nixxie Pixel says:
    9 years ago

    a great post….. bookmarking it (:

    Reply
  8. Ali Ehsanfar says:
    9 years ago

    Make your computer faster literally :) … what kind of trolling is this? :D

    Reply
    • Chankey Pathak says:
      9 years ago

      Man I’m a serious guy, trust me ;)

      Reply
  9. Alex Adekola says:
    9 years ago

    I need to brush up on my command line skills

    Reply
    • Chankey Pathak says:
      9 years ago

      Try performing every task from command line, it will help :)

      Reply
  10. Akhil Ravidas says:
    9 years ago

    cat a b | sort | uniq -d > c # c is a intersect b

    I dont think your set intersection command is correct, for example:

    > cat a
    1
    1
    2
    3

    > cat b
    3
    4
    5

    > cat a b | sort | uniq -d
    1
    3

    Reply
    • Daniel says:
      9 years ago

      I guess you could work around it:
      (sort <a|uniq && sort <b|uniq)|sort|uniq -d

      Reply
  11. Ed says:
    9 years ago

    I would love to have a text file version of that Cheat Sheet. It’s something I could use to embed into my desktop using Conky.

    Reply
  12. Phil Hudson says:
    9 years ago

    Fantastic tips. Thought I was clued up but I learnt several things. One suggestion: if you can, please revise using monospace type (and highlighting?) for commands.

    Reply
  13. Eddie says:
    9 years ago

    Very good! I am learning the Linux operating system

    Reply
  14. fool says:
    9 years ago

    would be cool tutorial on each tip

    for example:
    iostat, netstat, top (or the better htop), and
    To know your system’s memory status use free and vmstat command.

    I know command, but I’m not sure how to interpret it

    Reply
  15. Robert V says:
    9 years ago

    In which paralelle universe exactly ‘ ifconfig ‘ deprecated??

    Reply
    • Chankey Pathak says:
      9 years ago

      Modern Linux distributions are in the process of deprecating ifconfig and route, replacing them with iproute2.
      Source: http://en.wikipedia.org/wiki/Ifconfig

      Also read this discussion: http://www.reddit.com/r/linux/comments/odqrx/linux_command_line_tips_that_every_linux_user/

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Terms and Conditions
  • Contact Us
  • About Us

© 2012 - 2020 Linux Stall - A place for all your Linux needs.

No Result
View All Result
  • Home
  • Android
  • How to
  • Perl
  • Tips
  • Tutorials

© 2012 - 2020 Linux Stall - A place for all your Linux needs.

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In