Linux Command Line tips that every Linux user should know t

  Chankey Pathak        2012-03-21 09:27:03       9,344        0    

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 root@{} 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

LINUX  UNIX  COMMAND LINE  TIPS 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Select all squares with bugs