Tag Archive for 'command'

What’s in /proc?

Quicktip:

/proc has subdirectory for each running process as well as some subdirectories for various aspects of your hardware. You can learn lots of details about your cpu, your memory, processes running on your computer, and more by digging around in /proc.

A few things you’ll find in /proc:

  • /proc/cpuinfo: Info about your cpu
  • /proc/meminfo: Info about your RAM
  • In /proc/<pid>/:
    • cmdline – the command line used to start the process
    • cwd – a link to the current working directory of the process
    • environ – the environment variable for the process separated by nulls. Try “cat /proc/<pid>/environ | xargs -n 1 -o”
    • fd – directory containing links to file descriptors opened by the process, including files that have been deleted since the fd was created!

What it’s good for:

  • Recover a deleted file if some process is still writing to it (tail -n 999999999 -f /proc/<pid>/fd/<file handle> > ~/recovered-file; kill <pid>)
  • Check if your cpu is 32 or 64 bit
  • Look into the environment variables your process was started with.
  • See what directory a process is running in.
  • Quickly see the full command like of a process.
  • Much more – look around!

 

From The SDE Tip – Amazon

Popularity: 1% [?]

Shell: Get Partition With Max Disk Space

I wanted to find the partition that has the maximum disk space and it’s mount point. This is the script that Anuj and I (mostly Anuj) came up with.

Explanations:

  • df gives us the disk space.
  • The first awk sorts all the lines numerically based on the 2nd column, starting from the second line.
  • The second awk prints the 6th column which gives the mounted partition information.
  • Finally the combination of head and tail gives us the top result.

Popularity: 1% [?]

Find Files Of A Given Name Instantly

Usually when I have to find a file, I use a find and a grep command together – because I still haven’t learnt to use find properly. This is what I do:

find -L . | grep <name>

But this process is usually slow as it traverses the whole directory structure recursively following the symlinks to find all the files and then greps for the name that I need.

Recently I learnt about a faster way of doing the same – use locate to find files of a given name quickly. The command is:

locate <name>

It creates an index of all the files on the system and searches off it when called for. The index is rebuilt (by default) once a day by a cron’d find so results maybe stale by 24 hours.

If locate is not already present on your system, you could install it by:

sudo yum install mlocate

From the SDE Tip – Amazon

Popularity: 1% [?]

Save File With Sudo Permissions In VIM

A lot of time I open a file, make changes and when the time comes to save it realize that I hadn’t sudo’ed it. Learnt that the following command helps:

:w ! sudo tee %

Here’s the reason:

:w tells the vi to save modification of the file

! sudo tee executes the tee command with sudo permissions taking it’s input from :w

% here means the current file name

From the SDE Tip – Amazon

Popularity: 1% [?]

Kill A Process Running On A Particular Port In Windows

This has become a very common scenario for me these days. I have my grails application running on localhost:8080 and at times I need to manually kill the process. I am writing down the steps so that I know where to look it up quickly.

  • List the processes running on ports
    • netstat -a -o -n
  • Find the one you need. I search for 0.0.0.0:8080 and get the PID.
    • taskkill /F /PID <pid>

Task complete!

Popularity: 2% [?]