Few useful commands in Linux

There are a lot of commands which I use regularly. Some of them are very frequently used. The problem is rarely I remember all the required syntax and probable options for many of them. There is a very popular comic on xkcd on the same. tarSometimes I check man and sometimes I google them. So to help myself I’m accumulating some of them here.
Not adding up all of the explanations here for those options. They are known and self-explanatory mostly. The main purpose is to keep them handy. This list is infinite and the counter will increase regularly.

  • Find and replace text within file(s)
    $ find . -name "*.ext" -type f | xargs sed -i 's/a/b/g'
  • tar
    $ tar -czvf crawler.tar.gz crawler
    $ tar -C /home/kuntal/Desktop/ -xzvf crawler.tar.gz
  • xargs
    Won’t repeat here. Have already noted this down in detail here.
  • kill
    Regular kill works with pid but process can be killed using regex too
    $ pkill -f "string"
  • Check port status
    $ netstat -tln | grep 27017
    or find by a process $ netstat -plunt | grep java
  • How data packets are coming through
    $ mtr google.com
  • Faketime
    Very useful to test when some process needs to be executed manually at a specific time
    $ faketime 'last Friday 5 pm' /bin/date
    i.e. simply $ faketime "Fri Nov 11 02:00:00 UTC 2016" [command]
  • How long a process is running
    $ ps -p "$$" -o etime=
  • Observe logging
    $ tail -f /var/log/somelogfile.log | grep -C4 "Process completed"
  • Print value of a symbolic link or canonical file name
    $ readlink -f $(which java)
  • Strip header and merge files
    $ sed 1d file2.csv > file2noheader.csv
    $ cat file1.csv file2noheader.csv > outputfile.csv
  • Read file to find out specific word appearance and print the newline counts
    $ cat nohup.out | grep "Fetching xyz data for abc" | wc -l
  • Grep pattern to find out count
    $ cat nohup.out | grep -P '(Some specific string:\s[0-9])' | wc -l
  • CPU details
    $ lscpu
  • Wireless connection details
    $ iwconfig
  • Set SSH client alive
    $ vim ~/.ssh/config
    ServerAliveInterval 30
  • Refresh shell
    $ source ~/.bashrc
  • Find application location
    $ whereis python
    python: /usr/bin/python3.5m /usr/bin/python /usr/bin/python3.5 /usr/bin/python2.7 /usr/lib/python3.5 /usr/lib/python2.7 /etc/python /etc/python3.5 /etc/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python2.7 /usr/include/python3.5m /usr/share/python /usr/share/man/man1/python.1.gz
  • Message output color
    $ RED='\033[0;31m'
    $ NC='\033[0m'
    $ printf "${RED}This is in red.${NC} This is in no color."
  • Append stderr and stdout to a file. More on redirection
    command [$1] 2>&1 | tee -a $LOG
  • Get details of executed process PID

    $ command & echo $!
    $ command & export COMMAND_PID=$!
    $ command & echo $! >> /tmp/command.pid
    $ command > /tmp/command.log 2> /tmp/command.error.log & echo $! >> /tmp/command.pid
    $ echo "command PID: $(cat /tmp/command.pid)"
Advertisements

2 thoughts on “Few useful commands in Linux

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s