kal
kal

Reputation: 29401

What are the most often linux (power) commands that you use?

To find files containing a particular string, I use this often

find . -name * | xargs grep -iH "string"

Upvotes: 2

Views: 2267

Answers (19)

Matt Walsh
Matt Walsh

Reputation: 129

For my grep-ing I define an alias to this mini script.

It does a grep, recursive with line number, ignoring binary files and case. It uses -v to filter out results from my ever-present .svn directories. It then colors the searched-for keyword in the results.

egrep -rniI --color "$1" * | egrep -v "\.svn" | egrep --color -iE "$1|"\044

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882116

For finding which directories take up most space (for potential clean up), start at the desired level, such as /home and execute:

cd /home
du -s * | sort -k1 -n

This gives a sorted (most used at the bottom so you can still see the main offenders if it scrolls) list based on space used such as:

     16  lost+found
     84  daniel
   4284  george
   6308  alice
4413700  bob

You can then run the same command from within /home/bob:

cd /home/bob
du -s * | sort -k1 -n

to get:

    100  src
4413600  p0rn

Hence you now know what's using up most of the space on the /home file system, and Bob will soon be looking for other work :-).

Upvotes: 3

user50049
user50049

Reputation:

My most frequently used commands are ps, lsof (list open files), grep / awk / sed / cut (and various other line editors) as well as netstat.

Lsof is one of the most useful but frequently forgotten. For instance:

# umount /foo
umount: /foo busy

lsof | grep /foo

1339 /bin/bash ... ... ... cwd ... ... ...

Ok, so now I know /foo is busy becuase someone has a shell open and is sitting in /foo :) That's really just the tip of the iceberg. Its also handy to know what fds any given process has open.

A lot of people use find all of the time, when slocate might be better, so don't forget about slocate :) If you find yourself (pardon the pun) doing this:

find / -name foo.txt 

... you'd be better off using slocate first.

Finally, valrgind is commonly associated as a programming tool .. however it is entirely useful for detecting leaks and other erratic behavior in other programs. For instance, if by some crazy way you manage to make sed segfault ... valgrind is neat to take a peek.

Upvotes: 0

Adam Hawes
Adam Hawes

Reputation: 5449

I find that

sed -i "regexp" $file

Is very useful sed on a file, but in place instead of requiring to put the output somewhere then move it over the top of the source.

grep --color -Rne "regexp" file list

Is also handy to do a recursive grep and hilight the found matches. I actually wrote a little shell function called 'svngrep' to skip over the .svn files in our working copies and just look at active code and provide hilighting. The same can be done for Git and others.

I have a .bashrc that contains the following:

alias ls="/bin/ls --color"
alias ll="ls -l"

pgrep and pkill come in handy all of the time. Awk is your friend. Learn its syntax because it can so many great things and save you much time.

du -shc *

Will give you a break down of sizes of all the file you list and also show you the total. Very useful for working out quickly if the current directory is big and what under it might be big.

vim

Self-explanitory.

screen
screen -DR

Screen is the spawn of the Unix god. If you ever work by SSH on a machine then consider using Screen. It gives you a persistent session with the ability to create multiple "tabs" and also detach, leaving screen running after you log off. You can come back later and reattach with 'screen -r'. If your remote session gets booted due to network problems you can log back in and use the detach-reattach method to get your work back, having not lost that last critical edit that took half an hour and you hadn't saved yet. Screen has saved my ass from that countless times.

Upvotes: 0

M.N
M.N

Reputation: 11078

#poweroff

You talking about power commands, right? ;-P

Upvotes: 0

jscoot
jscoot

Reputation: 2109

two power commands:
wget: to download a file
curl: manipulate http headers

btw, your find and grep combination is not safe, what if the filename contains space? the safe way is to do like this:

find . -name * -print0  | xargs -0 grep -iH "String"

Upvotes: 0

cowgod
cowgod

Reputation: 8656

Find if a process is running and get the pids in a tree view:

ps afx | grep 'foo'

Find a string in a directory of files recursively:

grep -r 'foo' *

Make all the files executable in a directory recursively:

chmod -R +x *

Erase a file but keep its existence and permissions:

cat /dev/null > file

Just for fun:

rm -rf /

Upvotes: 0

Pop Catalin
Pop Catalin

Reputation: 62960

My favorite commands in linux are :

ps -ax
kill [n]

Also less known (and dangerous) don't try this at home kids :

hack [targetPC]
nuke [targetPC]
sol    <--this is solitaire
/.     <--opens up slash dot in IE
quote  <--quotes a /. meme from the following list so you can use it to post on /.
       1 Yes but does it run linux?
       2 Can you imagine a Beowulf cluster of those
       3 In Soviet Russia ...
       4 ...
       5 Profit.

Upvotes: 0

Aif
Aif

Reputation: 11220

a dos2unix perl one liner I like :

perl -pi -e 's!\r\n!\n!g' << filename >> 

That can easily be converted in unix2dos by reversing the order in the replacement.

Upvotes: 0

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507145

Mine's are

awk - for filtering and extracting fields
find - for finding files/directories
xargs - build command lists, often i use it with find
less - for quickly browsing/reading files
man/info - for viewing manpages and info pages
emacs - for editing source code
irssi - to get in touch with other developers
cd - to change to home dir and to other directories
killall - to kill not behaving commands (yeah, you get power!)
ps - to list processes (oh noes, i hate hanging mplayers blocking my sound!)
<CTRL>+<R> - completion of commands by searching in history file
<TAB> - for completing directory and file names

And, last but not least, the most often used power command is shutdown, isn't it? :)

Upvotes: 3

unmounted
unmounted

Reputation: 34408

I find nohup very handy, for things that are done over an unreliable connection or take a lot of time, ie nohup python build_big_db_on_this_remote_server.py

Upvotes: 0

orip
orip

Reputation: 75487

The ones I use the most from the command-line are grep and all sorts of zsh goodies, e.g.

# count number of lines in all .java and .py files
wc -l **/*.{java,py}

Upvotes: 2

jtimberman
jtimberman

Reputation: 8258

The command in the original question can be better written as,

grep -RHi "string"

I use grep -R quite frequently. Of course, the find command can be used for fine-tuning the files to search.

I often use the -00 flag to perl to print "paragraph" mode.

perl -wnl -00 -e '/something:/ and print;'

Change a string to something else, inline, while making backup copies of the original file(s) (from Minimal Perl):

perl -s -i.bak -wpl 's/old_string/new_string/g;' *.txt

I like side-by-side diffs:

sdiff -s file1 file2

Or syntax-highlighted diffs:

diff file1 file2 | vim - # or mate - on my Mac

I went looking through my history a bit to see if there were other commands, but sadly(??) most of my system maintenance, administration and programming is done through automated tools, lately all written in Ruby (puppet, capistrano, some home-rolled tools, etc), or are related to SCM (git, svn).

Upvotes: 1

mipadi
mipadi

Reputation: 411012

I use who -T | sort to get a sorted list of logged-in users. Also, to get a sorted list of groups (rather than the normal unsorted list), I use groups | tr ' ' '\n' | sort | tr '\n' ' ' && echo.

Upvotes: 0

Scotty Allen
Scotty Allen

Reputation: 13447

I use find's -exec option fairly often. For instance, I often want to change the permissions for a whole directory tree, giving the directories execute permissions, but not the files. I do this in two steps:

find root_dir -type d -exec chmod 555 {} \;
find root_dir -type f -exec chmod 444 {} \;

The above would make the whole tree readonly to everyone, but still allow anyone to cd into any directory.

Upvotes: 2

Himanshu
Himanshu

Reputation: 482

Not exactly a regular command but rather the short-cut 'Ctrl + r' for auto completion of bash commands.

Upvotes: 1

bugmagnet
bugmagnet

Reputation: 7769

#!/bin/sh
ps axxw | grep $1 | grep -v grep | grep -v boost 
sudo renice -20 `ps axww | grep $1 | grep -v grep | grep -v boost | awk '{print $1}'`

Callling the script boost, I use it to give highest priority to the named application.

Upvotes: 1

Drakonite
Drakonite

Reputation: 1309

I often find myself needing to find a file that contains a particular string. For that I use:

grep -ri 'string'

There is such a wide range of things I need to do on a regular basis, that it is hard to pick out specific commands. Mostly just lots of combinations of sed, awk, grep, and find, with some random until thrown in for processing.

Upvotes: 0

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827724

My top commands:

  • vi (my editor of choice to do almost all configuration tasks)
  • top (list running tasks, and show performance statistics)
  • tar/gzip (compress/uncompress archives)
  • grep (find text on files using regular expressions)
  • man (command manual reference)
  • kill (kill running processes)
  • chmod (change and manage permissions)

Upvotes: 2

Related Questions